Python 3 vs. C # Basic Grammar (Basic Knowledge Field): https://www.cnblogs.com/dotnetcrazy/p/9102030.html
Python 3 versus C # Basic Grammar (String column) https://www.cnblogs.com/dotnetcrazy/p/9114691.html
Today I'll talk about List and Tuple and Dict. There are also some POP parts such as Func, IO (or OOP) and then object-oriented.
First Tucao: Python object oriented really need to be standardized, otherwise it's too easy to get caught up in the fire - - - -!!! Khan, next time.
Contrastive writing is really much more tiring than single writing. I hope you will enjoy it more.
Step into the main topic:
1. List related:
Traversal is the same as before, for or while (for extension: https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse)
NetCore: var infos_list = new List<object>() { "C#", "JavaScript" };
Traversal can be done using foreach, for, while.
Add the Python list:
Python actually inserts a list at a specified location. C# inserts elements one by one.
NetCore: Add, AddRange, Insert, InsertRange (different from Python Insert List)
Python list deletion series:
Once again.
NetCore: Remove the specified index: infos_list.RemoveAt(1); Remove the specified value: infos_list.Remove(item); Clear list: infos_list.Clear();
Python modification: (can only be modified by index)
NetCore: Basically the same as Python
Python Query Series: in, not in, index, count
NetCore: IndexOf , Count
Find Contains. Look at the rest first. I'll talk about it later.
Python sort
List nesting, get the way with subscripts: num_list[5][1]
NetCore: var num_list2 = new List<object>() { 33, 44, 22,new List<object>(){11,55,77} };
Instead of subscribing like python, you can define multidimensional arrays to support num_list2[i][j] (PS, which is actually not very useful, and later all lists are nested with Dict, similar to Json)
2.Tuple tuples
Let's start with NetCore this time. (Value Tuple is used more against the sky, and this is the case below.)





