当前位置:网站首页>If you can quickly generate a dictionary from two lists
If you can quickly generate a dictionary from two lists
2022-07-04 09:02:00 【Happy learning Python】
Built in functions zip():
Used to take iteratable objects as parameters , Package the corresponding elements in the object into a tuple , Then return a list of these tuples .
items = ['Fruits','Books','Others']
prices = [96,78,85]
lst = dict(zip(items,prices))
print(lst)
The above code passes zip() Function pair items and prices Pack two lists , And adjust the result to the dictionary type to assign lst, The printouts are as follows :
If the third line of code , We will not zip() The result of is adjusted to dict( Dictionaries ) type , Can you output the dictionary directly ?
items = ['Fruits','Books','Others']
prices = [96,78,85]
lst = zip(items,prices)
print(lst)
The input result is as follows :
Directly through zip() Function does not directly get a dictionary , because zip() function , What is returned is a traversal object , use for Yes zip() The result is a traversal output :
items = ['Fruits','Books','Others']
prices = [96,78,85]
lst = zip(items,prices)
for i in lst:
print(i)
Output is as follows :
You can also generate dictionaries in the following ways :
items = ['Fruits','Books','Others']
prices = [96,78,85]
lst = {item:price for item, price in zip(items,prices)}
print(lst)
Output is as follows :
边栏推荐
- 《网络是怎么样连接的》读书笔记 - FTTH
- std::is_ union,std::is_ class,std::integral_ constant
- Sword finger offer 30 contains the stack of Min function
- Sequence model
- Internal learning
- ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
- Cancel ctrl+alt+delete when starting up
- Awk from entry to earth (8) array
- [Chongqing Guangdong education] National Open University spring 2019 455 logistics practice reference questions
- Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
猜你喜欢
CLion-控制台输出中文乱码
What if the wireless network connection of the laptop is unavailable
Comparison between sentinel and hystrix
How to play dapr without kubernetes?
GoLand environment variable configuration
地平线 旭日X3 PI (一)首次开机细节
User login function: simple but difficult
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
How college students choose suitable computers
Getting started with microservices: gateway gateway
随机推荐
[untitled] forwarding least square method
What is inner connection and outer connection? What are the uses and benefits
C语言-入门-基础-语法-[运算符,类型转换](六)
After unplugging the network cable, does the original TCP connection still exist?
[Chongqing Guangdong education] National Open University spring 2019 455 logistics practice reference questions
Codeforces Round #803 (Div. 2)(A-D)
Internal learning
Manjaro install wechat
How to play dapr without kubernetes?
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Bishi blog (13) -- oral arithmetic test app
C语言-入门-基础-语法-[变量,常亮,作用域](五)
Explain TCP protocol in detail three handshakes and four waves
A subclass must use the super keyword to call the methods of its parent class
Codeforces Round #793 (Div. 2)(A-D)
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
Launpad | basic knowledge
C language - Introduction - Foundation - syntax - data type (4)