程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Skills of meeting each other late in Python

編輯:Python

Don't talk much , Let's go straight to work , Saved for a long time !

1. Exchange variable values

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

2. Combine all the elements in the list into a string

This is also a basic grammar

a = ['python', 'java', 'c++', 'go']
print(','.join(a)) //python,java,c++,go

Python

Copy

3. Find the highest frequency value in the list

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

4. Check whether two strings are composed of the same alphabet in a disordered order

from collections import Counter
a = 'abcdefg'
b = 'adcbgfb'
print(Counter(a) == Counter(b))

Python

Copy

5. Reverse string 、 list

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

6. Transpose a two-dimensional array

origin = [['a', 'b'], ['c', 'd'], ['e', 'f']]
transposed = zip(*origin)
print(list(transposed ))

Python

Copy

7. Chain comparison

This is more in line with the habit of mathematics

b = 6
print(4 < b < 7)
print(1 == b < 9)

Python

Copy

8. Ternary operator

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

9. Chain function call

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

10. for-else grammar

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

11. Merge dictionaries

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

12. Remove duplicate elements from list

items = [2,2,3,4,1]
print(list(set(items)))

Python

Copy

13. **kwargs

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

14. List derivation

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

15. map function

x = [1, 2, 3]
y = map(lambda x : x + 1 , x)
print(y) //[2, 3, 4]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved