1. Merge list (extend)
Like tuples, add up the two lists with the plus sign (+) to achieve merge:
In [1]: x=list(range(1, 13, 2)) In [2]: x + ['b', 'a'] Out[2]: [1, 3, 5, 7, 9, 11, 'b', 'a']
For defined lists, you can use the extend method to add multiple elements at once:
In [7]: x2=[3, 6, 1] In [8]: (x2) In [9]: x Out[9]: [1, 3, 5, 7, 9, 11, 3, 6, 1, 3, 6, 1, 3, 6, 1]
It should be noted that the plus sign (+) performs a list merge is a waste of resources, because it is necessary to create a new list and copy all objects in the past, and it is much better to attach elements to an existing list with extend (especially when building a large list).
Therefore, when performing list merging operations, especially for list merging of large data volumes, it is highly recommended to use the extend function.
2. List sort (sort)
The sort method of the list can implement in-place sorting (no need to create new objects, strings are sorted by initial letter):
In [10]: a=[1, 5, 3, -3, 0] In [11]: () In [12]: a Out[12]: [-3, 0, 1, 3, 5] In [13]: s=['a','ab','3e','z'] In [14]: () In [15]: s Out[15]: ['3e', 'a', 'ab', 'z']
sort has several good options, one is the secondary sort key, that is, a function that can produce values that can be used to sort. If you can sort a set of strings by length:
In [16]: b=['a','nl','drz','mowgt','aa'] In [17]: (key=len) In [18]: b Out[18]: ['a', 'nl', 'aa', 'drz', 'mowgt']
For example, whether to sort in descending order, as shown in the following example of sorting the first letter in descending order:
In [21]: (key= lambda x:x[0], reverse=True) In [22]: b Out[22]: ['nl', 'mowgt', 'drz', 'a', 'aa']
3. Binary search and maintenance of ordered lists (bisect)
The built-in bisect module implements binary search and insertion of ordered lists. You can find out where the new element should be inserted to maintain order in the metalist, and insert the new element to that correct position.
In [23]: import bisect In [24]: c=[1,2,1,-1,4,5] In [25]: () In [26]: (c, 2) Out[26]: 4 In [27]: (c, 4) In [28]: c Out[28]: [-1, 1, 1, 2, 4, 4, 5]
Note: The functions of the bisect module will not judge whether the original list is ordered, because this overhead is too high; therefore, although there is no error when using them as unordered lists, it may lead to incorrect results. Based on this, it is recommended to perform sorting operations on the original list before using the functions of the bisect module.
3. Slice (index operator [] and start: stop)
You can slice the sequence type (array, list, tuple, etc.). The elements at the start index are included in the result of the slice, the elements at the stop index are not included in the result, and the number of elements is stop-start. Both start or stop can be omitted, and they default to the beginning and end of the sequence respectively.
You can also add a step after the second colon, such as taking an element every other bit:
In [35]: d=[x for x in range(10)] In [36]: d Out[36]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [37]: e=d[:8:2] In [38]: e Out[38]: [0, 2, 4, 6]
You can use -1 ingeniously to implement inverse order of lists or elements, as follows:
In [40]: f=d[::-1] In [41]: f Out[41]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
4. Built-in sequence functions of list
4.1 enumerate
The enumerate function can return (i, value) tuples of a sequence one by one, as shown in the following example:
In [43]: #for i value in enumerate(collection): In [44]: #Do something with i, valueIn [49]: slist=['qin', 'wang', 'wqc'] In [50]: mapping = dict((v, i) for i, v in enumerate(list)) In [51]: mapping Out[51]: {'qin': 0, 'wang': 1, 'wqc': 2}
4.2 sorted
The sorted function can return any sequence to a new ordered list (note: the sort function is sorted in-place), as follows:
In [59]: sorted(['z', 'd', 'c', 'n']) Out[59]: ['c', 'd', 'n', 'z'] In [60]: sorted('my name is chaogo') Out[60]: [' ', ' ', ' ', 'a', 'a', 'c', 'e', 'g', 'h', 'i', 'm', 'm', 'n', 'o', 'o', 's', 'y']
Sorted and set are often used in combination to obtain an ordered list of unique elements in a sequence:
In [61]: set(sorted('my name is chaogo')) Out[61]: {' ', 'a', 'c', 'e', 'g', 'h', 'i', 'm', 'n', 'o', 's', 'y'} In [62]: sorted(set('my name is chaogo')) Out[62]: [' ', 'a', 'c', 'e', 'g', 'h', 'i', 'm', 'n', 'o', 's', 'y'] In [63]: set('my name is chaogo') Out[63]: {' ', 'a', 'c', 'e', 'g', 'h', 'i', 'm', 'n', 'o', 's', 'y'}
The above results are the same, what is the reason? This is because: (1) set and sorted operate on sequences. When the parameters are not sequences, they will be converted to lists by default; (2) set will sort elements by default.
4.3 zip
Zip is used to "pair" elements in multiple sequences (lists, tuples, etc.) to produce a new list of tuples; Zip can accept any number of sequences, and the number of tuples finally obtained is determined by the shortest sequence; the most common usage of zip is to iterate multiple sequences at the same time, and can also be used in conjunction with enumerate, as follows:
In [77]: seq1 = ['chao', 'qing', 'wq'] In [78]: seq2 = ['qin', 'wang', 'qc'] In [79]: for i , (a,b) in enumerate(zip(seq1, seq2)): ...: print('%d: %s %s' % (i, a, b)) ...: 0: chao qin 1: qing wang 2: wq qc
For "zipped" sequences, zip has a clever use, that is, decompress the sequence (unzip, represented by *). In fact, it is to convert a set of rows into a set of columns, as follows:
In [86]: pitchers = [('a','b'), (1,2), ('tmd','bat')] In [87]: one, two = zip(*pitchers) In [88]: one Out[88]: ('a', 1, 'tmd') In [89]: two Out[89]: ('b', 2, 'bat')
4.4 reversed
Used to iterate over elements in a sequence in reverse order, as follows:
In [92]: [x for x in reversed([1, 2, 5, 3, -1])] Out[92]: [-1, 3, 5, 2, 1]
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.