Python learning diary: day16 ------- built-in functions and anonymous functions, pythonday16 -------

Source: Internet
Author: User

Python learning diary: day16 ------- built-in functions and anonymous functions, pythonday16 -------

I. built-in functions

1. Data Type: int, bool ..........

2. Data Structure: dict, list, tuple, set, str

3, reversed -- keep the original list and return a reverse iterator

reversed()l =[1,2,3,4,5]l2 =reversed(l)print(l2)

 

4. slice slices
l =(1,2,23,213,5612,234,43)sli =slice(1,5,2)print(l[sli])

# (2,213)

5, format

print(format('test', '<20'))print(format('test', '>40'))print(format('test', '^40'))

6, bytes

# Convert bytes to bytes type
# I got the gbk encoding. I want to convert it to UTF-8 encoding.
# Print (bytes ('hello', encoding = 'gbk') # convert unicode to bytes of GBK
# Print (bytes (' ', encoding = 'utf-8') # convert unicode to UTF-8 bytes
7, bytearray
B _array = bytearray (' ', encoding = 'utf-8 ')
Print (B _array)
Print (B _array [0])
7,zip 
l = [1,2,3,4,5]l2 = ['a','b','c','d']l3 = ('*','**',[1,2])d = {'k1':1,'k2':2}for i in zip(l,l2,l3,d):    print(i)

8, all

If either of the values is false, false is returned.

9, any

True if one of them is True.

10, filter

The result set after the filter is executed must be equal to the number before execution.

# Filter only filters and does not change the original value

11. map

# The number of elements before and after execution remains unchanged, and the value may be changed

12, sorted

# Use C language. The data is small, short, and the original list must be retained. 2. Anonymous Functions
def add(x,y):    return x+y

Convert to an anonymous function:

add = lambda x,y:x+yprint(add(1,2))

1. Find the key with the maximum value

Dic = {'k1 ': 10, 'k2': 100, 'k3': 30} def func (key): return dic [key] print (max (dic, key = func) # determine the maximum value based on the returned value. The parameter with the maximum returned value is print (max (dic, key = lambda key: dic [key]) max, 3, 4, 5,-6,-7], key = abs)

2. output the final result of the Code

d = lambda p:p*2t = lambda p:p*3x = 2x = d(x) #x = 4x = t(x) #x = 12x = d(x) #x = 24print(x)#--->24

3,

# Existing two-element group ('A'), ('B'), ('C'), ('D ')),
# Use an anonymous function in python to generate a list [{'A': 'C'}, {'B': 'D'}]
 
ret = zip((('a'),('b')),(('c'),('d')))ret = map(lambda t:{t[0]:t[1]},ret)print(list(ret))
4. What is the output of the following code? Provide answers and explanations. Def multipliers (): return [lambda x: I * x for I in range (4)] print ([m (2) for m in multipliers ()]) modify the definition of multipliers to generate the expected results.
Def multipliers (): return (lambda x: I * x for I in range (4) # change to generator print ([m (2) for m in multipliers ()])

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.