Commonly used functions and tricks in Python

1. Read the data split and put it into the list

Usage scenario: In general development, data is often read from local files and placed in the list,
for example: 10086 , 10010, 110, 120, 999

>>> str = '10086,10010,110,120,999'
>>> data = [x for x in str.strip().split(',')]
>>> print data
['10086', '10010', '110', '120', '999']
>>> 

2. The function str.format() formats the string

Usage scenario: generally used in the combination of variables and strings

Usage 1:

>>> name = 'mile' 
>>> age = '2'
>>>> str = 'name:{},age:{}'.format(name,age)
>>> print str
name:mile,age:2

Usage 2:

>>> data = ['mile',2]
>>>> str = "My son's name is {0[0]}, this year {0[1]} years old, had {0[1]} birthday".format(data)
>>> print str
My son's name is mile, this year 2 years old, had 2 birthday

Usage 3:
Padding and Alignment

Padding is often used together with alignment.
^, <, > are centered, left-aligned, right-aligned, followed by a width
: The character with padding after the sign can only be one character. If it is not specified, it will be filled with spaces by default.

>>> '{:0^8}'.format('86')
'00086000'
>>> '{:0>4}'.format('86')
'0086'
>>> '{:0<4}'.format('86')
'8600'

Usage 4:
Precision is often used with type f

>>> '{:.3f}'.format(3.141592653589793238462643383249901429)
'3.142'

Looking at the example, it is automatically rounded here.

Usage 5: Use
with base b, d, o, x are binary, decimal, octal, hexadecimal respectively.

>>> '{:b}'.format(6)
'110'
>>> '{:d}'.format(110)
'110'
>>> '{:o}'.format(110)
'156'
>>> '{:x}'.format(110)
'6e'

Usage 6:
Thousands separator that can be used to separate numbers

>>> '{:,}'.format(2342342342342342342)
'2,342,342,342,342,342,342'

3. Remove the characters on the left and right of the string str.rstrip() str.lstrip()

Example 1:

>>> str = '000000099999999000000'
>>> str.rstrip('0')
'000000099999999'
>>> str.lstrip('0')
'99999999000000'

Example 2: No content is added to the pattern to remove spaces

>>> str = '   i have a dream   '
>>> str.lstrip()
'i have a dream   '

Fourth, list sorting

>>> data = [1,4,2,5,3,7,0,2,4]
>>> data.sort(key=lambda x :x)
>>> print data
[0, 1, 2, 2, 3, 4, 4, 5, 7]

Strings in python list are sorted according to the first

>>> data = ['1','9','0','2','4','8']
>>> data.sort(key = lambda x :x)
>>> print data
['0', '1', '2', '4', '8', '9']
>>> data = ['1','9','0','200','49','8']
>>> data.sort(key = lambda x :x)
>>> print data
['0', '1', '200', '49', '8', '9']

Five, lambda and filter combined filtering

The lambda function is used to sort above, and the lambda and filter functions are used below to filter

Example 1: Filter out the list greater than 5

>>> data = [1,9,0,200,49,8]
>>>> data = filter(lambda x:x>5,data)
>>> print data
[9, 200, 49, 8]

Example 2: If the parameter of filter is None, delete 0, or False, and leave non-0 and true

>>> data = [1,9,0,200,49,8,0,-2,False,True]
>>> data = filter(None,data)
>>> print data
[1, 9, 200, 49, 8, -2, True]

Six, map () function

I can't tell what I'm doing with this specific language, so let's look at the code directly.

Example 1:

>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> def f(x):
...     return x+1
... 
>>> print map(f,data)
[2, 3, 4, 5, 6, 7, 8, 9, 10]

From the code point of view, the map function adds 1 to each element in the data according to the f method

Example 2: map() combined with lambda

>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> data = map(lambda x:x+3,data)
>>> print data
[4, 5, 6, 7, 8, 9, 10, 11, 12]

Example 3:
Input: ['adam', 'LISA', 'barT']
Output: ['Adam', 'Lisa', 'Bart']

>>> def format_name(s):
...     return s[0].upper()+s[1:].lower()
... 
>>> print map(format_name,['adm','LISA','braT'])
['Adm', 'Lisa', 'Brat']

Seven, reduce () function

Example 1:

>>> reduce(lambda x,y:x*y,[1,2,3,4,5])
120

The result is obtained from: 1*2*3*4*5 = 120

Eight, cutting array

Data[:size] or Data[size:]

>>> data = ['1','2','4','5','6']
>>> print data[:3]
['1', '2', '4’]
>>> print data[3:]
['5','6']

Nine, re.sub regular replacement

re.sub is a regular expression function, which is used to achieve a more powerful replacement function than the replacement of ordinary strings through regular expressions.

Example 1:

>>> str = 'hello 123 world 456'
>>> import re
>>>> replaceStr = re.sub('\d+','222',str)
>>> print replaceStr
hello 222 world 222

To be continued. . . . . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325710713&siteId=291194637