Python traversal correlation:
# is equivalent to taking a tuple at a time, which can be simplified by the previous example: c,d=a # is equivalent to: c=a[0] d=a[1]
NetCore: Similar to Python
Python additions and deletions series:
Add, modify: infos_dict["wechat"]="dotnetcrazy"# modify if you have, add if you haven't
Delete series:
NetCore:
Python Query Series: Recommendation: infos_dict.get("mmd")# Not abnormal if not found
NetCore: infos_dict["name"] can avoid exceptions through ContainsKey(key). ContainsValue(value)
Appendix Code:
Python List:
# Define a list, although the list can store different types, we generally store the same type of value list, different types of Dictionary ( key,value) infos_list=["C#","JavaScript"]#[] # ########################################################### # # ergodic for while # for item in infos_list: # print(item) # i=0 # while i<len(infos_list): # print(infos_list[i]) # i+=1 # ########################################################### # # increase # # Append at the end # infos_list.append("Java") # print(infos_list) # # Specified position insertion # infos_list.insert(0,"Python") # print(infos_list) # temp_list=["test1","test2"] # infos_list.insert(0,temp_list) # print(infos_list) # # Add a list # infos_list2=["Zhang San",21]#python The list inside is similar to List<object> # infos_list.extend(infos_list2) # print(infos_list) # # help(infos_list.extend)#You can check it. etend Method description # ########################################################### # # delete # # pop()Delete the last element and return the deleted element # # pop(index) Delete the specified subscript element # print(infos_list.pop()) # print(infos_list) # print(infos_list.pop(0)) # # print(infos_list.pop(10)) #Reporting an error if it does not exist # print(infos_list) # # remove("")Delete the specified element # infos_list.remove("Zhang San") # # infos_list.remove("dnt") #Reporting an error if it does not exist # print(infos_list) # # del xxx[index] Delete the specified subscript element # del infos_list[1] # print(infos_list) # # del infos_list[10] #Reporting an error if it does not exist # # del infos_list #Delete collections (collections are no longer accessible) # ########################################################### # # modify xxx[index]=xx # # Note: Generally not recommended in for Modification in the loop # print(infos_list2) # infos_list2[1]="PHP" #Only Subscripts Modify One Way # # infos_list2[3]="GO" #If nonexistence is abnormal # print(infos_list2) # # If you want to modify by value, you need to check the subscription before modifying it. # infos_list2.index("Zhang San") # infos_list2[0]="GO" # print(infos_list2) # # infos_list2.index("dnt")#If nonexistence is abnormal # # Knowledge expansion: https://www.zhihu.com/question/49098374 # # Why python Not recommended in for Modify the list in the loop? # # As one of the elements is deleted during the traversal process, the latter elements move forward as a whole, resulting in one element becoming a fish out of the net. # # Similarly, the use of insert operations during traversal can lead to similar errors. That's what the problem says is that you can't "track" elements. # # If you use while, you can be flexible in dealing with such situations. ########################################################### # # query in, not in, index, count # # # for Extension: https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse # names_list=["Zhang San","Li Si","May you stay forever young"] # # #Zhang San performs operations in the list # if "Zhang San" in names_list: # names_list.remove("Zhang San") # print(names_list) # # #See"Brother-in-law"Do not perform operations in the list # if "Brother-in-law" not in names_list: # names_list.append("Brother-in-law") # print(names_list) # # #Query the index of Ermazi Wang # print(names_list.index("May you stay forever young")) # print(names_list.count("Brother-in-law")) # print(names_list.count("Adverse weather")) ########################################################### # # sort(sort, reverse Inversion) # num_list=[1,3,5,88,7] # #Reverse order # num_list.reverse() # print(num_list) # # Ranking from small to large # num_list.sort() # print(num_list) # # From big to small # num_list.sort(reverse=True) # print(num_list) # # ########################################################### # # #List nesting(Lists can also be nested) # 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]) # # ########################################################### # # # Introduce Null==>None # # a=[1,2,3,4] # # b=[5,6] # # a=a.append(b)#a.append(b)no return value # # print(a)#None
Python Tuple:
# Only queries, other operations and lists are similar (immutable) test_tuple=("Adorable",1,3,5,"Increase interest","Increase interest") # count index print(test_tuple.count("Increase interest")) print(test_tuple.index("Adorable"))#No, find Method # Note that the left closed right open interval==>[1,4) # print(test_tuple.index("Increase interest", 1, 4))#No error was detected: ValueError: tuple.index(x): x not in tuple #Subscript selection print(test_tuple[0]) # ergodic for item in test_tuple: print(item) i=0 while i<len(test_tuple): print(test_tuple[i]) i+=1 # Extension: test_tuple1=(1,) #(1)It's not the ancestor of Yuan Dynasty. test_tuple2=(2) print(type(test_tuple1)) print(type(test_tuple2)) # # ============================================== # Extension: (I will mention it later when I talk about dictionary traversal) a=(1,2) b=a#hold a Quote to b #a There are two values in it.,Appoint two variables directly to the left (a bit like unpacking) c,d=a #Not put a Assignment to c and d,Equivalent to: c=a[0] d=a[1] print(a) print(b) print(c) print(d)
Python Dict:
infos_dict={"name":"dnt","web":"dkill.net"} # # ergodic # for item in infos_dict.keys(): # print(item) # #Note that if you are right infos Traveling, in fact, is just traversing 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])) # #Each time it is equivalent to taking a tuple, it can be simplified by the example mentioned earlier. c,d=a #Equivalent to: c=a[0] d=a[1] # for k,v in infos_dict.items(): # print("Key:%s,Value:%s"%(k,v)) # # Additional modifications (Modify if you have, add if you haven't.) # # Add to # infos_dict["wechat"]="lll" # print(infos_dict) # # modify # infos_dict["wechat"]="dotnetcrazy" # print(infos_dict) # # delete # del infos_dict["name"] # del infos_dict["dog"] #Reporting an error if it does not exist # print(infos_dict) # #Empty the dictionary content # infos_dict.clear() # print(infos_dict) # # Delete dictionary # del infos_dict # query infos_dict["name"] # infos_dict["mmd"] #Anomaly if you can't find it infos_dict.get("name") infos_dict.get("mmd")#Not abnormal if not found # view help # help(infos_dict) len(infos_dict) #How many pairs? key,value # infos_dict.has_key("name") #This is in Python 2.
NetCore List:
// using System; // using System.Collections.Generic; // using System.Linq; // namespace aibaseConsole // { // public static class Program // { // private static void Main() // { // #region List // //# Define a list // // # infos_list=["C#","JavaScript"]#[] // var infos_list = new List<object>() { "C#", "JavaScript" }; // // var infos_list2 = new List<object>() { "Zhang San", 21 }; // // // # ########################################################### // // // # # Traversing 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++]); // // } // // // # ########################################################### // // // # # increase // // // # # Append at the end // // // # infos_list.append("Java") // // // # print(infos_list) // // DivPrintList(infos_list); // // infos_list.Add("Java"); // // DivPrintList(infos_list); // // // # # Specified position insertion // // // # infos_list.insert(0,"Python") // // // # print(infos_list) // // infos_list.Insert(0,"Python"); // // DivPrintList(infos_list); // // // # # Add a list // // // # infos_list2=["Zhang San",21]#The list in python is similar to List < object >. // // // # infos_list.extend(infos_list2) // // // # print(infos_list) // // infos_list.AddRange(infos_list2); // // DivPrintList(infos_list); // // /*C#insertRange method */ // // DivPrintList(infos_list2,"List2 The original list:"); // // infos_list2.InsertRange(0,infos_list); // // DivPrintList(infos_list2,"List2 List after change:"); // // // # # help(infos_list.extend)#You can view the etend method description // // // # ########################################################### // // // # # delete // // // # # pop() deletes the last element and returns the deleted element // // // # # pop(index) deletes the specified subscript element // // // # print(infos_list.pop()) // // // # print(infos_list) // // // # print(infos_list.pop(1)) // // // # # print(infos_list.pop(10)) #Reporting an error if it does not exist // // // # print(infos_list) // // // # # remove("") Deletes the specified element // // // # infos_list.remove("Zhang San") // // // # # infos_list.remove("dnt") #Reporting an error if it does not exist // // // # print(infos_list) // // // # # del xxx[index] Deletes the specified subscript element // // // # del infos_list[1] // // // # print(infos_list) // // // # # del infos_list[10] #Reporting an error if it does not exist // // // # del infos_list #Delete collections (collections are no longer accessible) // // DivPrintList(infos_list); // // infos_list.RemoveAt(1); // // // infos_list.RemoveAt(10);//Error reporting if nonexistence // // // infos_list.RemoveRange(0,1); //Multiple can be removed // // DivPrintList(infos_list); // // infos_list.Remove("Is my home in the northeast?"); //Remove specified item,There is no error-free // // DivPrintList(infos_list,"Before emptying:"); // // infos_list.Clear();//clear list // // DivPrintList(infos_list,"After emptying:"); // // // # ########################################################### // // // # # Modify xxx[index]=xx // // // # # Note: It is generally not recommended to modify the for loop // // // # print(infos_list2) // // // # infos_list2[1]="PHP" #Only Subscripts Modify One Way // // // # # infos_list2[3]="GO" #If nonexistence is abnormal // // // # print(infos_list2) // // DivPrintList(infos_list2); // // infos_list2[1] = "PHP"; // // // infos_list2[3]="GO"; //If nonexistence is abnormal // // DivPrintList(infos_list2); // // // # # If you want to modify by value, you need to check the subscription before modifying it. // // // # infos_list2.index("Zhang San") // // // # infos_list2[0]="GO" // // // # print(infos_list2) // // // # # infos_list2.index("dnt")#If nonexistence is abnormal // // int index = infos_list2.IndexOf("Zhang San"); // // infos_list2[index] = "GO"; // // DivPrintList(infos_list2); // // infos_list2.IndexOf("dnt");//No return-1 // // // ########################################################### // // // # Query in, not in, index, count // // // # # for Extension:https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse // // // # names_list=["Zhang San", "Li Si", "Wang Ermazi"] // // var names_list=new List<string>(){"Zhang San","Li Si","May you stay forever young"}; // // // Console.WriteLine(names_list.Find(i=>i=="Zhang San")); // // // Console.WriteLine(names_list.FirstOrDefault(i=>i=="Zhang San")); // // Console.WriteLine(names_list.Exists(i=>i=="Zhang San")); // // System.Console.WriteLine(names_list.Contains("Zhang San")); // // // # #Zhang San performs operations in the list // // // # if "Zhang San" in names_list: // // // # names_list.remove("Zhang San") // // // # else: // // // # print(names_list) // // // # #Check that Uncle does not perform operations in the list // // // # if "Uncle" is not in names_list: // // // # names_list.append("uncle") // // // # else: // // // # print(names_list) // // // # #Query the index of Ermazi Wang // // // # print(names_list.index("Wang Ermazi") // // // names_list.IndexOf("May you stay forever young"); // // // # print(names_list.count("uncle") // // // # print(names_list.count("against the sky") // // // Console.WriteLine(names_list.Count); // // // ########################################################### // // // # # Sort (reverse inversion) // // // # num_list=[1,3,5,88,7] // // var num_list = new List<object>() { 1, 3, 5, 88, 7 }; // // // # #Reverse order // // // # num_list.reverse() // // // # print(num_list) // // num_list.Reverse(); // // DivPrintList(num_list); // // // # # Ranking from small to large // // // # num_list.sort() // // // # print(num_list) // // num_list.Sort(); // // DivPrintList(num_list); // // // # # From big to small // // // # num_list.sort(reverse=True) // // // # print(num_list) // // num_list.Sort(); // // num_list.Reverse(); // // DivPrintList(num_list); // // // # ########################################################### // // // # #List nesting (lists can also be nested) // // // # 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);//Multidimensional arrays can be defined to support num_list2[i][j] // // // # for item in num_list: // // // # print(item) // // // # ########################################################### // // // # # Introduce Null==>None // // // # a=[1,2,3,4] // // // # b=[5,6] // // // # a=a.append(b)#a.append(b) has no return value // // // # 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} "); // } // } // } // }
NetCore Tuple:
// using System; // namespace aibaseConsole // { // public static class Program // { // private static void Main() // { // #region Tuple // // C#The tuple is mainly convenient for programmers, not natural. // // The Yuan Dynasty:https://msdn.microsoft.com/zh-cn/library/system.tuple.aspx // // Value tuple:https://msdn.microsoft.com/zh-cn/library/system.valuetuple.aspx // // such as:When you return multiple values, do you still use them? ref out Or return one list And so on.? // // All of these need to be defined first.,More trouble.Yuan Zu used more in some scenes. eg: // // Initialization // // var test_tuple = ("Adorable", 1, 3, 5, "Increase interest", "Increase interest"); //This way is valueTuple 了 // // test_tuple.Item1 = "ddd";//Values can be modified // // test_tuple.GetType(); // // test_tuple.itemxxx //Getting values can only pass through itemxxx // var result = GetCityAndTel(); //Support async/await Pattern // var city = result.city; // var tel = result.tel; // // Unpacking mode: // var (city1, tel1) = GetCityAndTel(); // #endregion // // Console.Read(); // } // // public static (string city, string tel) GetCityAndTel() // // { // // return ("Beijing", "110"); // // } // // Simplified writing // public static (string city, string tel) GetCityAndTel() => ("Beijing", "110"); // } // }
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"} // # # ergodic // # 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])) // # #Each time it is equivalent to taking a tuple, it can be simplified by the example mentioned earlier. c,d=a #Equivalent to: 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}"); // } // // # # Add modifications (if you have, add) // // # # Add to // // # infos_dict["wechat"]="lll" // // # print(infos_dict) // infos_dict.Add("wechat", "lll"); // infos_dict["wechat1"] = "lll"; // // # # modify // // # infos_dict["wechat"]="dotnetcrazy" // // # print(infos_dict) // infos_dict["wechat"] = "dotnetcrazy"; // // # # delete // // # del infos_dict["name"] // // # del infos_dict["dog"] #Reporting an error if it does not exist // // # print(infos_dict) // infos_dict.Remove("name"); // infos_dict.Remove("dog"); // // # #Clear the list content // // # infos_dict.clear() // // # print(infos_dict) // infos_dict.Clear(); // // # # Delete list // // # del infos_dict // # query // infos_dict["name"] // infos_dict["mmd"] #Anomaly if you can't find it // infos_dict.get("name") // infos_dict.get("mmd")#Not abnormal if not found Console.WriteLine(infos_dict["name"]); // Console.WriteLine(infos_dict["mmd"]); //#Anomaly if you can't find it // Let's see if there are any. ContainsKey(key),Look at the value. ContainsValue(value) if (infos_dict.ContainsKey("mmd")) Console.WriteLine(infos_dict["mmd"]); // # view help // help(infos_dict) // len(infos_dict) #There are several pairs of keys, values Console.WriteLine(infos_dict.Count); #endregion // Console.Read(); } } }