Jupyter最新版:https://www.cnblogs.com/dotnetcrazy/p/9155310.html

在线演示http://nbviewer.jupyter.org/github/lotapp/BaseCode/blob/master/python/notebook/1.POP/3.list_tuple_dict

更新:新增Python可变Tuple、List切片、Set的扩展:https://www.cnblogs.com/dotnetcrazy/p/9155310.html#extend

今天说说List和Tuple以及Dict。POP部分还有一些如Func、IO(也可以放OOP部分说)然后就说说面向对象吧。

先吐槽一下:Python面向对象真心需要规范,不然太容易走火入魔了 -_-!!! 汗,下次再说。。。

对比写作真的比单写累很多,希望大家多捧捧场 ^_^

进入扩展https://www.cnblogs.com/dotnetcrazy/p/9155310.html#ext

步入正题:


1.列表相关:

Python定义一个列表(列表虽然可以存不同类型,一般我们把相同类型的值存列表里面,不同类型存字典里(key,value))
info_list=[] #空列表
infos_list=["C#","JavaScript"]

遍历和之前一样,for 或者 while 都可以(for扩展:https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse

NetCorevar infos_list = new List<object>() { "C#", "JavaScript" };

遍历可以用foreach,for,while


Python列表的添加

# 末尾追加 infos_list.append("Java")
添加一个列表 infos_list.extend(infos_list2)
# 指定位置插入 infos_list.insert(0,"Python") 
# 插入列表:infos_list.insert(0,temp_list) 

看后面的列表嵌套,是通过下标方式获取,eg: infos_list[0][1]

Python在指定位置插入列表是真的插入一个列表进去,C#是把里面的元素挨个插入进去

NetCore:Add,AddRange,Insert,InsertRange (和Python插入列表有些区别)


Python列表删除系列

infos_list.pop() #删除最后一个
infos_list.pop(0) #删除指定索引,不存在就报错
infos_list.remove("张三") # remove("")删除指定元素,不存在就报错

del infos_list[1] #删除指定下标元素,不存在就报错
del infos_list #删除集合(集合再访问就不存在了)不同于C#给集合赋null

再过一遍

NetCore移除指定索引:infos_list.RemoveAt(1); 移除指定值: infos_list.Remove(item); 清空列表: infos_list.Clear();


Python修改:(只能通过索引修改)

infos_list2[1]="PHP" #只有下标修改一种方式,不存在则异常
# 想按值修改需要先查下标再修改 eg:
infos_list2.index("张三")
infos_list2[0]="GO"
# infos_list2.index("dnt")#不存在则异常

# 为什么python中不建议在for循环中修改列表?
# 由于在遍历的过程中,删除了其中一个元素,导致后面的元素整体前移,导致有个元素成了漏网之鱼。
# 同样的,在遍历过程中,使用插入操作,也会导致类似的错误。这也就是问题里说的无法“跟踪”元素。
# 如果使用while,则可以在面对这样情况的时候灵活应对。

NetCore:基本上和Python一样


Python查询系列:in, not in, index, count

if "张三" in names_list:
  names_list.remove("张三")
if "大舅子" not in names_list:
  names_list.append("大舅子")
names_list.index("王二麻子")
names_list.count("逆天")

NetCoreIndexOf Count

查找用Contains,其他的先看看,后面会讲


Python排序

num_list.reverse() # 倒序
num_list.sort() # 从小到大排序
num_list.sort(reverse=True) # 从大到小

列表嵌套,获取用下标的方式:num_list[5][1]

NetCorevar num_list2 = new List<object>() { 33, 44, 22,new List<object>(){11,55,77} };

不能像python那样下标操作,可以定义多维数组来支持 num_list2[i][j] (PS,其实这个嵌套不太用,以后都是列表里面套Dict,类似与Json)

2.Tuple 元组

这次先说NetCore吧:(逆天ValueTuple用的比较多,下面案例就是用的这个)

 
C#中元组主要是方便程序员,不用自然可以。比如:当你返回多个值是否还用ref out 或者返回一个list之类的? 这些都需要先定义,比较麻烦.元祖在这些场景用的比较多。先说说基本使用:
 
初始化:var test_tuple = ("萌萌哒", 1, 3, 5, "加息", "加息"); //这种方式就是valueTuple了(看vscode监视信息)
需要说下的是,取值只能通过itemxxx来取了,然后就是valueTuple的值是可以修改的
忽略上面说的(一般不会用的),直接进应用场景:
就说到这了,代码部分附录是有的
 
Python:用法基本上和列表差不多(下标和前面说的用法一样,比如test_tuples[-1] 最后一个元素)
定义:一个元素:test_tuple1=(1,)
 
test_tuple=("萌萌哒",1,3,5,"加息","加息")
test_tuple.count("加息")
test_tuple.index("萌萌哒") #没有find方法
test_tuple.index("加息", 1, 4) #从特定位置查找,左闭右开区间==>[1,4)
来说说拆包相关的,C#的上面说了,这边来个案例即可:
a=(1,2)
b=a #把a的引用给b
c,d=a #不是把a分别赋值给c和d,等价于:c=a[0] d=a[1]
来个扩展吧(多维元组)
some_tuples=[(2,"萌萌哒"),(4,3)]
some_tuples[0]
some_tuples[0][1]
3.Dict系列

Python遍历相关

#每一次相当于取一个元组,那可以用之前讲的例子来简化了:c,d=a #等价于:c=a[0] d=a[1]

for k,v in infos_dict.items():
  print("Key:%s,Value:%s"%(k,v))

NetCore:方式和Python差不多

foreach (KeyValuePair<string, object> kv in infos_dict)
{
  Console.WriteLine($"Key:{kv.Key},Value:{kv.Value}");
}


Python增删改系列:

增加、修改infos_dict["wechat"]="dotnetcrazy" #有就修改,没就添加

删除系列

# 删除
del infos_dict["name"] #不存在就报错
#清空字典内容
infos_dict.clear()
# 删除字典
del infos_dict

NetCore

添加:infos_dict.Add("wechat", "lll");  infos_dict["wechat1"] = "lll";
修改

infos_dict["wechat"] = "dotnetcrazy";
 
删除

infos_dict.Remove("dog"); //不存在不报错  infos_dict.Clear(); //列表内容清空


Python查询系列推荐infos_dict.get("mmd") #查不到不会异常


NetCoreinfos_dict["name"] 可以通过 ContainsKey(key) 避免异常。看值就 ContainsValue(value)

扩展:

1.多维元组:

some_tuples=[(2,"萌萌哒"),(4,3)]
some_tuples[0]
some_tuples[0][1]


2.运算符扩展:(+,*,in,not in)

# 运算符扩展:
test_str="www.baidu.com"
test_list=[1,"d",5]
test_dict={"name":"dnt","wechat":"xxx"}
test_list1=[2,4,"n","t",3]
# + 合并 (不支持字典)
print(test_str+test_str)
print(test_list+test_list1)
# * 复制 (不支持字典)
print(test_str*2)
print(test_list*2)

# in 是否存在(字典是查key)
print("d" in test_str) #True
print("d" in test_list) #True
print("d" in test_dict) #False
print("name" in test_dict) #True
 
# not in 是否不存在(字典是查key)
print("z" not in test_str) #True
print("z" not in test_list) #True
print("z" not in test_dict) #True
print("name" not in test_dict) #False


3.内置函数扩展:(len,max,min,del)

len(),这个就不说了,用的太多了

max(),求最大值,dict的最大值是比较的key

这个注意一种情况(当然了,你按照之前说的规范,list里面放同一种类型就不会出错了)

min(),这个和max一样用

del() or del xxx 删完就木有了
 
#可以先忽略cmp(item1, item2)   比较两个值 #是Python2里面有的 cmp(1,2) ==> -1 #cmp在比较字典数据时,先比较键,再比较值

知识扩展

可变的元组(元组在定义的时候就不能变了,但是可以通过类似这种方式来改变)

List切片

Set集合扩展:


更新:(漏了一个删除的方法):

概念再补充下

# dict内部存放的顺序和key放入的顺序是没有关系的
# dict的key必须是不可变对象(dict根据key进行hash算法,来计算value的存储位置
# 如果每次计算相同的key得出的结果不同,那dict内部就完全混乱了)

用一张图理解一下:(测试结果:元组是可以作为Key的 -_-!

附录Code

Python:https://github.com/lotapp/BaseCode/tree/master/python/1.POP/3.list_tuple_dict

Python List:

  1. # 定义一个列表,列表虽然可以存不同类型,一般我们把相同类型的值存列表里面,不同类型存字典里(key,value)
  2. infos_list=["C#","JavaScript"]#[]
  3.  
  4. # ###########################################################
  5. # # 遍历 for while
  6. # for item in infos_list:
  7. # print(item)
  8.  
  9. # i=0
  10. # while i<len(infos_list):
  11. # print(infos_list[i])
  12. # i+=1
  13. # ###########################################################
  14. # # 增加
  15. # # 末尾追加
  16. # infos_list.append("Java")
  17. # print(infos_list)
  18.  
  19. # # 指定位置插入
  20. # infos_list.insert(0,"Python")
  21. # print(infos_list)
  22.  
  23. # temp_list=["test1","test2"]
  24. # infos_list.insert(0,temp_list)
  25. # print(infos_list)
  26.  
  27. # # 添加一个列表
  28. # infos_list2=["张三",21]#python里面的列表类似于List<object>
  29. # infos_list.extend(infos_list2)
  30. # print(infos_list)
  31.  
  32. # # help(infos_list.extend)#可以查看etend方法描述
  33. # ###########################################################
  34. # # 删除
  35. # # pop()删除最后一个元素,返回删掉的元素
  36. # # pop(index) 删除指定下标元素
  37. # print(infos_list.pop())
  38. # print(infos_list)
  39. # print(infos_list.pop(0))
  40. # # print(infos_list.pop(10)) #不存在就报错
  41. # print(infos_list)
  42.  
  43. # # remove("")删除指定元素
  44. # infos_list.remove("张三")
  45. # # infos_list.remove("dnt") #不存在就报错
  46. # print(infos_list)
  47.  
  48. # # del xxx[index] 删除指定下标元素
  49. # del infos_list[1]
  50. # print(infos_list)
  51. # # del infos_list[10] #不存在就报错
  52.  
  53. # # del infos_list #删除集合(集合再访问就不存在了)
  54. # ###########################################################
  55. # # 修改 xxx[index]=xx
  56. # # 注意:一般不推荐在for循环里面修改
  57. # print(infos_list2)
  58. # infos_list2[1]="PHP" #只有下标修改一种方式
  59. # # infos_list2[3]="GO" #不存在则异常
  60. # print(infos_list2)
  61.  
  62. # # 想按值修改需要先查下标再修改
  63. # infos_list2.index("张三")
  64. # infos_list2[0]="GO"
  65. # print(infos_list2)
  66. # # infos_list2.index("dnt")#不存在则异常
  67.  
  68. # # 知识面拓展: https://www.zhihu.com/question/49098374
  69. # # 为什么python中不建议在for循环中修改列表?
  70. # # 由于在遍历的过程中,删除了其中一个元素,导致后面的元素整体前移,导致有个元素成了漏网之鱼。
  71. # # 同样的,在遍历过程中,使用插入操作,也会导致类似的错误。这也就是问题里说的无法“跟踪”元素。
  72. # # 如果使用while,则可以在面对这样情况的时候灵活应对。
  73.  
  74. ###########################################################
  75. # # 查询 in, not in, index, count
  76. # # # for扩展:https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse
  77.  
  78. # names_list=["张三","李四","王二麻子"]
  79.  
  80. # # #张三在列表中执行操作
  81. # if "张三" in names_list:
  82. # names_list.remove("张三")
  83. # print(names_list)
  84.  
  85. # # #查看"大舅子"不在列表中执行操作
  86. # if "大舅子" not in names_list:
  87. # names_list.append("大舅子")
  88. # print(names_list)
  89.  
  90. # # #查询王二麻子的索引
  91. # print(names_list.index("王二麻子"))
  92.  
  93. # print(names_list.count("大舅子"))
  94. # print(names_list.count("逆天"))
  95. ###########################################################
  96. # # 排序(sort, reverse 逆置)
  97. # num_list=[1,3,5,88,7]
  98.  
  99. # #倒序
  100. # num_list.reverse()
  101. # print(num_list)
  102.  
  103. # # 从小到大排序
  104. # num_list.sort()
  105. # print(num_list)
  106.  
  107. # # 从大到小
  108. # num_list.sort(reverse=True)
  109. # print(num_list)
  110. # # ###########################################################
  111.  
  112. # # #列表嵌套(列表也是可以嵌套的)
  113. # num_list2=[33,44,22]
  114. # num_list.append(num_list2)
  115. # print(num_list)
  116. # # for item in num_list:
  117. # # print(item,end="")
  118.  
  119. # print(num_list[5])
  120. # print(num_list[5][1])
  121. # # ###########################################################
  122.  
  123. # # # 引入Null==>None
  124. # # a=[1,2,3,4]
  125. # # b=[5,6]
  126. # # a=a.append(b)#a.append(b)没有返回值
  127. # # print(a)#None

Python Tuple:

  1. # 只能查询,其他操作和列表差不多(不可变)
  2. test_tuple=("萌萌哒",1,3,5,"加息","加息")
  3.  
  4. # count index
  5. print(test_tuple.count("加息"))
  6. print(test_tuple.index("萌萌哒"))#没有find方法
  7. # 注意是左闭右开区间==>[1,4)
  8. # print(test_tuple.index("加息", 1, 4))#查不到报错:ValueError: tuple.index(x): x not in tuple
  9.  
  10. #下标取
  11. print(test_tuple[0])
  12.  
  13. # 遍历
  14. for item in test_tuple:
  15. print(item)
  16.  
  17. i=0
  18. while i<len(test_tuple):
  19. print(test_tuple[i])
  20. i+=1
  21.  
  22. # 扩展:
  23. test_tuple1=(1,) #(1)就不是元祖了
  24. test_tuple2=(2)
  25. print(type(test_tuple1))
  26. print(type(test_tuple2))
  27.  
  28. # # ==============================================
  29. # 扩展:(后面讲字典遍历的时候会再提一下的)
  30. a=(1,2)
  31. b=a#把a的引用给b
  32. #a里面两个值,直接给左边两个变量赋值了(有点像拆包了)
  33. c,d=a #不是把a分别赋值给c和d,等价于:c=a[0] d=a[1]
  34.  
  35. print(a)
  36. print(b)
  37. print(c)
  38. print(d)

Python Dict:

  1. infos_dict={"name":"dnt","web":"dkill.net"}
  2.  
  3. # # 遍历
  4. # for item in infos_dict.keys():
  5. # print(item)
  6.  
  7. # #注意,如果你直接对infos遍历,其实只是遍历keys
  8. # for item in infos_dict:
  9. # print(item)
  10.  
  11. # for item in infos_dict.values():
  12. # print(item)
  13.  
  14. # for item in infos_dict.items():
  15. # print("Key:%s,Value:%s"%(item[0],item[1]))
  16. # #每一次相当于取一个元组,那可以用之前讲的例子来简化了:c,d=a #等价于:c=a[0] d=a[1]
  17. # for k,v in infos_dict.items():
  18. # print("Key:%s,Value:%s"%(k,v))
  19.  
  20. # # 增加 修改 (有就修改,没就添加)
  21. # # 添加
  22. # infos_dict["wechat"]="lll"
  23. # print(infos_dict)
  24.  
  25. # # 修改
  26. # infos_dict["wechat"]="dotnetcrazy"
  27. # print(infos_dict)
  28.  
  29. # # 删除
  30. # del infos_dict["name"]
  31. # del infos_dict["dog"] #不存在就报错
  32. # print(infos_dict)
  33.  
  34. # #清空字典内容
  35. # infos_dict.clear()
  36. # print(infos_dict)
  37.  
  38. # # 删除字典
  39. # del infos_dict
  40.  
  41. # 查询
  42. infos_dict["name"]
  43. # infos_dict["mmd"] #查不到就异常
  44.  
  45. infos_dict.get("name")
  46. infos_dict.get("mmd")#查不到不会异常
  47.  
  48. # 查看帮助
  49. # help(infos_dict)
  50. len(infos_dict) #有几对key,value
  51. # infos_dict.has_key("name") #这个是python2里面的

NetCore:https://github.com/lotapp/BaseCode/tree/master/netcore/1_POP

NetCore List:

  1. // using System;
  2. // using System.Collections.Generic;
  3. // using System.Linq;
  4.  
  5. // namespace aibaseConsole
  6. // {
  7. // public static class Program
  8. // {
  9. // private static void Main()
  10. // {
  11. // #region List
  12. // //# 定义一个列表
  13. // // # infos_list=["C#","JavaScript"]#[]
  14. // var infos_list = new List<object>() { "C#", "JavaScript" };
  15. // // var infos_list2 = new List<object>() { "张三", 21 };
  16. // // // # ###########################################################
  17. // // // # # 遍历 for while
  18. // // // # for item in infos_list:
  19. // // // # print(item)
  20. // // foreach (var item in infos_list)
  21. // // {
  22. // // System.Console.WriteLine(item);
  23. // // }
  24. // // for (int i = 0; i < infos_list.Count; i++)
  25. // // {
  26. // // System.Console.WriteLine(infos_list[i]);
  27. // // }
  28. // // // # i=0
  29. // // // # while i<len(infos_list):
  30. // // // # print(infos_list[i])
  31. // // // # i+=1
  32. // // int j=0;
  33. // // while(j<infos_list.Count){
  34. // // Console.WriteLine(infos_list[j++]);
  35. // // }
  36. // // // # ###########################################################
  37. // // // # # 增加
  38. // // // # # 末尾追加
  39. // // // # infos_list.append("Java")
  40. // // // # print(infos_list)
  41. // // DivPrintList(infos_list);
  42.  
  43. // // infos_list.Add("Java");
  44. // // DivPrintList(infos_list);
  45. // // // # # 指定位置插入
  46. // // // # infos_list.insert(0,"Python")
  47. // // // # print(infos_list)
  48. // // infos_list.Insert(0,"Python");
  49. // // DivPrintList(infos_list);
  50. // // // # # 添加一个列表
  51. // // // # infos_list2=["张三",21]#python里面的列表类似于List<object>
  52. // // // # infos_list.extend(infos_list2)
  53. // // // # print(infos_list)
  54. // // infos_list.AddRange(infos_list2);
  55. // // DivPrintList(infos_list);
  56. // // /*C#有insertRange方法 */
  57. // // DivPrintList(infos_list2,"List2原来的列表:");
  58. // // infos_list2.InsertRange(0,infos_list);
  59. // // DivPrintList(infos_list2,"List2变化后列表:");
  60. // // // # # help(infos_list.extend)#可以查看etend方法描述
  61. // // // # ###########################################################
  62. // // // # # 删除
  63. // // // # # pop()删除最后一个元素,返回删掉的元素
  64. // // // # # pop(index) 删除指定下标元素
  65. // // // # print(infos_list.pop())
  66. // // // # print(infos_list)
  67. // // // # print(infos_list.pop(1))
  68. // // // # # print(infos_list.pop(10)) #不存在就报错
  69. // // // # print(infos_list)
  70.  
  71. // // // # # remove("")删除指定元素
  72. // // // # infos_list.remove("张三")
  73. // // // # # infos_list.remove("dnt") #不存在就报错
  74. // // // # print(infos_list)
  75.  
  76. // // // # # del xxx[index] 删除指定下标元素
  77. // // // # del infos_list[1]
  78. // // // # print(infos_list)
  79. // // // # # del infos_list[10] #不存在就报错
  80.  
  81. // // // # del infos_list #删除集合(集合再访问就不存在了)
  82.  
  83. // // DivPrintList(infos_list);
  84. // // infos_list.RemoveAt(1);
  85. // // // infos_list.RemoveAt(10);//不存在则报错
  86. // // // infos_list.RemoveRange(0,1); //可以移除多个
  87. // // DivPrintList(infos_list);
  88. // // infos_list.Remove("我家在东北吗?"); //移除指定item,不存在不会报错
  89. // // DivPrintList(infos_list,"清空前:");
  90. // // infos_list.Clear();//清空列表
  91. // // DivPrintList(infos_list,"清空后:");
  92.  
  93. // // // # ###########################################################
  94. // // // # # 修改 xxx[index]=xx
  95. // // // # # 注意:一般不推荐在for循环里面修改
  96. // // // # print(infos_list2)
  97. // // // # infos_list2[1]="PHP" #只有下标修改一种方式
  98. // // // # # infos_list2[3]="GO" #不存在则异常
  99. // // // # print(infos_list2)
  100. // // DivPrintList(infos_list2);
  101. // // infos_list2[1] = "PHP";
  102. // // // infos_list2[3]="GO"; //不存在则异常
  103. // // DivPrintList(infos_list2);
  104. // // // # # 想按值修改需要先查下标再修改
  105. // // // # infos_list2.index("张三")
  106. // // // # infos_list2[0]="GO"
  107. // // // # print(infos_list2)
  108. // // // # # infos_list2.index("dnt")#不存在则异常
  109. // // int index = infos_list2.IndexOf("张三");
  110. // // infos_list2[index] = "GO";
  111. // // DivPrintList(infos_list2);
  112. // // infos_list2.IndexOf("dnt");//不存在返回-1
  113.  
  114. // // // ###########################################################
  115. // // // # 查询 in, not in, index, count
  116. // // // # # for扩展:https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse
  117. // // // # names_list=["张三","李四","王二麻子"]
  118. // // var names_list=new List<string>(){"张三","李四","王二麻子"};
  119. // // // Console.WriteLine(names_list.Find(i=>i=="张三"));
  120. // // // Console.WriteLine(names_list.FirstOrDefault(i=>i=="张三"));
  121. // // Console.WriteLine(names_list.Exists(i=>i=="张三"));
  122. // // System.Console.WriteLine(names_list.Contains("张三"));
  123. // // // # #张三在列表中执行操作
  124. // // // # if "张三" in names_list:
  125. // // // # names_list.remove("张三")
  126. // // // # else:
  127. // // // # print(names_list)
  128.  
  129. // // // # #查看"大舅子"不在列表中执行操作
  130. // // // # if "大舅子" not in names_list:
  131. // // // # names_list.append("大舅子")
  132. // // // # else:
  133. // // // # print(names_list)
  134.  
  135. // // // # #查询王二麻子的索引
  136. // // // # print(names_list.index("王二麻子"))
  137. // // // names_list.IndexOf("王二麻子");
  138.  
  139. // // // # print(names_list.count("大舅子"))
  140. // // // # print(names_list.count("逆天"))
  141. // // // Console.WriteLine(names_list.Count);
  142.  
  143. // // // ###########################################################
  144. // // // # # 排序(sort, reverse 逆置)
  145. // // // # num_list=[1,3,5,88,7]
  146. // // var num_list = new List<object>() { 1, 3, 5, 88, 7 };
  147.  
  148. // // // # #倒序
  149. // // // # num_list.reverse()
  150. // // // # print(num_list)
  151. // // num_list.Reverse();
  152. // // DivPrintList(num_list);
  153. // // // # # 从小到大排序
  154. // // // # num_list.sort()
  155. // // // # print(num_list)
  156. // // num_list.Sort();
  157. // // DivPrintList(num_list);
  158.  
  159. // // // # # 从大到小
  160. // // // # num_list.sort(reverse=True)
  161. // // // # print(num_list)
  162. // // num_list.Sort();
  163. // // num_list.Reverse();
  164. // // DivPrintList(num_list);
  165.  
  166. // // // # ###########################################################
  167.  
  168. // // // # #列表嵌套(列表也是可以嵌套的)
  169. // // // # num_list2=[33,44,22]
  170. // // // # num_list.append(num_list2)
  171. // // // # print(num_list)
  172. // // var num_list2 = new List<object>() { 33, 44, 22,new List<object>(){11,55,77} };
  173. // // DivPrintList(num_list2);//可以定义多维数组来支持 num_list2[i][j]
  174. // // // # for item in num_list:
  175. // // // # print(item)
  176. // // // # ###########################################################
  177.  
  178. // // // # # 引入Null==>None
  179. // // // # a=[1,2,3,4]
  180. // // // # b=[5,6]
  181. // // // # a=a.append(b)#a.append(b)没有返回值
  182. // // // # print(a)#None
  183. // #endregion
  184.  
  185. // // Console.Read();
  186. // }
  187.  
  188. // private static void DivPrintList(List<object> list, string say = "")
  189. // {
  190. // Console.WriteLine($"\n{say}");
  191. // foreach (var item in list)
  192. // {
  193. // System.Console.Write($"{item} ");
  194. // }
  195. // }
  196. // }
  197. // }

NetCore Tuple:

  1. // using System;
  2.  
  3. // namespace aibaseConsole
  4. // {
  5. // public static class Program
  6. // {
  7. // private static void Main()
  8. // {
  9. // #region Tuple
  10. // // C#中元组主要是方便程序员,不用自然可以.
  11. // // 元祖系:https://msdn.microsoft.com/zh-cn/library/system.tuple.aspx
  12. // // 值元组:https://msdn.microsoft.com/zh-cn/library/system.valuetuple.aspx
  13. // // 比如:当你返回多个值是否还用ref out 或者返回一个list之类的?
  14. // // 这些都需要先定义,比较麻烦.元祖在一些场景用的比较多 eg:
  15.  
  16. // // 初始化
  17. // // var test_tuple = ("萌萌哒", 1, 3, 5, "加息", "加息"); //这种方式就是valueTuple了
  18.  
  19. // // test_tuple.Item1 = "ddd";//可以修改值
  20.  
  21. // // test_tuple.GetType();
  22. // // test_tuple.itemxxx //获取值只能通过itemxxx
  23.  
  24. // var result = GetCityAndTel(); //支持async/await模式
  25. // var city = result.city;
  26. // var tel = result.tel;
  27. // // 拆包方式:
  28. // var (city1, tel1) = GetCityAndTel();
  29.  
  30. // #endregion
  31. // // Console.Read();
  32. // }
  33. // // public static (string city, string tel) GetCityAndTel()
  34. // // {
  35. // // return ("北京", "110");
  36. // // }
  37. // // 简化写法
  38. // public static (string city, string tel) GetCityAndTel() => ("北京", "110");
  39. // }
  40. // }

NetCore Dict:

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace aibaseConsole
  5. {
  6. public static class Program
  7. {
  8. private static void Main()
  9. {
  10. #region Dict
  11. // infos_dict={"name":"dnt","web":"dkill.net"}
  12. // # # 遍历
  13. // # for item in infos_dict.keys():
  14. // # print(item)
  15. // # for item in infos_dict.values():
  16. // # print(item)
  17. // # for item in infos_dict.items():
  18. // # print("Key:%s,Value:%s"%(item[0],item[1]))
  19. // # #每一次相当于取一个元组,那可以用之前讲的例子来简化了:c,d=a #等价于:c=a[0] d=a[1]
  20. // # for k,v in infos_dict.items():
  21. // # print("Key:%s,Value:%s"%(k,v))
  22. var infos_dict = new Dictionary<string, object>{
  23. {"name","dnt"},
  24. {"web","dkill.net"}
  25. };
  26. // foreach (var item in infos_dict.Keys)
  27. // {
  28. // System.Console.WriteLine(item);
  29. // }
  30. // foreach (var item in infos_dict.Values)
  31. // {
  32. // System.Console.WriteLine(item);
  33. // }
  34. // foreach (KeyValuePair<string, object> kv in infos_dict)
  35. // {
  36. // // System.Console.WriteLine("Key:%s,Value:%s",(kv.Key,kv.Value));
  37. // System.Console.WriteLine($"Key:{kv.Key},Value:{kv.Value}");
  38. // }
  39.  
  40. // // # # 增加 修改 (有就修改,没就添加)
  41. // // # # 添加
  42. // // # infos_dict["wechat"]="lll"
  43. // // # print(infos_dict)
  44. // infos_dict.Add("wechat", "lll");
  45. // infos_dict["wechat1"] = "lll";
  46. // // # # 修改
  47. // // # infos_dict["wechat"]="dotnetcrazy"
  48. // // # print(infos_dict)
  49. // infos_dict["wechat"] = "dotnetcrazy";
  50.  
  51. // // # # 删除
  52. // // # del infos_dict["name"]
  53. // // # del infos_dict["dog"] #不存在就报错
  54. // // # print(infos_dict)
  55. // infos_dict.Remove("name");
  56. // infos_dict.Remove("dog");
  57. // // # #清空列表内容
  58. // // # infos_dict.clear()
  59. // // # print(infos_dict)
  60. // infos_dict.Clear();
  61. // // # # 删除列表
  62. // // # del infos_dict
  63.  
  64. // # 查询
  65. // infos_dict["name"]
  66. // infos_dict["mmd"] #查不到就异常
  67.  
  68. // infos_dict.get("name")
  69. // infos_dict.get("mmd")#查不到不会异常
  70. Console.WriteLine(infos_dict["name"]);
  71. // Console.WriteLine(infos_dict["mmd"]); //#查不到就异常
  72. // 先看看有没有 ContainsKey(key),看值就 ContainsValue(value)
  73. if (infos_dict.ContainsKey("mmd")) Console.WriteLine(infos_dict["mmd"]);
  74.  
  75. // # 查看帮助
  76. // help(infos_dict)
  77. // len(infos_dict) #有几对key,value
  78. Console.WriteLine(infos_dict.Count);
  79.  
  80. #endregion
  81.  
  82. // Console.Read();
  83. }
  84. }
  85. }

Python3 与 NetCore 基础语法对比(List、Tuple、Dict、Set专栏)的更多相关文章

  1. Python3 与 C# 面向对象之~继承与多态 Python3 与 C# 面向对象之~封装 Python3 与 NetCore 基础语法对比(Function专栏) [C#]C#时间日期操作 [C#]C#中字符串的操作 [ASP.NET]NTKO插件使用常见问题 我对C#的认知。

    Python3 与 C# 面向对象之-继承与多态   文章汇总:https://www.cnblogs.com/dotnetcrazy/p/9160514.html 目录: 2.继承 ¶ 2.1.单继 ...

  2. Python3 与 NetCore 基础语法对比(Function专栏)

    Jupyter最新排版:https://www.cnblogs.com/dotnetcrazy/p/9175950.html 昨晚开始写大纲做demo,今天牺牲中午休息时间码文一篇,希望大家点点赞 O ...

  3. Python3 与 NetCore 基础语法对比(就当Python和C#基础的普及吧)

    Jupyter排版:https://www.cnblogs.com/dotnetcrazy/p/9102030.html 汇总系列:https://www.cnblogs.com/dunitian/p ...

  4. Python3 与 NetCore 基础语法对比(String专栏)

    汇总系列:https://www.cnblogs.com/dunitian/p/4822808.html#ai Jupyter排版:https://www.cnblogs.com/dunitian/p ...

  5. Python3 与 C# 基础语法对比(就当Python和C#基础的普及吧)

      文章汇总:https://www.cnblogs.com/dotnetcrazy/p/9160514.html 多图旧排版:https://www.cnblogs.com/dunitian/p/9 ...

  6. Python3 与 C# 基础语法对比(Function专栏)

      Code:https://github.com/lotapp/BaseCode 多图旧版:https://www.cnblogs.com/dunitian/p/9186561.html 在线编程: ...

  7. Python3 与 C# 基础语法对比(List、Tuple、Dict、Set专栏)

      Code:https://github.com/lotapp/BaseCode 多图旧版:https://www.cnblogs.com/dunitian/p/9156097.html 在线预览: ...

  8. Python3 与 C# 基础语法对比(String专栏)

      Code:https://github.com/lotapp/BaseCode 多图旧排版:https://www.cnblogs.com/dunitian/p/9119986.html 在线编程 ...

  9. python3笔记<一>基础语法

    随着AI人工智能的兴起,网络安全的普及,不论是网络安全工程师还是AI人工智能工程师,都选择了Python.(所以本菜也来开始上手Python) Python作为当下流行的脚本语言,其能力不言而喻,跨平 ...

随机推荐

  1. sed命令实现文件内容替换总结案例

    sed -i "s@AAAAA@BBBBB@g" /home/local/payment-biz-service/env/test.txt sed -i "s#htxk. ...

  2. Java常用API——String字符串运算

    一.字符串运算 String类 1.概述 String是特殊的引用数据类型,它是final类. 2.构造方法 String str = "abc"; 相当于:  char date ...

  3. Codeforces gym 101291 M (最长交替子序列)【DP】

    <题目链接> 题目大意:给你一段序列,要求你求出该序列的最长交替子序列,所谓最长交替子序列就是,这段序列的相邻三项必须是先递增再递减或者先递减再递增这样交替下去. 解题分析: 这与一道dp ...

  4. BOM 和 DOM

    目录 一.BOM 1.什么是BOM 2. 浏览器内容划分 归BOM管的: 归DOM管的: 3. BOM常见方法 二.DOM 1 什么是DOM 2. DOM常见方法 一.BOM 1.什么是BOM BOM ...

  5. js异步梳理:1.从浏览器的多进程到JS的单线程,理解JS运行机制

    大家很早就知道JS是一门单线程的语言.但是也时不时的会看到进程这个词.首先简单区分下线程和进程的概念 1. 简单理解进程 - 进程是一个工厂,工厂有它的独立资源 - 工厂之间相互独立 - 线程是工厂中 ...

  6. BZOJ-6-2460: [BeiJing2011]元素-线性基

    链接 :https://www.lydsy.com/JudgeOnline/problem.php?id=2460 思路 :线性基不唯一,所以排序 进行贪心选择,价值最大的线性基, #include& ...

  7. asp.net core 依赖注入实现全过程粗略剖析(2)

    接着 上篇 目前也算是交代清楚了相关的类.那么框架具体是如何来实例化的呢?整个的流程是怎么样的. 我们参考源码中的Test文件夹来看看: var collection = new ServiceCol ...

  8. Codeforces.1110E.Magic Stones(思路 差分)

    题目链接 听dalao说很nb,做做看(然而不小心知道题解了). \(Description\) 给定长为\(n\)的序列\(A_i\)和\(B_i\).你可以进行任意多次操作,每次操作任选一个\(i ...

  9. BZOJ.4650.[NOI2016]优秀的拆分(后缀数组 思路)

    BZOJ 洛谷 令\(st[i]\)表示以\(i\)为开头有多少个\(AA\)这样的子串,\(ed[i]\)表示以\(i\)结尾有多少个\(AA\)这样的子串.那么\(Ans=\sum_{i=1}^{ ...

  10. BZOJ.2115.[WC2011]Xor(线性基)

    题目链接 \(Description\) 给定一张无向带边权图(存在自环和重边).求一条1->n的路径,使得路径经过边的权值的Xor和最大.可重复经过点/边,且边权和计算多次. \(Soluti ...