What are the three types of python sequence
Python sequence types include: list, tuple, dictionary
List: ordered variable sequence
Create: userlist = [1,2,3,4,5,6]
Modify: userlist[5] = 999
Add: userlist.append(777)
Delete: userlist.remove(4) or del(userlist[3])
Pop method: remove an element, the default is the last one.
userlist.pop(3) removes the third element and returns the value.
Insert: userlist.insert(3,555)
Sort: userlist.sort() defaults to ascending order. userlist.sort(reverse=True) uses descending order. Or use sorted(userlist) to sort
Reverse: userlist.reverse()
Search: userlist.index(3) or use in reserved words to search
Take element: userlist[2]
Take the coordinates: userlist.index(999)
The connection of the list: extend() method. Or use + to connect two lists. The two are different
Tuple: ordered immutable sequence
Create: tuple1=(1,2,3,4,5,6)
Modify: the value cannot be modified
Add: There is no append function, it can only be added by assignment: tuple2=(tuple1,7,8,9)
Delete: (the immutable sequence does not have this attribute)
Insertion: (the immutable sequence does not have this attribute)
Sorting: can only be sorted using sorted (userlist)
Reverse: (Immutable sequence does not have this attribute)
Search: userlist.index(3) or use in reserved words to search
Take element: tuple1[4]
Take the coordinates: tuple1.index(3)
Deduplication: set(tuple1)
Unpacking: a,b,c,d,e,f = tuple1
Dictionary: Unordered variable sequence
Create: dict1={'a':'001','b':'002','c':'003','d':'004'} Or use a function to create a dictionary: dict1 = dict([(' a','001'),('b','002'),('c','003'),('d','004')))
Modify: the value cannot be modified
Add: direct assignment: dict1['f'] = '006'; or use the setdefault() function to add dictionary elements: dict1.setdefault('e','005'), when the key already exists, keep the original The kv remains unchanged. When the key does not exist, the kv is added.
Delete: The dictionary does not have the remove() function, but the kv of the dictionary can be deleted with the del() function: del(dict1['e']). You can also use the pop() method to delete the specified element. Since the dictionary is unordered, pop() will not delete the last element by default. You must specify the key
Insertion: The dictionary has no index coordinates, only adding, not inserting
Sorting: The dictionary has no index coordinates, so it is also unordered, and the value can only be found by key. But it can be sorted by other methods: for k in sorted(dict1): print(k,dict1[k])
Reversal: (disordered cannot be reversed)
Search: dict1['c'] or use in reserved words to search. Or use the items() method to convert each pair of kv in the dictionary into a tuple for convenient search
Get element: dict1['c'] or use dict1.get('c')
Take the coordinates: the key is unique, the value is not unique, it can only be found by looping the convenience dictionary
De-duplication: unique key, no need to de-duplicate
String-tuple-list-dictionary type conversion
1、 Convert tuples to lists: list()
2、 The list is turned into a tuple: tuple()
3、 Convert dictionaries to lists and tuples: dict1.items()
4、 The list ancestor is converted to a dictionary: dict()
Knowledge point expansion:
Sequence types in Python include:
Container sequence
Container sequence
Flat sequence
Variable sequence
Immutable sequence
Once created, it cannot be modified.
The above is the detailed content of the detailed explanation of python sequence types. For more information about the three types of python sequence types, please pay attention to other related articles on ZaLou.Cn!
Recommended Posts