当前位置:网站首页>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 :
边栏推荐
- After unplugging the network cable, does the original TCP connection still exist?
- User login function: simple but difficult
- [C Advanced] file operation (2)
- [Chongqing Guangdong education] National Open University spring 2019 455 logistics practice reference questions
- Awk from entry to soil (5) simple condition matching
- Awk from entry to earth (7) conditional statements
- 什么是权限?什么是角色?什么是用户?
- C语言-入门-基础-语法-数据类型(四)
- China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
- How to solve the problem that computers often flash
猜你喜欢
埃氏筛+欧拉筛+区间筛
Dede plug-in (multi-function integration)
Relationship and operation of random events
C语言-入门-基础-语法-数据类型(四)
What exactly is DAAS data as a service? Don't be misled by other DAAS concepts
C language - Introduction - Foundation - syntax - data type (4)
High order phase difference such as smear caused by myopic surgery
Clion console output Chinese garbled code
Codeforces Round #803 (Div. 2)(A-D)
What if I forget the router password
随机推荐
什么是权限?什么是角色?什么是用户?
Awk from entry to earth (15) awk executes external commands
C语言-入门-基础-语法-[运算符,类型转换](六)
Basic operations of databases and tables ----- view data tables
What exactly is DAAS data as a service? Don't be misled by other DAAS concepts
C語言-入門-基礎-語法-[運算符,類型轉換](六)
Internal learning
Codeforces Round #793 (Div. 2)(A-D)
awk从入门到入土(18)gawk线上手册
【无标题】转发最小二乘法
GoLand environment variable configuration
"How to connect the network" reading notes - Web server request and response (4)
Awk from entry to earth (12) awk can also write scripts to replace the shell
"How to connect the Internet" reading notes - FTTH
HMS core helps baby bus show high-quality children's digital content to global developers
[attack and defense world | WP] cat
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
Jianzhi offer 09 realizes queue with two stacks
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
C language - Introduction - Foundation - syntax - [main function, header file] (II)