Don't talk much , Let's go straight to work , Saved for a long time !
This should be relatively simple , But everyday use is easy to ignore .
a, b = 5, 10 print(a, b) //5, 10 a, b = b, a print(a, b) //10, 5
Python
Copy
This is also a basic grammar
a = ['python', 'java', 'c++', 'go'] print(','.join(a)) //python,java,c++,go
Python
Copy
Sensory Python Brush algorithm problem is not very cool ?
a = [1, 1, 1, 2, 3, 3, 3, 3, 4, 4, 4] print(max(set(a), key = a.count))
Python
Copy
from collections import Counter a = 'abcdefg' b = 'adcbgfb' print(Counter(a) == Counter(b))
Python
Copy
This one uses Java It can be realized in one sentence
a = 'dadabjdnakdmnkafad' print(a[::-1]) num = 1343453535 print(int(str(num)[::-1])) a = [1,3,554,64,2] print(a[::-1])
Python
Copy
origin = [['a', 'b'], ['c', 'd'], ['e', 'f']] transposed = zip(*origin) print(list(transposed ))
Python
Copy
This is more in line with the habit of mathematics
b = 6 print(4 < b < 7) print(1 == b < 9)
Python
Copy
Actually Python There is no ternary operator in , But we can replace it in another way :
b = 'B' c = 'C' flag = True a = b if flag else c
Python
Copy
def product(a, b): return a * b def add(a, b): return a + b b = True print((product if b else add)(5 ,7))
Python
Copy
Notice that it's not if-else, It is for The cycle can be used else:
a = [1, 2, 3, 4, 5] for el in a: if(el == 0) print(' find 0 了 ') else: print(' Can't find 0')
Python
Copy
d1 = {'a': 1} d2 = {'b': 2} print(**d1, **d2) # python3.5 Support print(dict(d1.items() | d2.items())) d1.update(d2) print(d1)
Python
Copy
items = [2,2,3,4,1] print(list(set(items)))
Python
Copy
Variable length parameters , It's a dictionary .
The double asterisk in front of the dictionary object allows you to enter the contents of the dictionary into the function as a named parameter . The key to the dictionary is the parameter name , Value is the value passed to the function . You don't even need to call it kwargs!
dictionary = {'a': 1, 'b': 2} def func(**kwargs) for key in kwargs: print('key:', key, 'value:',kwargs[key])
Python
Copy
You can manipulate data in a complete list with one line of code
numbers = [1, 2, 3, 4, 5, 6] y = [x for x in numbers if x % 2 == 0] print(y) //[2, 4, 6]
Python
Copy
x = [1, 2, 3] y = map(lambda x : x + 1 , x) print(y) //[2, 3, 4]