当前位置:网站首页>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 :

边栏推荐
- HMS core helps baby bus show high-quality children's digital content to global developers
- The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
- go-zero微服务实战系列(九、极致优化秒杀性能)
- The upper layer route cannot Ping the lower layer route
- Solve the problem of "Chinese garbled MySQL fields"
- Reading notes of how the network is connected - understanding the basic concepts of the network (I)
- 老掉牙的 synchronized 锁优化,一次给你讲清楚!
- C语言-入门-基础-语法-[变量,常亮,作用域](五)
- From scratch, use Jenkins to build and publish pipeline pipeline project
- Awk from getting started to digging in (4) user defined variables
猜你喜欢

User login function: simple but difficult

How college students choose suitable computers
](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)

Mantis creates users without password options

L1 regularization and L2 regularization

The basic syntax of mermaid in typera

埃氏筛+欧拉筛+区间筛

Turn: excellent managers focus not on mistakes, but on advantages
](/img/5a/c6a3c5cd8038d17c5b0ead2ad52764.png)
C语言-入门-基础-语法-[主函数,头文件](二)

Solve the problem of "Chinese garbled MySQL fields"
随机推荐
Implementation principle of redis string and sorted set
Educational Codeforces Round 119 (Rated for Div. 2)
Internal learning
How do microservices aggregate API documents? This wave of show~
swatch
C语言-入门-基础-语法-[主函数,头文件](二)
Solve the problem of "Chinese garbled MySQL fields"
Awk from entry to earth (15) awk executes external commands
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
The basic syntax of mermaid in typera
Review of last week's hot spots (6.27-7.3)
In depth investigation and Strategic Research Report on China's motion controller Market (2022 Edition)
There are 100 people eating 100 apples, one adult eating 4 apples, and four children eating 1 apple. How can they eat exactly 100 apples? Use any high-level language you are familiar with
Opencv environment construction (I)
awk从入门到入土(4)用户自定义变量
Codeforces Round #793 (Div. 2)(A-D)
awk从入门到入土(12)awk也可以写脚本,替代shell
What if I forget the router password
[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-
Use Alibaba cloud NPM image acceleration