Python3 與 NetCore 基礎語法對比(List、Tuple、Dict、Set專欄)

Jupyter最新版:http://www.javashuo.com/article/p-ftgdkctm-g.htmlhtml

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

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

今天說說List和Tuple以及Dict。POP部分還有一些如Func、IO(也能夠放OOP部分說)而後就說說面向對象吧。程序員

先吐槽一下:Python面向對象真心須要規範,否則太容易走火入魔了 -_-!!! 汗,下次再說。。。github

對比寫做真的比單寫累不少,但願你們多捧捧場 ^_^web

進入擴展https://www.cnblogs.com/dotnetcrazy/p/9155310.html#ext算法

步入正題數組


 1.列表相關:app

Python定義一個列表(列表雖然能夠存不一樣類型,通常咱們把相同類型的值存列表裏面,不一樣類型存字典裏(key,value))
info_list=[] #空列表
infos_list=["C#","JavaScript"]

遍歷和以前同樣,for 或者 while 均可以(for擴展:http://www.javashuo.com/article/p-dwdjxhac-g.htmlasync

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:

# 定義一個列表,列表雖然能夠存不一樣類型,通常咱們把相同類型的值存列表裏面,不一樣類型存字典裏(key,value)
infos_list=["C#","JavaScript"]#[]

# ###########################################################
# # 遍歷 for while
# for item in infos_list:
#     print(item)

# i=0
# while i<len(infos_list):
#     print(infos_list[i])
#     i+=1
# ###########################################################
# # 增長
# # 末尾追加
# infos_list.append("Java")
# print(infos_list)

# # 指定位置插入
# infos_list.insert(0,"Python")
# print(infos_list)

# temp_list=["test1","test2"]
# infos_list.insert(0,temp_list)
# print(infos_list)

# # 添加一個列表
# infos_list2=["張三",21]#python裏面的列表相似於List<object>
# infos_list.extend(infos_list2)
# print(infos_list)

# # help(infos_list.extend)#能夠查看etend方法描述
# ###########################################################
# # 刪除
# # pop()刪除最後一個元素,返回刪掉的元素
# # pop(index) 刪除指定下標元素
# print(infos_list.pop())
# print(infos_list)
# print(infos_list.pop(0))
# # print(infos_list.pop(10)) #不存在就報錯
# print(infos_list)

# # remove("")刪除指定元素
# infos_list.remove("張三")
# # infos_list.remove("dnt") #不存在就報錯
# print(infos_list)

# # del xxx[index] 刪除指定下標元素
# del infos_list[1]
# print(infos_list)
# # del infos_list[10] #不存在就報錯

# # del infos_list #刪除集合(集合再訪問就不存在了)
# ###########################################################
# # 修改 xxx[index]=xx
# # 注意:通常不推薦在for循環裏面修改
# print(infos_list2)
# infos_list2[1]="PHP" #只有下標修改一種方式
# # infos_list2[3]="GO" #不存在則異常
# print(infos_list2)

# # 想按值修改須要先查下標再修改
# infos_list2.index("張三")
# infos_list2[0]="GO"
# print(infos_list2)
# # infos_list2.index("dnt")#不存在則異常

# # 知識面拓展: https://www.zhihu.com/question/49098374
# # 爲何python中不建議在for循環中修改列表?
# # 因爲在遍歷的過程當中,刪除了其中一個元素,致使後面的元素總體前移,致使有個元素成了漏網之魚。
# # 一樣的,在遍歷過程當中,使用插入操做,也會致使相似的錯誤。這也就是問題裏說的沒法「跟蹤」元素。
# # 若是使用while,則能夠在面對這樣狀況的時候靈活應對。

###########################################################
# # 查詢 in, not in, index, count
# # # for擴展:https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse

# names_list=["張三","李四","王二麻子"]

# # #張三在列表中執行操做
# if "張三" in names_list:
#     names_list.remove("張三")
# print(names_list)

# # #查看"大舅子"不在列表中執行操做
# if "大舅子" not in names_list:
#     names_list.append("大舅子")
# print(names_list)

# # #查詢王二麻子的索引
# print(names_list.index("王二麻子"))

# print(names_list.count("大舅子")) 
# print(names_list.count("逆天")) 
###########################################################
# # 排序(sort, reverse 逆置)
# num_list=[1,3,5,88,7]

# #倒序
# num_list.reverse()
# print(num_list)

# # 從小到大排序
# num_list.sort()
# print(num_list)

# # 從大到小
# num_list.sort(reverse=True)
# print(num_list)
# # ###########################################################

# # #列表嵌套(列表也是能夠嵌套的)
# num_list2=[33,44,22]
# num_list.append(num_list2)
# print(num_list)
# # for item in num_list:
# #     print(item,end="")

# print(num_list[5])
# print(num_list[5][1])
# # ###########################################################

# # # 引入Null==>None
# # a=[1,2,3,4]
# # b=[5,6]
# # a=a.append(b)#a.append(b)沒有返回值
# # print(a)#None
View Code

Python Tuple:

# 只能查詢,其餘操做和列表差很少(不可變)
test_tuple=("萌萌噠",1,3,5,"加息","加息")


# count index
print(test_tuple.count("加息"))
print(test_tuple.index("萌萌噠"))#沒有find方法
# 注意是左閉右開區間==>[1,4)
# print(test_tuple.index("加息", 1, 4))#查不到報錯:ValueError: tuple.index(x): x not in tuple

#下標取
print(test_tuple[0])

# 遍歷
for item in test_tuple:
    print(item)

i=0
while i<len(test_tuple):
    print(test_tuple[i])
    i+=1

# 擴展:
test_tuple1=(1,) #(1)就不是元祖了
test_tuple2=(2)
print(type(test_tuple1))
print(type(test_tuple2))

# # ==============================================
# 擴展:(後面講字典遍歷的時候會再提一下的)
a=(1,2)
b=a#把a的引用給b
#a裏面兩個值,直接給左邊兩個變量賦值了(有點像拆包了)
c,d=a #不是把a分別賦值給c和d,等價於:c=a[0] d=a[1]

print(a)
print(b)
print(c)
print(d)
View Code

Python Dict:

infos_dict={"name":"dnt","web":"dkill.net"}

# # 遍歷
# for item in infos_dict.keys():
#     print(item)

# #注意,若是你直接對infos遍歷,其實只是遍歷keys
# for item in infos_dict:
#     print(item)

# for item in infos_dict.values():
#     print(item)

# for item in infos_dict.items():
#     print("Key:%s,Value:%s"%(item[0],item[1]))
# #每一次至關於取一個元組,那能夠用以前講的例子來簡化了:c,d=a #等價於:c=a[0] d=a[1]
# for k,v in infos_dict.items():
#     print("Key:%s,Value:%s"%(k,v))

# # 增長 修改 (有就修改,沒就添加)
# # 添加
# infos_dict["wechat"]="lll"
# print(infos_dict)

# # 修改
# infos_dict["wechat"]="dotnetcrazy"
# print(infos_dict)

# # 刪除
# del infos_dict["name"]
# del infos_dict["dog"] #不存在就報錯
# print(infos_dict)

# #清空字典內容
# infos_dict.clear()
# print(infos_dict)

# # 刪除字典
# del infos_dict

# 查詢
infos_dict["name"]
# infos_dict["mmd"] #查不到就異常


infos_dict.get("name")
infos_dict.get("mmd")#查不到不會異常

# 查看幫助
# help(infos_dict)
len(infos_dict) #有幾對key,value 
# infos_dict.has_key("name") #這個是python2裏面的
View Code

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

NetCore List:

// using System;
// using System.Collections.Generic;
// using System.Linq;

// namespace aibaseConsole
// {
//     public static class Program
//     {
//         private static void Main()
//         {
//             #region List
//             //# 定義一個列表
//             // # infos_list=["C#","JavaScript"]#[]
//             var infos_list = new List<object>() { "C#", "JavaScript" };
//             // var infos_list2 = new List<object>() { "張三", 21 };
//             // // # ###########################################################
//             // // # # 遍歷 for while
//             // // # for item in infos_list:
//             // // #     print(item)
//             // foreach (var item in infos_list)
//             // {
//             //     System.Console.WriteLine(item);
//             // }
//             // for (int i = 0; i < infos_list.Count; i++)
//             // {
//             //     System.Console.WriteLine(infos_list[i]);
//             // }
//             // // # i=0
//             // // # while i<len(infos_list):
//             // // #     print(infos_list[i])
//             // // #     i+=1
//             // int j=0;
//             // while(j<infos_list.Count){
//             //    Console.WriteLine(infos_list[j++]);
//             // }
//             // // # ###########################################################
//             // // # # 增長
//             // // # # 末尾追加
//             // // # infos_list.append("Java")
//             // // # print(infos_list)
//             // DivPrintList(infos_list);

//             // infos_list.Add("Java");
//             // DivPrintList(infos_list);
//             // // # # 指定位置插入
//             // // # infos_list.insert(0,"Python")
//             // // # print(infos_list)
//             // infos_list.Insert(0,"Python");
//             // DivPrintList(infos_list);
//             // // # # 添加一個列表
//             // // # infos_list2=["張三",21]#python裏面的列表相似於List<object>            
//             // // # infos_list.extend(infos_list2)
//             // // # print(infos_list)
//             // infos_list.AddRange(infos_list2);
//             // DivPrintList(infos_list);
//             // /*C#有insertRange方法 */
//             // DivPrintList(infos_list2,"List2原來的列表:");
//             // infos_list2.InsertRange(0,infos_list);
//             // DivPrintList(infos_list2,"List2變化後列表:");
//             // // # # help(infos_list.extend)#能夠查看etend方法描述
//             // // # ###########################################################
//             // // # # 刪除
//             // // # # pop()刪除最後一個元素,返回刪掉的元素
//             // // # # pop(index) 刪除指定下標元素
//             // // # print(infos_list.pop())
//             // // # print(infos_list)
//             // // # print(infos_list.pop(1))
//             // // # # print(infos_list.pop(10)) #不存在就報錯
//             // // # print(infos_list)

//             // // # # remove("")刪除指定元素
//             // // # infos_list.remove("張三")
//             // // # # infos_list.remove("dnt") #不存在就報錯
//             // // # print(infos_list)

//             // // # # del xxx[index] 刪除指定下標元素
//             // // # del infos_list[1]
//             // // # print(infos_list)
//             // // # # del infos_list[10] #不存在就報錯

//             // // # del infos_list #刪除集合(集合再訪問就不存在了)

//             // DivPrintList(infos_list);
//             // infos_list.RemoveAt(1);
//             // // infos_list.RemoveAt(10);//不存在則報錯
//             // // infos_list.RemoveRange(0,1); //能夠移除多個
//             // DivPrintList(infos_list);
//             // infos_list.Remove("我家在東北嗎?"); //移除指定item,不存在不會報錯
//             // DivPrintList(infos_list,"清空前:");
//             // infos_list.Clear();//清空列表
//             // DivPrintList(infos_list,"清空後:");

//             // // # ###########################################################
//             // // # # 修改 xxx[index]=xx
//             // // # # 注意:通常不推薦在for循環裏面修改
//             // // # print(infos_list2)
//             // // # infos_list2[1]="PHP" #只有下標修改一種方式
//             // // # # infos_list2[3]="GO" #不存在則異常
//             // // # print(infos_list2)
//             // DivPrintList(infos_list2);
//             // infos_list2[1] = "PHP";
//             // // infos_list2[3]="GO"; //不存在則異常
//             // DivPrintList(infos_list2);
//             // // # # 想按值修改須要先查下標再修改
//             // // # infos_list2.index("張三")
//             // // # infos_list2[0]="GO"
//             // // # print(infos_list2)
//             // // # # infos_list2.index("dnt")#不存在則異常
//             // int index = infos_list2.IndexOf("張三");
//             // infos_list2[index] = "GO";
//             // DivPrintList(infos_list2);
//             // infos_list2.IndexOf("dnt");//不存在返回-1

//             // // ###########################################################
//             // // # 查詢 in, not in, index, count
//             // // # # for擴展:https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse
//             // // # names_list=["張三","李四","王二麻子"]
//             // var names_list=new List<string>(){"張三","李四","王二麻子"};
//             // // Console.WriteLine(names_list.Find(i=>i=="張三"));
//             // // Console.WriteLine(names_list.FirstOrDefault(i=>i=="張三"));
//             // Console.WriteLine(names_list.Exists(i=>i=="張三"));
//             // System.Console.WriteLine(names_list.Contains("張三"));
//             // // # #張三在列表中執行操做
//             // // # if "張三" in names_list:
//             // // #     names_list.remove("張三")
//             // // # else:
//             // // #     print(names_list)

//             // // # #查看"大舅子"不在列表中執行操做
//             // // # if "大舅子" not in names_list:
//             // // #     names_list.append("大舅子")
//             // // # else:
//             // // #     print(names_list)

//             // // # #查詢王二麻子的索引
//             // // # print(names_list.index("王二麻子"))
//             // // names_list.IndexOf("王二麻子");

//             // // # print(names_list.count("大舅子")) 
//             // // # print(names_list.count("逆天")) 
//             // // Console.WriteLine(names_list.Count);

//             // // ###########################################################
//             // // # # 排序(sort, reverse 逆置)
//             // // # num_list=[1,3,5,88,7]
//             // var num_list = new List<object>() { 1, 3, 5, 88, 7 };

//             // // # #倒序
//             // // # num_list.reverse()
//             // // # print(num_list)
//             // num_list.Reverse();
//             // DivPrintList(num_list);
//             // // # # 從小到大排序
//             // // # num_list.sort()
//             // // # print(num_list)
//             // num_list.Sort();
//             // DivPrintList(num_list);

//             // // # # 從大到小
//             // // # num_list.sort(reverse=True)
//             // // # print(num_list)
//             // num_list.Sort();
//             // num_list.Reverse();
//             // DivPrintList(num_list);

//             // // # ###########################################################

//             // // # #列表嵌套(列表也是能夠嵌套的)
//             // // # num_list2=[33,44,22]
//             // // # num_list.append(num_list2)
//             // // # print(num_list)
//             // var num_list2 = new List<object>() { 33, 44, 22,new List<object>(){11,55,77} };
//             // DivPrintList(num_list2);//能夠定義多維數組來支持 num_list2[i][j]
//             // // # for item in num_list:
//             // // #     print(item)
//             // // # ###########################################################

//             // // # # 引入Null==>None
//             // // # a=[1,2,3,4]
//             // // # b=[5,6]
//             // // # a=a.append(b)#a.append(b)沒有返回值
//             // // # print(a)#None
//             #endregion

//             // Console.Read();
//         }

//         private static void DivPrintList(List<object> list, string say = "")
//         {
//             Console.WriteLine($"\n{say}");
//             foreach (var item in list)
//             {
//                 System.Console.Write($"{item} ");
//             }
//         }
//     }
// }
View Code

NetCore Tuple:

// using System;

// namespace aibaseConsole
// {
//     public static class Program
//     {
//         private static void Main()
//         {
//             #region Tuple
//             // C#中元組主要是方便程序員,不用天然能夠.
//             // 元祖系:https://msdn.microsoft.com/zh-cn/library/system.tuple.aspx
//             // 值元組:https://msdn.microsoft.com/zh-cn/library/system.valuetuple.aspx
//             // 好比:當你返回多個值是否還用ref out 或者返回一個list之類的?
//             // 這些都須要先定義,比較麻煩.元祖在一些場景用的比較多 eg:        

//             // 初始化
//             // var test_tuple = ("萌萌噠", 1, 3, 5, "加息", "加息"); //這種方式就是valueTuple了

//             // test_tuple.Item1 = "ddd";//能夠修改值

//             // test_tuple.GetType();
//             // test_tuple.itemxxx //獲取值只能經過itemxxx

//             var result = GetCityAndTel();  //支持async/await模式
//             var city = result.city;
//             var tel = result.tel;
//             // 拆包方式:
//             var (city1, tel1) = GetCityAndTel();
            
//             #endregion
//             // Console.Read();
//         }
//         // public static (string city, string tel) GetCityAndTel()
//         // {
//         //     return ("北京", "110");
//         // }
//         // 簡化寫法
//         public static (string city, string tel) GetCityAndTel() => ("北京", "110");
//     }
// }
View Code

NetCore Dict:

using System;
using System.Collections.Generic;

namespace aibaseConsole
{
    public static class Program
    {
        private static void Main()
        {
            #region Dict
            // infos_dict={"name":"dnt","web":"dkill.net"}
            // # # 遍歷
            // # for item in infos_dict.keys():
            // #     print(item)
            // # for item in infos_dict.values():
            // #     print(item)
            // # for item in infos_dict.items():
            // #     print("Key:%s,Value:%s"%(item[0],item[1]))
            // # #每一次至關於取一個元組,那能夠用以前講的例子來簡化了:c,d=a #等價於:c=a[0] d=a[1]
            // # for k,v in infos_dict.items():
            // #     print("Key:%s,Value:%s"%(k,v))
            var infos_dict = new Dictionary<string, object>{
                {"name","dnt"},
                {"web","dkill.net"}
            };
            // foreach (var item in infos_dict.Keys)
            // {
            //     System.Console.WriteLine(item);
            // }
            // foreach (var item in infos_dict.Values)
            // {
            //     System.Console.WriteLine(item);
            // }
            // foreach (KeyValuePair<string, object> kv in infos_dict)
            // {
            //     //    System.Console.WriteLine("Key:%s,Value:%s",(kv.Key,kv.Value));
            //     System.Console.WriteLine($"Key:{kv.Key},Value:{kv.Value}");
            // }

            // // # # 增長 修改 (有就修改,沒就添加)
            // // # # 添加
            // // # infos_dict["wechat"]="lll"
            // // # print(infos_dict)
            // infos_dict.Add("wechat", "lll");
            // infos_dict["wechat1"] = "lll";
            // // # # 修改
            // // # infos_dict["wechat"]="dotnetcrazy"
            // // # print(infos_dict)
            // infos_dict["wechat"] = "dotnetcrazy";

            // // # # 刪除
            // // # del infos_dict["name"]
            // // # del infos_dict["dog"] #不存在就報錯
            // // # print(infos_dict)
            // infos_dict.Remove("name");
            // infos_dict.Remove("dog");
            // // # #清空列表內容
            // // # infos_dict.clear()
            // // # print(infos_dict)
            // infos_dict.Clear();
            // // # # 刪除列表
            // // # del infos_dict

            // # 查詢
            // infos_dict["name"]
            // infos_dict["mmd"] #查不到就異常            

            // infos_dict.get("name")
            // infos_dict.get("mmd")#查不到不會異常
            Console.WriteLine(infos_dict["name"]);
            // Console.WriteLine(infos_dict["mmd"]); //#查不到就異常
            // 先看看有沒有 ContainsKey(key),看值就 ContainsValue(value)
            if (infos_dict.ContainsKey("mmd")) Console.WriteLine(infos_dict["mmd"]);

            // # 查看幫助
            // help(infos_dict)
            // len(infos_dict) #有幾對key,value
            Console.WriteLine(infos_dict.Count);

            #endregion

            // Console.Read();
        }
    }
}
View Code
相關文章
相關標籤/搜索