How is Python's List best handled? Correct case explanation

This article mainly deals with some unusual list processing. For some functions or methods of Python list, please refer to the common Python list methods below

Relevant methods will continue to be added, and the readers are also expected to have some methods that they don't know how to use, or other methods, please remind us


We should note that the theory of light is not enough. Here, by the way, we have a set of 2020's latest Python introduction to advanced project practice video tutorial. You can go to the small-scale Python exchange. Skirt: seven clothes, 97 bars and five (Digital homophony) can be found under conversion, and you can also exchange with the old driver for advice!

  1. Handle each element of the list one by one
>>> import math
>>> a = [math.sqrt(i) for i in range(10)]
[0.0, 1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903, 3.0]
>>> b = [list(range(1,5)), list(range(5,9)), list(range(9,13))]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
  • Create a list for direct processing
>>> # One dimensional mode
>>> [int(i*10)/10 for i in a]
[0.0, 1.0, 1.4, 1.7, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0]
>>> # Two dimensional mode
>>> [[b[j][i]**2 for i in range(len(b[j]))] for j in range(len(b))]
[[1, 4, 9, 16], [25, 36, 49, 64], [81, 100, 121, 144]]
  • With functions, you can reuse them and deal with more complex situations
>>> def def func(sequence):
...     return [int(i*10)/10 for i in sequence]
>>> print(func(a))
[0.0, 1.0, 1.4, 1.7, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0]
  • Using map + function can simplify the processing of function, more suitable for single variable or list
>>> def func(i):
...     return int(i*10)/10
>>> list(map(func, a))
[0.0, 1.0, 1.4, 1.7, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0]
>>> func(1.41459)
1.4
  • Using the method of map + lambda, we can make map correspond to multi parameter function
>>> list(map(lambda i:int(i*10)/10, a))
[0.0, 1.0, 1.4, 1.7, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0]
  • Use map+lambda+function
>>> def Map(func, sequence, *argc):
...     return list(map(lambda i:func(i, *argc), sequence))
>>> Map(round, a, 2)
[0.0, 1.0, 1.41, 1.73, 2.0, 2.24, 2.45, 2.65, 2.83, 3.0]
>>> Map(int, a)
[0, 1, 1, 1, 2, 2, 2, 2, 2, 3]
  1. Conditional selection / deletion of elements
  • Use if condition to create a new list
>>> # One dimensional mode
>>> [i for i in a if 2<i<3]
[2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903]
  • Use filter + function
>>> def func(i):
...     return 2<i<3
>>> list(filter(func, a))
[2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903]
  • Using filter + lambda
>>> list(filter(lambda i: 2<i<3, a))
[2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903]
  • Use filter + lambda + function
>>> def Filter(func, sequence, *argc):
...     return list(filter(lambda i:func(i, *argc), sequence))
>>> Filter(float.__gt__, a, 2) # Greater than 2
>>> b = ['This', 'There', 'Where', 'Here']
>>> Filter(str.__contains__, b, 'T') # With 'T' in string
['This', 'There']
>>> Filter(str.__contains__, b, 'r') # With 'r' in the string
['There', 'Where', 'Here']
  1. Flatten list
def Flat_List(sequence):
    result = []
    if isinstance(sequence, list):
        for item in sequence:
            if isinstance(item, list):
                result += Flat_List(item)
            else:
                result.append(item)
        return result
    else:
        return sequence
Flat_List([[1,2,[3,4,5],6,[7,8,[9,10],[11,12]]]])
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  1. List conversion
  • Lists and dictionaries
>>> a = [[1,2],[3,4]]
>>> b = dict(a)
>>> b
{1: 2, 3: 4}
>>> list(map(list, b.items()))
[[1, 2], [3, 4]]
  1. Arrangement and combination of lists
>>> a = [1,2,3]
>>> [[i, j] for i in a for j in a]
[[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
>>> [[i, j] for i in a for j in a if i!=j]
[[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
>>> from itertools import permutations, combinations, product
>>> list(map(list, permutations(a, 2)))
[[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
>>> list(map(list, combinations(a, 2)))
[[1, 2], [1, 3], [2, 3]]
>>> b = [4, 5, 6]
>>> list(map(list, product(a, b)))
[[1, 

Tags: Python Lambda

Posted on Wed, 13 May 2020 10:09:14 -0400 by nac20