list(list):
A list is an ordered collection from which elements can be added and removed at any time
Define a list:
classmates = ['Michael','Bob','Tracy']
Use the len() function to get the number of list elements:
In [3]: len(classmates)
Out[3]: 3
You can also use the index to access the element at each position, remember that the index starts from 0, and an error will be reported if it exceeds the range:
In [5]: classmates[0]
Out[5]: 'Michael'
In [6]: classmates[1]
Out[6]: 'Bob'
In [7]: classmates[3]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-7-81a27e3ce05f> in <module>()
----> 1 classmates[3]
IndexError: list index out of range
List related operations:
<1> Add elements ("add" append, extend, insert):
Why are there three? Of course they are different, ok, let's give an example
In [8]: names = [100,200,'300','ok']
In [9]: names.append("400")
In [10]: names
Out[10]: [100, 200, '300', 'ok', '400']
See the effect, yes append is to add at the end of the list
extend is the merging of two lists
In [13]: names2 = [500,600]
In [14]: names.extend(names2)
In [15]: names
Out[15]: [100, 200, '300', 'ok', '400', 500, 600]
Out[15]: [100, 200, '300', 'ok', '400', 500, 600]
insert(index.object) inserts the element object before the specified position index:
In [16]: names.insert(0,"000")
In [17]: names
Out[17]: ['000', 100, 200, '300', 'ok', '400', 500, 600]
<2>Modify element: ("modify" specifies index modification)
In [18]: names[2]=1000
In [19]: names
Out[19]: ['000', 100, 1000, '300', 'ok', '400', 500, 600]
<3> Find elements ("check" in, not in, index, count)
in and not in are generally used for judgment
Find if an element exists
in (exists), if exists the result is True, otherwise False
not in (does not exist) if it does not exist, the result is True, otherwise it is False
In [22]: 100 in names
Out[22]: True
In [23]: 100 not in names
Out[23]: False
index (find the element corresponding to the index):
In [19]: names
Out[19]: ['000', 100, 1000, '300', 'ok', '400', 500, 600]
In [20]: names.index('ok')
Out[20]: 4
count finds the number of elements in a list:
In [21]: names.count('ok')
Out[21]: 1
<4>("删"del,pop,remove)
del : delete according to the subscript bit, you can also delete all variables
In [24]: names
Out[24]: ['000', 100, 1000, '300', 'ok', '400', 500, 600]
In [25]: del names[2]
In [26]: names
Out[26]: ['000', 100, '300', 'ok', '400', 500, 600]
pop: By default, only the last one in the list can be deleted and returned to you
In [27]: names.pop()
Out[27]: 600
In [28]: names
Out[28]: ['000', 100, '300', 'ok', '400', 500]
remove : remove the specified content
In [31]: names.remove('000')
In [32]: names
Out[32]: [100, '300', 'ok', '400', 500]
<5> Sort (sort, reverse)
The sort method rearranges the list in a specific order, the default is from small to large, and the parameter reverse=T ure can be sorted from the size of the list
In [36]: num = [1,5,6,32,23,67]
In [37]: num.sort()
In [38]: num
Out[38]: [1, 5, 6, 23, 32, 67]
Note: Sorting must ensure that the elements are of the same type, otherwise an error will be reported.
The reverse method is to reverse the list:
In [42]: num = [1,5,6,32,23,67]
In [43]: num.reverse()
In [44]: num
Out[44]: [67, 23, 32, 6, 5, 1]
Tuple (Original)
The tuple is similar to the list, the difference is that the elements of the tuple cannot be modified, the tuple uses parentheses, and the list uses square brackets
The various operations of tuple access and list are basically the same,
Here's an example:
In [46]: num = [1,5,6,32,23,67]
In [47]: my_tuple = (100,200,num)
In [48]: my_tuple
Out[48]: (100, 200, [1, 5, 6, 32, 23, 67])
In [49]: my_tuple[2][1]= 20
In [50]: my_tuple
Out[50]: (100, 200, [1, 20, 6, 32, 23, 67])
You can take a closer look. On the surface, the ancestor has been modified, but it is not the case. It is only the list element that has been modified, not the ancestor.
dict(dictionary):
Dictionary uses key-value (key-value) storage, with extremely fast lookup speed
In [63]: my_dict = {"name":"laowang","age":25,"sex":"男"}
In [64]: my_dict["name"]
Out[64]: 'laowang'
When accessing a dictionary, we can't use the index, we use the key, and the key is not repeatable in the dictionary
In [67]: my_dict.get("name")
Out[67]: 'laowang'
In [68]: my_dict.get("addr")
You can also use this method to access. When accessing a non-existent key, no error will be reported, and a None will be returned. The above method will report an error.
Additions to the dictionary:
In [69]: my_dict["addr"]="BJ"
In [70]: my_dict
Out[70]: {'addr': 'BJ', 'age': 25, 'name': 'laowang', 'sex': '男'}
In [71]: my_dict["addr"]="SZ"
In [72]: my_dict
Out[72]: {'addr': 'SZ', 'age': 25, 'name': 'laowang', 'sex': '男'}
You can use the above methods to add, if the key does not exist, it is added, and if it already exists, it is modified
To delete, just use del
In [75]: del my_dict["addr"]
In [76]: my_dict
Out[76]: {'age': 25, 'name': 'laowang', 'sex': '男'}
function in dictionary:
keys: returns a list of all keys in the dictionary
In [76]: my_dict
Out[76]: {'age': 25, 'name': 'laowang', 'sex': '男'}
In [77]: my_dict.keys()
Out[77]: dict_keys(['name', 'age', 'sex'])
values: returns a list of all values in the dictionary
In [78]: my_dict.values()
Out[78]: dict_values(['laowang', 25, '男'])
items: returns all key-value pairs in the dictionary
In [79]: my_dict.items()
Out[79]: dict_items([('name', 'laowang'), ('age', 25), ('sex', '男')])
traversal of dictionary
Traversal of keys:
In [82]: for key in my_dict.keys():
...: print(key)
...:
...:
name
age
sex
traversal of value
In [83]: for value in my_dict.values():
...: print(value)
...:
laowang
25
男
Iterate over all items of dictionary
In [86]: for dic in my_dict.items():
...: print("key=%s,value=%s"%(dic))
...:
...:
key=name,value=laowang
key=age,value=25
key=sex,value=男
I'm here today, and it's over. The reason why I copied the previous code is that I hope that friends who want to learn python can type the code themselves instead of copying.