2.1 List comprehensions
First we look at this code, gives a list, by circulation, I would like to add a list of 1 to 10:
li = []
for i in range(1,11):
li.append(i)
print(li)
Well, according to the above requires us to write a list comprehension about:
ls = [i for i in range(1,11)]
print(ls)
The above code is the list comprehension, then we will be a list comprehensions Category:
List comprehensions divided into two modes:
1. circulation mode: [variables (process variables) for the variable in iterable]
2. Screening Mode: [variables (process variables) for the variable in iterable if conditions]
2.1.1 cycle mode
- The squares of all integers within 10 written list.
l1 = [i*i for i in range(1,11)]
print(l1)
- Within 100 all even written list.
l1 = [i for i in range(2,101,2)]
print(l1)
- Write List lst period from python1 period to python24
lst = [f'python{i}期' % i for i in range(1,25)]
print(lst)
Above that variable formatted output of f'python {i} ', the processing is variable.
2.1.2 Screening mode
Filtering mode is the basis of the above plus a determination condition, the condition variable is left to the list.
The elements of this list of more than 3 to stay.
l1 = [4, 3, 2, 6, 5, 5, 7, 8]
print([i for i in l1 if i > 3])
Within thirty number can be divisible by three.
multiples = [i for i in range(30) if i % 3 == 0] print(multiples)
Filtered list of strings of length less than 3, and the remaining to uppercase
l = ['wusir', 'laonanhai', 'aa', 'b', 'taibai'] # print([i.upper() for i in l if len(i) > 3])
Find all the names in the name list contains two nested 'e' is ( difficult )
names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'], ['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva']] print([name for lst in names for name in lst if name.count('e') >= 2]) # 注意遍历顺序,这是实现的关键
2.2 Generator expressions (very important)
Syntactically identical list generator expression and comprehension, but the [] with () on the line. For example the square of the number of all ten or less into a generator expression in
gen = (i**2 for i in range(10))
print(gen)
# 结果: <generator object <genexpr> at 0x0000026046CAEBF8>
Generator expressions can also be screened
# 获取1-100内能被3整除的数
gen = (i for i in range(1,100) if i % 3 == 0)
for num in gen:
print(num)
And generating a list of expressions derived distinction formula:
- Derivation comparator list memory consumption, all one-time data is loaded into memory. The generator expression follows the iterator protocol, generating an element one by one.
- The resulting values are different, a listing is obtained a formula derived list of generator expression is obtained by a generator
- List comprehension at a glance, to generate a memory address just expressions.
Whether generator expression, or list comprehensions, he just Python provides you with a relatively simple manner of construction, as the derivation is very simple to use, so most will be fascinated, this must be careful derivation only build a relatively complex subject and regularly, for no law, nesting and more (for more than three cycles) so as not to suggest that you build with comprehension.
Inert mechanism Generator: The generator only when accessed only value
1.1.4 Other relevant derivations (understand)
Dictionary derivations
lst1 = ['jay','jj','meet']
lst2 = ['周杰伦','林俊杰','郭宝元']
dic = {lst1[i]:lst2[i] for i in range(len(lst1))}
print(dic)
结果:
{'jay': '周杰伦', 'jj': '林俊杰', 'meet': '郭宝元'}
Set derivations
Set derivations can help us to directly generate a set of feature set; disorder not repeat derivation it comes to re-set function
lst = [1,2,3,-1,-3,-7,9]
s = {abs(i) for i in lst}
print(s)
结果:
{1, 2, 3, 7, 9}