Python06- formula / regressed two built-in functions

Formula: to simplify the for loop , selecting / generating a list that meets certain criteria, set of dictionaries usually include conditional logic (if-else statement)

List of formula:
list1 = ["A","B","C"]
list2 = ["a","b","c"]
[m+n for m in list1 for n in list2]
my_data=[[1,2,3],[4,5,6],[7,8,9]]
rows_to_keep = [row for row in my_data if row[2]>5][4,5,6]  [7,8,9]
Collection formula:
my_data=[(1,2,3),(4,5,6),(7,8,9),(7,8,9)]
set_of_tuple1 = {x for x in my_data}
set_of_tuple2 = set(mydata)

As a result two, with the formula For example, use can be set up

Dictionary of formula:
my_dictionary = {'customer1':7 ,'customer2':9,'customer3':11}
my_results = {key:value for key,value in my_dictionary.items() if value > 10}
#筛选出值大于10滴

====================================

Two fast hardware BIF
  • filter - to filter out non-True content
    filter (function or none, iterable)

EG1 : front is none, behind the filter out data (True)

list([filter(None,[1,0,False,True])])
[1,True]

EG2 : front as a function of the data corresponding to the function data later iteration

def odd(x):
    return x%2
temp = range(10)
show = filter(odd,temp)
list(show)
[1,3,5,7,9]

The filter function of the number of iterations is substituted, leaving the filter function of qualified items (item into the function, the return result item. 1)

  • map-- mapping, apply the formula for each element
list(map(lambda x:x*2, range(10)))
[0,2,4,6,8,10,12,14,16,18]

# attention:强大的map后边可以接受多个序列作为参数
list(map(lambda x,y:[x y],[1,3,5,7,9],[2,4,6,8,10]))
[[1,2],[3,4]……]
Published 56 original articles · won praise 0 · Views 775

Guess you like

Origin blog.csdn.net/xiuxiuxiu666/article/details/104316132