brief introduction
Whether doing scientific calculation or writing applications, we need to use some basic data structures, such as lists, tuples, dictionaries and so on.
This article will explain these basic data structures in Python in detail.
list
A list is a list, which can be expressed in square brackets:
In [40]: ages = [ 10, 14, 18, 20 ,25] In [41]: ages Out[41]: [10, 14, 18, 20, 25]
list has some very useful methods, such as append, extend, insert, remove, pop, index, count, sort, reverse, copy, etc.
for instance:
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana'] >>> fruits.count('apple') 2 >>> fruits.count('tangerine') 0 >>> fruits.index('banana') 3 >>> fruits.index('banana', 4) # Find next banana starting a position 4 6 >>> fruits.reverse() >>> fruits ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange'] >>> fruits.append('grape') >>> fruits ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape'] >>> fruits.sort() >>> fruits ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear'] >>> fruits.pop() 'pear'
Lists are used as stacks
The stack is characterized by last in first out, and the list provides us with append and pop methods, so it is very simple to use the list to realize the stack:
>>> stack = [3, 4, 5] >>> stack.append(6) >>> stack.append(7) >>> stack [3, 4, 5, 6, 7] >>> stack.pop() 7 >>> stack [3, 4, 5, 6] >>> stack.pop() 6 >>> stack.pop() 5 >>> stack [3, 4]
Lists are used as queues
Queues are characterized by first in, first out, but using lists to insert elements at the head of the queue is slow because all elements need to be moved.
We can use collections.deque To quickly operate from both ends:
>>> from collections import deque >>> queue = deque(["Eric", "John", "Michael"]) >>> queue.append("Terry") # Terry arrives >>> queue.append("Graham") # Graham arrives >>> queue.popleft() # The first to arrive now leaves 'Eric' >>> queue.popleft() # The second to arrive now leaves 'John' >>> queue # Remaining queue in order of arrival deque(['Michael', 'Terry', 'Graham'])
List derivation
To create a list, the usual approach is to use a for loop to traverse the list and set a value for it:
>>> squares = [] >>> for x in range(10): ... squares.append(x**2) ... >>> squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Or we can use list derivation to generate a list more succinctly:
squares = [x**2 for x in range(10)]
The structure of a list derivation consists of the following contents contained in square brackets: an expression followed by a for clause, followed by zero or more for or if clauses.
The list derivation will traverse the elements in the for sentence, evaluate them with an expression, and return the generated elements as new list elements.
Look at a complex point:
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
The above expression is equivalent to:
>>> combs = [] >>> for x in [1,2,3]: ... for y in [3,1,4]: ... if x != y: ... combs.append((x, y)) ... >>> combs [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
The list derivation can also be nested, if we have a matrix:
>>> matrix = [ ... [1, 2, 3, 4], ... [5, 6, 7, 8], ... [9, 10, 11, 12], ... ]
You can use the following expression to exchange rows and columns of the matrix:
>>> [[row[i] for row in matrix] for i in range(4)] [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
Or use a simpler zip function:
>>> list(zip(*matrix)) [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
del
del can be used to delete an element in the list. del can delete a specific value in the list, delete slices, or even delete the whole list:
>>> a = [-1, 1, 66.25, 333, 333, 1234.5] >>> del a[0] >>> a [1, 66.25, 333, 333, 1234.5] >>> del a[2:4] >>> a [1, 66.25, 1234.5] >>> del a[:] >>> a [] >>> del a
tuple
Tuples are similar to lists, except that tuples are immutable.
Tuples are represented by parentheses, or parentheses may not be used.
>>> t = 12345, 54321, 'hello!' >>> t[0] 12345 >>> t (12345, 54321, 'hello!') >>> # Tuples may be nested: ... u = t, (1, 2, 3, 4, 5) >>> u ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
Tuple and List operations are very similar, with slicing and indexing operations.
Tuples can be unpacked easily:
>>> x, y, z = t
aggregate
A collection is represented by a set function or curly braces.
The elements in the collection are not repeated, which is very similar to the set in java.
Because the representation of the dictionary is also curly braces, if you need to create an empty set, you need to use set, because the empty {} represents the dictionary.
Look at some simple examples of collections:
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>> print(basket) # show that duplicates have been removed {'orange', 'banana', 'pear', 'apple'} >>> 'orange' in basket # fast membership testing True >>> 'crabgrass' in basket False >>> # Demonstrate set operations on unique letters from two words ... >>> a = set('abracadabra') >>> b = set('alacazam') >>> a # unique letters in a {'a', 'r', 'b', 'c', 'd'} >>> a - b # letters in a but not in b {'r', 'd', 'b'} >>> a | b # letters in a or b or both {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} >>> a & b # letters in both a and b {'a', 'c'} >>> a ^ b # letters in a or b but not both {'r', 'd', 'b', 'm', 'z', 'l'}
Like lists, sets also support derivation:
>>> a = {x for x in 'abracadabra' if x not in 'abc'} >>> a {'r', 'd'}
Dictionaries
The dictionary is also represented by curly brackets. The difference is that the elements in the dictionary are presented in the form of key:value.
Here are some basic operations of the dictionary:
>>> tel = {'jack': 4098, 'sape': 4139} >>> tel['guido'] = 4127 >>> tel {'jack': 4098, 'sape': 4139, 'guido': 4127} >>> tel['jack'] 4098 >>> del tel['sape'] >>> tel['irv'] = 4127 >>> tel {'jack': 4098, 'guido': 4127, 'irv': 4127} >>> list(tel) ['jack', 'guido', 'irv'] >>> sorted(tel) ['guido', 'irv', 'jack'] >>> 'guido' in tel True >>> 'jack' not in tel False
In addition to curly braces, you can also use the dict function to build a dictionary:
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) {'sape': 4139, 'guido': 4127, 'jack': 4098}
If the keyword is a simple character, it can be written as follows:
>>> dict(sape=4139, guido=4127, jack=4098) {'sape': 4139, 'guido': 4127, 'jack': 4098}
The same derivation can also be used:
>>> {x: x**2 for x in (2, 4, 6)} {2: 4, 4: 16, 6: 36}
loop
We usually use the for statement to traverse a set or dictionary, list, etc.
When we traverse the dictionary, we can use the items() method to get the key and value at the same time:
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'} >>> for k, v in knights.items(): ... print(k, v) ... gallahad the pure robin the brave
If it is a list, you can use the enumerate function to get the index and value:
>>> for i, v in enumerate(['tic', 'tac', 'toe']): ... print(i, v) ... 0 tic 1 tac 2 toe
Previously, we also used the zip function, which can match the elements in multiple sequences one by one:
>>> questions = ['name', 'quest', 'favorite color'] >>> answers = ['lancelot', 'the holy grail', 'blue'] >>> for q, a in zip(questions, answers): ... print('What is your {0}? It is {1}.'.format(q, a)) ... What is your name? It is lancelot. What is your quest? It is the holy grail. What is your favorite color? It is blue.