当前位置:网站首页>Use of zip/enumerate/map function
Use of zip/enumerate/map function
2022-07-18 12:58:00 【Climbing snail】
zip/enumerate/map Use of functions
zip function
zip Function to package multiple iteratible objects , If the number of iteratible objects is different , Take the least .zip Unpacking is also a zip type , Need to cast to list or tuple type , The element after strong conversion is of tuple type
example :
pack
# Package into a list
li1 = ['a', 'b', 'c']
li2 = [1, 2, 3]
res = list(zip(li1, li2))
print(type(res))
print(res)
# Pack it up as a dictionary
li1 = ['a', 'b', 'c']
li2 = [1, 2, 3]
res = dict(zip(li1, li2))
print(type(res))
print(res)
example 2:
Unpack
# Unpack list
li1 = ['a', 'b', 'c']
li2 = [1, 2, 3]
res = list(zip(li1, li2))
print(type(res))
print(res)
print(' The following result is unpacking '.center(50, '*'))
res2 = list(zip(*res))
print(type(res2))
print(type(res2[0])) # First element
print(res2)
# Unpacking dictionary
li1 = ['a', 'b', 'c']
li2 = [1, 2, 3]
res = dict(zip(li1, li2)) # Pack it up as a dictionary
print(type(res))
print(res)
print(' The following result is unpacking '.center(50, '*'))
res2 = list(zip(*res.items())) # adopt items(), Turn the dictionary into a tuple of key value pairs , Then install it and replace it with list
print(type(res2))
print(type(res2[0])) # First element
print(res2)
enumerate function
He can transform an iteratable object into a enumerate object , Generally used for subscript , The default from the 0 Start
grammar :
enumerate( Iteratable object , Starting position )
example :
li1 = ['a', 'b', 'c']
for key, val in enumerate(li1, 10): # The default from the 0 Start , Specify from 10 Start
print(key, val)
map function
Traversing an iteratable object , Pass each element into a function for processing
grammar :
map( Function name , Iteratable object )
example :
li = [3, 4, 9]
def func(x):
return x ** 2
res = list(map(func, li))
print(res)
# Using anonymous functions
li = [3, 4, 9]
res = list(map(lambda x: x ** 2, li))
print(res)
边栏推荐
猜你喜欢
随机推荐
Record root file system image img production
揭秘MetaMask的起源:成为入门Web3文化的工具集
【Golang | gRPC】gRPC-Server Streaming服务端流实战
[flag]小程序-day2
[flag] applet -day2
TCP IP ICMP Ethernet frame format
AIRIOT答疑第4期|如何使用数据分析引擎?
Maximum return alternative method
Data table design and API data construction of three-level classification
Typescript之常量与对象冻结
还在用命令行看日志?快用Kibana吧,可视化日志分析YYDS ~
Qt编写物联网管理平台43-告警短信转发
ES6浏览器支持及运行环境支持检测及ES6转码ES5
Airiot low code development platform, 10 minutes to build the Internet of things system
Banned, off the shelf! Wechat has made a move to standardize and renovate the digital collection platform!
From application to bottom: 36 pictures take you into redis world (Part 1)
Summary of common JVM interview questions
Etherscan:熊市中的一些重要图表
[full text 7000 words] SF city test development side 49min answer
6000 Digital Collections Online seconds empty! "National treasure" digital collections look like this





![P1765 mobile phone [getting started]](/img/67/cdaad41abf5f1b1586e8919e4179e3.png)



