<> preface
python Middle list , Yuanzu , How can the dictionary be confused , look around , I'll buy it for you iphone12!
<> One , list

<>1. What is a list

The powerful thing about arrays in other languages is that the elements in the list can be of different types , How strong is it , Code up .
my_list = [1, "xin", (1, ), {"yi"}, ["nice"], {"hello": "world"}]
No language can be so unscrupulous , A list contains int, str, Yuanzu , list , Dictionaries …

<>2. Basic operation of list

<1> Add elements (" increase "append, extend, insert)
# Empty list my_list = list() # append Add an element to the end of the list my_list.append(" Loronoya ") # [" Loronoya "]
my_list.append(" Sauron ") # [" Loronoya ", " Sauron "] # extend Add elements from another collection one by one to the list b = [" Sandaoliu ",
" Profound meaning "] my_list.extend(b) # [" Loronoya ", " Sauron ", " Sandaoliu ", " Profound meaning "] my_list + b # There's a way to be lazy [
" Loronoya ", " Sauron ", " Sandaoliu ", " Profound meaning "] # insert insert(index, object) At the specified location index Insert element before object
my_list.insert(0, " A handsome stroke ") # [" A handsome stroke ", " Loronoya ", " Sauron ", " Sandaoliu ", " Profound meaning "]
<2> Modify element (“ change ”)
# Modifying elements can only be modified by subscript my_list = [1, 2, 3, 4] my_list[0] = " modify " # [" modify ", 2, 3, 4]
<3> Find element (" check "in, not in, index, count)
# in, not in # in( existence ), If it exists, the result is true, Otherwise false # not
in( non-existent ), If it doesn't exist, the result is true, otherwise false my_list = [1, 2, 3] "1" in my_list # False "1"
not in my_list # True# index, count # index and count Same usage as in string my_list.index(1, 0,
2) # The first parameter is the element to look for , The second and third is the search interval , Notice that it's left closed and right open , Returns the index of the element to find my_list.count(1) # Returns the number of search elements
1
<4> Delete element (" Delete "del, pop, remove)
# del: Delete according to the subscript del my_list[0] # [1, 2] # pop: The last element is deleted by default my_list.pop(1) # [1] #
remove: Delete based on the value of the element ( Note that only existing elements can be deleted ) my_list.remove(1) # []
<5> sort (sort, sorted, reverse)
# sort The method is to list Rearrange in a specific order , The default is from small to large , parameter reverse=True Can be changed to reverse order , From big to small . my_list = [1, 4, 2, 3
] my_list.reverse() # [3, 2, 4, 1] my_list.sort() # [1, 2, 3, 4] my_list.sort(
reverse=True) # [4, 3, 2, 1] # sorted Yes, keep the original list order , It is also the default from small to large new_my_list = sorted(
my_list) # my_list and new_my_list It's completely independent print(my_list) # [1, 4, 2, 3] print(
new_my_list) # [1, 2, 3, 4] print(sorted(my_list, reverse=True)) # [4, 3, 2, 1]
<>3. List nesting
# python The nested list is a multidimensional array in other languages my_list = [ [' Peking University ',' Tsinghua University '], [' Nankai University ',' Tianjin University ',
' Tianjin Normal University '], [' Shandong University ',' Ocean University of China '] ]
<>4. List derivation
# The so-called list derivation , This refers to lightweight loop creation list my_list = [x for x in rang(5)] print(my_list) # [0, 1,
2, 3, 4] # In fact, it can be seen from grammar , It is to add each value of the traversal to the list # Can be used during the loop if my_list = [x for x in
range(1, 10) if x % 2 == 0] print(my_list) # [2, 4, 6, 8] # There can be more than one for loop my_list =
[(x, y) for x in range(1, 3) for y in range(3)] print(my_list) # [(1, 0), (1, 1)
, (1, 2), (2, 0), (2, 1), (2, 2)]
<> Two , Yuanzu

<>python Tuples of are similar to lists , The difference is that elements of tuples cannot be modified . Tuples using parentheses , Use square brackets for lists .

<1> Define tuple
# There is only one element , The element should be followed by a comma my_tuple = ("a", ) # Multiple elements my_tuple = ("a", "b")
<2> Yuanzu can't modify it ( Why can't we change it later )

<3>count,index, Same usage as in strings and lists

<> Three , Dictionaries

<>1. A dictionary is a key value pair , It can also be understood as being used now json namely python Dictionary in ( Remember to use double quotes )
my_dict = { "name": " Sauron " "sex": " male " }
<>2. Common operation of dictionary

