Practical Python programming skills - how to list, dictionary, collection, according to criteria to filter the data - data structures and algorithms related issues and solving skills

 

actual case:

1. was filtered off negative listing [3,9, -1,10,20, -2 ..] in

2. Sieve the dictionary { 'LiLei': 79, 'Jim': 88, 'Lucy': 92 ...} value is lower than the entry 90

3. screening the set {77,89,32,20} .. whole element 3 can be out

 

--- the most common method, the FOR loop iteration, determines if filter plus

E.g: 

 

Write the question: is very verbose in Python, efficiency is very poor!

 

solution---

 List

 Liezi:

 List comprehension:

 

 

'' '
Filter function
filter (,) receives two parameters,
the first, passing a filter function, usually with lambda create an anonymous function
filter function that takes a parameter x, the x is in each list, iterate elements
returns a Boolean value, a Boolean value is false, the element will be discarded
second argument is to filter the list

filter
in the python2.7, will directly return a list
in python3 It will return a generator object
if python3 achieve the correct result in a list, it is necessary:
the generator object, directly to the list builder
** Builder the object is a one-time, if the next has been consumed, the need to re-create

 

 to sum up:

  Recommended list comprehension, more simple, no need to create function objects, faster

 

 dictionary:

 

 

 

 

set

 

Guess you like

Origin www.cnblogs.com/goddog1024/p/11228873.html