Article From:https://www.cnblogs.com/yuming-python/p/9969065.html

Built-in function

1, Bytes 

str1 = 'Chinese characters'

#unicode Bytes converted to GBK
print(bytes(str1,encoding='gbk')) 

#unicode Bytes converted to GBK

print(bytes(str1,encoding='utf-8'))

# bytes Decode into characters
print(bytes(str1,encoding='gbk').decode('gbk'))

 The output is as follows:

b'\xd6\xd0\xce\xc4\xd7\xd6\xb7\xfb'
b'\xe4\xb8\xad\xe6\x96\x87\xe5\xad\x97\xe7\xac\xa6'
Chinese characters

2 bytearray  function

str1 = 'Chinese characters'
# print(bytes(str1,encoding='gbk'))
print(bytes(str1,encoding='utf-8'))
#
# print(bytes(str1,encoding='gbk').decode('gbk'))

b1 = bytearray(str1,encoding='utf-8')
print(b1)
for i in b1:
    print(format(i,'x'), end= ' ')  #Output I in hexadecimal, default I is decimal

The output is as follows:

b'\xe4\xb8\xad\xe6\x96\x87\xe5\xad\x97\xe7\xac\xa6'
bytearray(b'\xe4\xb8\xad\xe6\x96\x87\xe5\xad\x97\xe7\xac\xa6')
e4 b8 ad e6 96 87 e5 ad 97 e7 ac a6 

3 memoryview 

tr1 = 'Chinese characters'
# print(bytes(str1,encoding='gbk'))
print(bytes(str1,encoding='utf-8'))
#
# print(bytes(str1,encoding='gbk').decode('gbk'))

b1 = bytearray(str1,encoding='utf-8')
print(b1)
for i in b1:
    print(format(i,'x'), end=' ')
print()
print(memoryview(b1).hex())

The output is as follows:

b'\xe4\xb8\xad\xe6\x96\x87\xe5\xad\x97\xe7\xac\xa6'
bytearray(b'\xe4\xb8\xad\xe6\x96\x87\xe5\xad\x97\xe7\xac\xa6')
e4 b8 ad e6 96 87 e5 ad 97 e7 ac a6 
e4b8ade69687e5ad97e7aca6   # This line is a whole, it's output once.

 

4 Little knowledge: Unicode of Chinese characters – & gt; method of Chinese characters: using Eval

str2 = 'in'
str3 = ascii(str2)
print(str3)
#unicode --> str
str4 = eval(str3)
print(str4)

The output is as follows:

'\u4e2d'
in

 5 zip Method, “Zipper Method”

Sample program:

list1 = [1,2,3,4,5]
list2 =['a','b','c','d']
tu1 = ('*','**','4*')
dic1 = {'k1':'v1','k2':'v2'}

for i in zip(list1,list2,tu1,dic1):
    print(i)

The output is as follows: (Pull up zipper according to the shortest length, for dictionary, only key, no value)

(1, 'a', '*', 'k1')
(2, 'b', '**', 'k2')

6 filter function

 Example 1: Extract an odd number from a list of numbers

def is_odd(x):
    return x%2==1

list1 = [ 1,4,7,9,34,57]

ret = filter(is_odd,list1)   #filterThe function returns an iterator

for i in ret:
    print(i)

Output:

1
7
9
57

Example 2: Select a string from a list.

def is_str(x):
    return type(x) == str

ret = filter(is_str,[1,3,'hello',5,'world'])

for i in ret:
    print(i)

Output:

hello
world

7 map function

 

ret = map(abs, [1,-4,-8,9])
for i in ret:
    print(i)

 

Output:

1
4
8
9

8 Anonymous function lambda

Example: Find out the key value with the largest value in the dictionary.

dic = {'k1':10,'k2':100,'k3':30}

print(max(dic, key=lambda k : dic[k]))

 Output: K2

In anonymous functions, K is a parameter and DIC [k] is the return value of anonymous functions.

Note: Max is a function that finds the maximum value, and key = is a way to accept the comparison size.  

(The default size comparison of dictionaries is to find out which dictionary has the largest key value, and the output of Max (dic) will be k3,.

Example: Two tuples (`a’, (`b’), (`c’, (`d’), using anonymous function output [{a’: `c’}, {b’: `d’}]

tu1 = (('a'),('b'))
tu2 = (('c'),('d'))
ret = zip(tu1, tu2)
# for i in ret:
#     print(i)
# def func(tup):
#     return {tup[0]:tup[1]}
res = map(lambda tup:{tup[0]:tup[1]}, zip(tu1,tu2))
print(list(res))

 

Summary:

Others: input, print, type, hash, open, import, dir

strType code execution: eval, Exec

Numbers: bool, int, float, abs, divmod, min, max, sum, round, pow

Sequence – List and tuple related: list and tuple

Sequences – String related: str, bytes, repr

Sequence: reversed, slice

Data Sets – Dictionaries and Collections: dict, set, frozenset

Data sets: len, sorted, enumerate, zip, filter, map

 

Leave a Reply

Your email address will not be published. Required fields are marked *