Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.


zipfunction

The zip() function can combine multiple lists into a single list, packing multiple list elements into tuples.


titles = ['t1'.'t2'.'t3']
hrefs = ['/t1.html'.'/t2.html'.'/t3.html']

for title, href in zip(titles, hrefs):
    print(title, href)
    
# The result is as follows
t1 /t1.html
t2 /t2.html
t3 /t3.html
Copy the code


Zip is smart, if the list length is different, it will cut itself, no error

titles = ['t1'.'t2'.'t3']
hrefs = ['/t1.html'.'/t2.html']

for title, href in zip(titles, hrefs):
    print(title, href)
    
# The result is as follows
t1 /t1.html
t2 /t2.html
Copy the code


If multiple lists have a high sequential correlation of data, you can package them using the ZIP function to facilitate manipulation of multiple list elements.


Derivations in Python

List derivation

List comprehensions make it easy to construct lists, and can help you iterate and evaluate lists in a single line of code.

The syntax is as follows:

[expression for variable in list] or [expression for variable in list if condition]

nums = [1.2.3.4.5]

li = [num**2 for num in nums]	Square each number in the list

# The result is as follows
# [1, 4, 9, 16, 25]
Copy the code


Odd or even up to 10

# odd
odd_num = [num for num in range(1.11) if num % 2= =1]
# [1, 3, 5, 7, 9]

# the even
even_num = [num for num in range(1.11) if num % 2= =0]
# [2, 4, 6, 8, 10]
Copy the code


99 times table

# 99 Times table
t_99 = ['\t'.join([f'{col} * {row} = {row * col}' for col in range(1, row + 1)]) for row in range(1.10)]

print('\n'.join(t_99))

# The result is as follows
1 * 1 = 1
1 * 2 = 2	2 * 2 = 4
1 * 3 = 3	2 * 3 = 6	3 * 3 = 9
1 * 4 = 4	2 * 4 = 8	3 * 4 = 12	4 * 4 = 16
1 * 5 = 5	2 * 5 = 10	3 * 5 = 15	4 * 5 = 20	5 * 5 = 25
1 * 6 = 6	2 * 6 = 12	3 * 6 = 18	4 * 6 = 24	5 * 6 = 30	6 * 6 = 36
1 * 7 = 7	2 * 7 = 14	3 * 7 = 21	4 * 7 = 28	5 * 7 = 35	6 * 7 = 42	7 * 7 = 49
1 * 8 = 8	2 * 8 = 16	3 * 8 = 24	4 * 8 = 32	5 * 8 = 40	6 * 8 = 48	7 * 8 = 56	8 * 8 = 64
1 * 9 = 9	2 * 9 = 18	3 * 9 = 27	4 * 9 = 36	5 * 9 = 45	6 * 9 = 54	7 * 9 = 63	8 * 9 = 72	9 * 9 = 81

Copy the code


The derivation above is like the for loop below

ret_li = []
for row in range(1.10):
    
    ret_li.append('\t'.join([f'{col} * {row} = {row * col}' for col in range(1, row + 1)))Copy the code

Restore the derivation of join to for

ret_li = []
for row in range(1.10):
    temp_li = []
    
    for col in range(1, row + 1)
    	Add each line to the temporary list
        temp_li.append(f'{col} * {row} = {row * col}')
        
    ret_li.append('\t'.join(temp_li))
Copy the code


Dictionary derivation

Since Python2.7, the concept of list derivation has been ported to dictionaries, resulting in dictionary derivation


titles = ['t1'.'t2'.'t3']
hrefs = ['/t1.html'.'/t2.html'.'/t3.html']


news_dict = {title: href for title, href in zip(titles, hrefs)}

# The result is as follows
{'t1': '/t1.html'.'t2': '/t2.html'.'t3': '/t3.html'}
Copy the code


Cookie string is converted to cookie information dictionary

cookies_str = 'session_id=asdhasj; name=hui; age=18'

# according to the first `; 'split, then split by' = ', index 0 is the key of the dictionary, 1 bit value
cookie_dict = {info.split('=') [0]: info.split('=') [1] for info in cookies_str.split('; ')}

# The result is as follows
{'session_id': 'asdhasj'.'name': 'hui'.'age': '18'}
Copy the code


Set derivation

Set features: the elements do not repeat, so the set derivation comes with the ability to remove weight.

li = [1, -1.8, -8.12]
s = {abs(i) for i in li}

# The result is as follows
{8.1.12}
Copy the code


Generator derivation

In [1]: g_num = (num for num in range(1000))

In [2]: g_num
Out[2]: <generator object <genexpr> at 0x00000211C2A118C8>

In [3] :next(g_num)
Out[3] :0

In [4] :next(g_num)
Out[4] :1
Copy the code


The tail language

✍ Code writes the world and makes life more interesting. ❤ ️

✍ thousands of rivers and mountains always love, ✍ go again. ❤ ️

✍ code word is not easy, but also hope you heroes support. ❤ ️