Skills of meeting each other late in Python of remember to collect

  • 2021-10-27 07:50:01
  • OfStack

Directory 1. Exchange variable values
2. Combine all the elements in the list into a string
3. Find the most frequent value in the list
4. Check whether two strings are composed of the same letters out of order 5. Invert the string, list
6. Transpose 2-dimensional arrays 7. Chain comparison
8. 3 yuan operator
9. Chain function calls 10. for-else syntax
11. Merge dictionaries 12. Remove repeating elements from lists 13. **kwargs
14. Table derivation
15. map function

Don't say much, go straight to work, save for a long time!

1. Exchange variable values

This should be relatively simple, but it is easy to ignore it for daily use.


a, b = 5, 10
print(a, b)   //5, 10
a, b = b, a
print(a, b)   //10, 5

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

This is actually a basic grammar


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

3. Find the most frequent value in the list

Does it feel great to brush algorithm problems with Python?


a = [1, 1, 1, 2, 3, 3, 3, 3, 4, 4, 4]
print(max(set(a), key = a.count))

4. Check whether two strings are made up of the same letters out of order


from collections import Counter

a = 'abcdefg'
b = 'adcbgfb'
print(Counter(a) == Counter(b))

5. Invert strings, lists

This can be achieved in one sentence with Java


a = 'dadabjdnakdmnkafad'
print(a[::-1])

num = 1343453535
print(int(str(num)[::-1]))

a = [1,3,554,64,2]
print(a[::-1])

6. Transpose 2-D arrays


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

7. Chain comparison

This comparison is in line with the mathematical comparison habit


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

8. 3 yuan operator

In fact, there is no 3 yuan operator in Python, but we can replace it in another way:


b = 'B'
c = 'C'
flag = True
a = b if flag else c

9. Chain function calls


def product(a, b):
   return a * b

def add(a, b):
   return a + b

b = True
print((product if b else add)(5 ,7))

10. for-else Syntax

Note that instead of if-else, the for loop can use else:


a = [1, 2, 3, 4, 5]
for el in a:
    if(el == 0)
       print(' Find  0  It's over ')
else:
    print(' Not found  0')

11. Merging dictionaries


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

12. Remove duplicate elements from a list


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

13. **kwargs

Variable length parameter, is a dictionary.

A double asterisk in front of a dictionary object allows you to enter the contents of the dictionary as a named parameter into a function. The secret key of the dictionary is the parameter name, and the value is the value passed to the function. You don't even need to call it kwargs!


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

14. Table derivation

You can manipulate the data in the complete list with 1 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]

15. map function


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

The above is the Python skills (remember to collect) in the details, more information about python skills please pay attention to other related articles on this site!


Related articles: