list (list)
use [...] Represents a list (list).
a = [10, 20, 30, 40]
Multi line narration , The last comma can be omitted .
colors = [
'red',
'green',
'blue',
]
The element types of the list can be different .
a = [10, 'ABC']
The traversal of the list uses for.
a = [1, 2, 3, 4, 5]
for n in a:
print n
[n] Subscript subscript . from 0 Start .
a = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
a1 = a[0] # The first 0 individual : 'A'
a2 = a[2] # The first 2 individual : 'C'
[n:m] On behalf of the n From the beginning to The first m The previous one ( That is to say The first m - 1 individual ) Elements .n The words omitted represent from 0 Start 、m Omitted words mean to the end .
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a1 = a[2:4] # The first 2 From the beginning to the 3 individual : [2, 3]
a2 = a[2:] # The first 2 From the beginning to the end : [2, 3, 4, 5, 6, 7, 8, 9]
a3 = a[:4] # The first 0 I started to 3 individual : [0, 1, 2, 3]
[n:m:s] Of s It stands for several jumps .
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a1 = a[1:8:2] # The first 1 One to the first 7 individual , Every jump 2 individual : [1, 3, 5, 7]
n, m When it's negative , It refers to the number from the end .
a = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
a1 = a[-1] # The last character 'G'
a2 = a[-3:-1] # Count... From the end 3 The first... To the end 1 Characters : ['E', 'F']
+ Operators represent combinations of lists .
print [1, 2, 3] + [4, 5, 6] #=> [1, 2, 3, 4, 5, 6]
len() Get the number of elements in the list .
print len([1, 2, 3]) #=> 3
List of lists ( List nesting ).
a = [[1, 2], [3, 4], [5, 6]]
for list in a:
for n in list:
print n, #=> 1 2 3 4 5 6
Tuples (tuple)
(...) Used to represent tuples (tuple). Tuples are used almost the same as lists , The content cannot be modified or edited .
a = (10, 20, 30, 40)
When there is only one element , Add a comma at the end (,), Used to distinguish the parenthesis operator .
a = (10) # It's not a tuple , It is 10 In itself
a = (10,) # Tuples with one element
Once the contents of tuples are established, they cannot be modified .
a1 = [10, 20, 30, 40]
a2 = (10, 20, 30, 40)
a1[2] = 60 # You can assign
a2[2] = 60 # Will report a mistake
adopt list() You can convert tuples to new lists , adopt tuple() Turn the list into a new tuple .
print list((1, 2, 3)) #=> [1, 2, 3]
print tuple([1, 2, 3]) #=> (1, 2, 3)
Dictionaries (dict)
{ ...} To mark , Dictionaries (dict) Is a set of key value pairs .
d = {'Yamada': 30, 'Suzuki': 40, 'Tanaka': 80}
Access method of elements
d1 = d['Yamada']
d2 = d['Suzuki']
d3 = d['Tanaka']
Get the collection and key of all elements , Collection of values . Use items(), keys(), valus(), iteritems().
d = {'Yamada': 30, 'Suzuki': 40, 'Tanaka': 80}
for k, v in d.items():
print k, v # Tanaka 80, Yamada 30, Suzuki 40
for k in d.keys():
print k, d[k] # Suzuki 40, Yamada 30, Tanaka 80
for v in d.values():
print v # 80, 30, 40
for k, v in d.iteritems():
print k, v # Tanaka 80, Yamada 30, Suzuki 40
List functions (map(), filter(), reduce())
map() The function is to process each element of the list one by one , Return the processing result to .
Here are all the elements *2 To deal with .
a = [1, 2, 3]
def double(x): return x * 2
print map(double, a) #=> [2, 4, 6] : Function mode
print map(lambda x: x * 2, a) #=> [2, 4, 6] : lambda The way
print [x * 2 for x in a] #=> [2, 4, 6] : In package mode 【 Postscript 】
filter() Process the contents of the list one by one , If the processing result is true, keep it . The following process returns an odd number .
a = [1, 2, 3]
def isodd(x): return x % 2
print filter(isodd, a) #=> [1, 3] : Function mode
print filter(lambda x: x % 2, a) #=> [1, 3] : lambda The way
print [x for x in a if x % 2] #=> [1, 3] : In package mode 【 Postscript 】
reduce() Is to process the first two elements first , Then process the result with the next element , And so on . Return a single result . The following process is to calculate the total .
a = [1, 2, 3, 4, 5]
def add(x, y): return x + y
print reduce(add, a) #=> 15 : Function mode
print reduce(lambda x, y: x + y, a) #=> 15 : lambda The way
List inclusion notation
List inclusion notation It means No map(), filter(), lambda You can do some simple list processing .
a = [1, 2, 3]
print [x * 2 for x in a] #=> [2, 4, 6]
print [x * 2 for x in a if x == 3] #=> [6]
print [[x, x * 2] for x in a] #=> [[1, 2], [2, 4], [3, 6]]
print [(x, x * 2) for x in a] #=> [(1, 2), (2, 4), (3, 6)]
b = [4, 5, 6]
print [x * y for x in a for y in b] #=> [4, 5, 6, 8, 10, 12, 12, 15, 18]
print [a[i] * b[i] for i in range(len(a))] #=> [4, 10, 18]
aggregate (set)
aggregate (set) It can be understood as a list without repeating elements . Sets can be subtracted 、OR、AND、XOR Wait for the operation .
a = set(['red', 'blue', 'green'])
b = set(['green', 'yellow', 'white'])
print a #=> set(['red', 'blue', 'green'])
print b #=> set(['green', 'yellow', 'white'])
print a - b #=> set(['red', 'blue'])
print a | b #=> set(['red', 'blue', 'green', 'yellow', 'white'])
print a & b #=> set(['green'])
print a ^ b #=> set(['red', 'blue', 'yellow', 'white'])
print 'green' in a #=> True
a.add('black')
print a #=> set(['red', 'blue', 'green', 'black'])