List dictionary tuple collection data structure in Python
- 2020-04-02 14:20:47
- OfStack
This article summarizes the list, dictionary, tuple and collection data structure in Python in detail. Share with you for your reference. Specific analysis is as follows:
List:
shoplist = ['apple', 'mango', 'carrot', 'banana']
Dictionary:
di = {'a':123,'b':'something'}
Collection:
jihe = {'apple','pear','apple'}
Tuples:
t = 123,456,'hello'
List 1.
Empty list: a=[]
Function method:
a.append(3) >>>[3]
a.extend([3,4,5]) >>>[3,3,4,5] # Add a list sequence
a.insert(1,'hello') >>>[3,'hello',3,4,5]
a.remove(3) >>>['hello',3,4,5] # Delete the first one that appears 3 , there is no 3 Is an error
a.pop() >>>['hello',3,4]
a.pop(0) >>>[3,4]
a.index(4) >>>1 # Returns the first one that appears 4 The subscript
a.count(3) >>>1 # List element 3 The number of
a.sort >>>[3,4] # The sorting
a.reverse() >>>[4,3] # trans
Delete element method:
a.remove(3) # Deletes an element by value, removing the first element as an argument
a.pop() # The element is deleted by subscript, the last value of the list is deleted by default, and the element with parameter is deleted by subscript
del a[0] # Delete an element by subscript,
del a[2:4] # delete a Table subscript for 2,3 The elements of the
del a[:] # delete a List all elements
del a # Delete the list
List derivation:
vec = [2,4,6]
[3*x for x in vec if x<6] >>>[6,12] 3*2,3*4
vec2 = [1,2,3]
[x*y for x in vec for y in vec2] >>>[2,4,6,4,8,12,6,12,18]
Nested list derivation:
mat = [
[1,2,3],
[4,5,6],
[7,8,9]
]
print ([[row[i] for row in mat] for i in [0,1,2]])
>>>[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Thinking: what's the difference between the results of list (zip(mat)) and list (zip(*mat)
2. A tuple
Null tuple: t = ()
Tuple assignment: t = (123,345)
T [0]
>
>
>
123
3. The dictionary
d = {'Jack':'jack@mail.com','Tom':'Tom@main.com'}
d['Jack'] >>>'jack@mail.com
d['Jim'] = 'Jim@sin.com' >>>{'Jim': 'Jim@sin.com', 'Jack': 'jack@mail.com', 'Tom': 'Tom@main.com'}
del d['Jim'] >>>{'Jack': 'jack@mail.com', 'Tom': 'Tom@main.com'}
list(d.keys()) # Returns an unordered list of all the keywords in the dictionary
sorted(d.keys()) # A sorted list of all the keywords in a dictionary is returned
dict() # The constructor can be written directly from key-value Create a dictionary in pairs
dict([('Tim',123),('Tiny',234)]) >>>{'Tiny': 234, 'Tim': 123}
Derivation to create a dictionary:
{d2:d2+'@main.com' for d2 in list(d.keys())}
>>>{'Jack': 'Jack@main.com', 'Tom': 'Tom@main.com'}
Exercise: loop output key value pairs in the dictionary:
for name,email in d.items():
print(name,email)
Collection of 4.
Empty set: A = set() does to create an empty set, you must use set()
Presentation:
basket = {'apple','orange','apple'} >>>{'orange', 'apple'} # Note that duplicate elements show only one
'apple' in basket >>>True
'pear' in basket >>>False
Mathematical operation of set:
a = set('ababcdabca') >>>{'c', 'b', 'a', 'd'}
b = {'a','b','m'} >>>{'b', 'a', 'm'}
a - b >>>{'c', 'd'}
b - a >>>{'m'}
a | b >>>{'c', 'd', 'b', 'a', 'm'}
a & b >>>{'a','b'}
a ^ b >>>{'c','d','m'}
Set derivation formula:
{x for x in a if x not in 'ab'} >>>{'c','d'}
I hope this article has helped you with your python programming.