当前位置:网站首页>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 (7) conditional statements
- 20220701 Barbalat引理证明
- Démarrage des microservices: passerelle
- awk从入门到入土(5)简单条件匹配
- Reading notes of how the network is connected - understanding the basic concepts of the network (I)
- 上周热点回顾(6.27-7.3)
- 随机事件的关系与运算
- Lauchpad X | 模式
- The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
- Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
猜你喜欢
](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
C语言-入门-基础-语法-[运算符,类型转换](六)
![Leetcode topic [array] -136- numbers that appear only once](/img/6d/f2e4b812e5dd872fbeb43732d6f27f.jpg)
Leetcode topic [array] -136- numbers that appear only once

微服务入门:Gateway网关

How can we make a monthly income of more than 10000? We media people with low income come and have a look

Horizon sunrise X3 PI (I) first boot details

How to ensure the uniqueness of ID in distributed environment

Turn: excellent managers focus not on mistakes, but on advantages

Comparison between sentinel and hystrix
](/img/5a/c6a3c5cd8038d17c5b0ead2ad52764.png)
C语言-入门-基础-语法-[主函数,头文件](二)

Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
随机推荐
Educational Codeforces Round 119 (Rated for Div. 2)
C语言-入门-基础-语法-[运算符,类型转换](六)
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
Codeforces Round #793 (Div. 2)(A-D)
awk从入门到入土(4)用户自定义变量
awk从入门到入土(12)awk也可以写脚本,替代shell
Awk from entry to earth (8) array
A subclass must use the super keyword to call the methods of its parent class
Developers really review CSDN question and answer function, and there are many improvements~
go-zero微服务实战系列(九、极致优化秒杀性能)
1211 or chicken and rabbit in the same cage
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
Awk from entry to earth (12) awk can also write scripts to replace the shell
System disk expansion in virtual machine
"How to connect the network" reading notes - Web server request and response (4)
How to re enable local connection when the network of laptop is disabled
Jianzhi offer 09 realizes queue with two stacks
随机事件的关系与运算
In depth investigation and Strategic Research Report on China's motion controller Market (2022 Edition)
Awk from digging into the ground to getting started (10) awk built-in functions