当前位置:网站首页>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 :
边栏推荐
- Awk from entry to earth (15) awk executes external commands
- awk从入门到入土(11)awk getline函数详解
- 什么是权限?什么是角色?什么是用户?
- Reading notes on how to connect the network - hubs, routers and routers (III)
- Awk from entry to earth (8) array
- What should I do if there is a problem with the graphics card screen on the computer
- awk从入门到入土(15)awk执行外部命令
- Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
- "How to connect the network" reading notes - Web server request and response (4)
- 埃氏筛+欧拉筛+区间筛
猜你喜欢
Opencv environment construction (I)
[C Advanced] file operation (2)
C language - Introduction - Foundation - syntax - [main function, header file] (II)
[untitled] forwarding least square method
Leetcode topic [array] -136- numbers that appear only once
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)
Sword finger offer 30 contains the stack of Min function
转:优秀的管理者,关注的不是错误,而是优势
Démarrage des microservices: passerelle
Codeforces Round #793 (Div. 2)(A-D)
随机推荐
Ehrlich sieve + Euler sieve + interval sieve
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
什么是权限?什么是角色?什么是用户?
After unplugging the network cable, does the original TCP connection still exist?
[untitled] forwarding least square method
"How to connect the network" reading notes - Web server request and response (4)
Launpad | basic knowledge
How to write unit test cases
The basic syntax of mermaid in typera
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
How to choose solid state hard disk and mechanical hard disk in computer
Awk from entry to earth (15) awk executes external commands
Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
Educational Codeforces Round 115 (Rated for Div. 2)
What exactly is DAAS data as a service? Don't be misled by other DAAS concepts
Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
awk从入门到入土(15)awk执行外部命令
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)
Reading notes on how to connect the network - tcp/ip connection (II)