<1> Access values by key
name = my_dict["name"] # But when the key does not exist, an error is reported , Null pointer exception name = my_dict.get("age") #
There is no return None name= my_dict.get("age", 10) # There is no return default value name = 10 if my_dict.get(
"age") is None else my_dict.get("age") # This is the way to take the value above
<2> modify , Add elements
# Key presence is modification my_dict["name"] = " Monkey D Luffy " # {"name": " Monkey D Luffy ", "sex": " male "} # Key does not exist, is added my_dict[
"age"] = 18 # {"name": " Monkey D Luffy ", "sex": " male ", "age": 18}
<3> Delete element
# del Delete the specified element del my_dict["age"] # {"name": " Monkey D Luffy ", "sex": " male "} # del Delete entire dictionary del
my_dict # my_dict It has been deleted , A second visit is undefined # clear Empty the whole dictionary my_dict.clear() # {}
<4>len In the survey dictionary , Number of key value pairs
my_dict = {"name": " Monkey D Luffy ", "sex": " male ", "age": 18} len(my_dict) # 3
<5>keys Returns a dictionary containing all KEY List of
my_dict = {"name": " Monkey D Luffy ", "sex": " male ", "age": 18} my_dict.keys # ["name", "sex",
"age"]
<6>values Returns a dictionary containing all value List of
my_dict = {"name": " Monkey D Luffy ", "sex": " male ", "age": 18} my_dict.values # [" Monkey D Luffy ", " male ", 18
] <7>items Returns a containing all ( key , value ) The list of Yuanzu ```c my_dict = {"name": " Monkey D Luffy ", "sex": " male ", "age":
18} my_dict.items # [("name", " Monkey D Luffy "), ("sex", " male "), ("age", 18)]
<> Four , aggregate

<>1. aggregate (set) Is an unordered sequence of non repeating elements .

<> You can use braces { } perhaps set() Function create collection , be careful : To create an empty collection, you must use set() instead of { }, because { } Is used to create an empty dictionary .
my_set = set() # Sets can be de duplicated my_set = {" Monkey D Luffy ", " Sauron ", " Nami ", " Monkey D Luffy "} print(my_set) # {
" Monkey D Luffy ", " Sauron ", " Nami "}
<>2. Common operations for collections

<1> Add elements
# add If the element already exists , No operation is performed my_set = set() my_set.add(" Monkey D Luffy ") # {" Monkey D Luffy "} # update
You can also add elements , And the parameter can be a list , tuple , Dictionaries, etc my_set.update({" Sandaoliu ", " Sauron "}) # {" Sandaoliu ", " Sauron ", " Monkey D Luffy "}
<2> Removing Elements
# remove If the element does not exist , An error occurs my_set.remove(" Monkey D Luffy ") # {" Sandaoliu ", " Sauron "} # discard
If the element does not exist , No errors will occur my_set.discard({" Sauron "}) # {" Sandaoliu "} # pop Delete an element in the collection my_set.pop()
<2> Other operations
# len Calculate the number of elements len(my_set) # clear Empty collection my_set.clear() # Determine whether the element exists in the collection x in
my_set # return bool
<>3.set,list,tuple contact
# Mutual conversion my_list = [1, 2, 3, 4] my_tuple = tuple(my_list) # (1, 2, 3, 4) my_set =
set(my_tuple) # {1, 2, 3, 4} # Quick weight removal my_list = [1, 3, 4, 1, 3] my_set = set(
my_list) # {1, 3, 4}
<> Five , ergodic

<> character string , list , The Yuan Dynasty is as like as two peas.
my_str = "hello world" my_list = ["hello", "world"] my_tuple = ("hello",
"world") # The most common traversal for i in my_list: print(i) # Print the elements in turn # With subscript for index, value
inenumerate(my_list): print(index) # Print subscripts in sequence print(value) # Print the elements in turn
<> Dictionaries

<1> Traversing dictionary key( key )
my_dict = {"name": " Monkey D Luffy ", "sex": " male ", "age": 18} for key in my_dict.keys():
print(key) # Print the dictionary in turn key
<2> Traversing dictionary value( value )
my_dict = {"name": " Monkey D Luffy ", "sex": " male ", "age": 18} for value in my_dict.values():
print(value) # Print the dictionary in turn value
<3> Traverses the entries in the dictionary ( element )
my_dict = {"name": " Monkey D Luffy ", "sex": " male ", "age": 18} for item in my_dict.items():
print(item) # Print the entries in the dictionary in turn , Put it in Yuanzu ("name", " Monkey D Luffy ")
<3> Traversing dictionary key-value( Key value pair )
my_dict = {"name": " Monkey D Luffy ", "sex": " male ", "age": 18} for key, value in my_dict.items
(): print(key) # Print the dictionary in turn key print(value) # Print the dictionary in turn value
<> summary

This is it. python Data types commonly used in , How do you feel , Is that enough ?

Technology
©2019-2020 Toolsou All rights reserved,
The 11th Blue Bridge Cup python The real topic of the University Group National Games JavaSwing To achieve a simple Lianliankan games 【Spring Source code analysis 】42-@Conditional Detailed explanation element-ui Step on pit record 2019PHP Interview questions ( Continuously updated )PHPJava Misunderstanding —— Method overloading is a manifestation of polymorphism ? First issue 500 100 million , Set up a new Department , What is Tencent going to do ? Google chrome The browser can't open the web page , But what if other browsers can open it ? Regression of dependent variable order categories (R language )【Golang Basic series 10 】Go language On conditional sentences if