0
Follow
0
View

Python dictionary derivation

curenzhao 注册会员
2023-02-27 11:24

A key in a dictionary will not be repeated, and will be overwritten when the same key is assigned a second time.
Back to code: d={x:y for x in [1,3,5] for y in [2,4,6]}
x traverses once, y traverses 3 times,

    < li > when x = 1, the change of d: < br / > d = {1} < br / > d = {and} < br / > d = {for} < / li > < li > when x = 3, the variation regularity of d: < br / > d = {1, 2} < br / > d = {1:4, 3:4} < br / > d = {} and set 1:6, < / li > < li > when x = 5, the variation regularity of d: < br / > d = {1, 3:2, 5-2} < br / > d = {1:4, 3:4, five} < br / > d = {1:6, set, but} < br / > so the end result is: {12 1:6, 3:6,} < blockquote >

    If it helps, please click to accept ~

duxinyu126 注册会员
2023-02-27 11:24

This answer quotes ChatGPT

This code is a dictionary derivation to create a dictionary where the key is the element in the list [1, 3, 5] and the value is the last element 6 in the list [2, 4, 6].

Specifically, the syntax of a dictionary derivation is {key_expression: value_expression for item in iterable}, where key_expression and value_expression are arbitrary expressions, and item is the element in iterable.

In this example, key_expression is x, value_expression is y, and item is the elements in the list [1,3, 5], i.e., 1, 3, and 5. In each iteration, y is the element in the list [2,4, 6], i.e. 2, 4, and 6, so a key-value pair is generated in each iteration, where the keys are 1,3, and 5, and the value is 6, because the last element in the list [2, 4, 6] is 6.

Therefore, the resulting dictionary is {1:6, 3:6, 5:6}. Note that since the key must be unique, there are only three keys in this dictionary, namely 1,3, and 5, but they all have the value 6.

csljxs 注册会员
2023-02-27 11:24

When the program runs, run the first loop, x is 1,3,5. This is the key, and the y loop is the key. You get 2 as all the keys, you get 4, you replace 2, and so on, until you get 6.
{1:2,3:2,5:2} {1:4,3:4,5:4} {1:6,3:6,5:6}. Key change

dbafj1314 注册会员
2023-02-27 11:24

Little Witch references parts of bing and GPT :
Dictionary derivation is a concise expression in Python that can be used to create new dictionaries. You don't need to use a for loop. In the code above, we create a new dictionary d whose keys are elements in [1,3,5] and whose values are elements in [2,4,6].

In dictionary comprehensions, we use the for statement to loop through the elements in the list, and then add those elements as keys and values to the newly created dictionary. Here, we first loop through the [1,3,5] lists, using them as the keys to the dictionary; Then loop through the list [2,4,6] as dictionary values.

Therefore, the resulting dictionary d is {1:6, 3.6, 5.6}. This is because for every element x in [1,3,5], a last element y is taken from [2,4,6] as its value. Since there are three elements in [2,4,6], the resulting dictionary d has all keys corresponding to the last value of 6.
The answer is not easy, so take it.

roz0803 注册会员
2023-02-27 11:24

The dictionary cannot contain a key with the same name. The second time it appears will overwrite the first value, so the value will always be 6.

12 14 16
32 34 36
52 54 56

will only keep the result after.

About the Author

Question Info

Publish Time
2023-02-27 11:24
Update Time
2023-02-27 11:24

Related Question

我如何从我的PyQt5 UI输入传递数据到我的python程序?

Python图:如何找到源节点和目标节点之间的所有可能路径

python的while循环问题

如何在python中为加载的.pkl模型提供输入

Python multiprocessing Queue为什么判断队列一直是空的呢?

样条曲线在python使用splprep和脾

Python的列表遍历

SOAPProxy报错,如何解决?(语言-python)

"startfile()"函数在Python 3.9.7中无效[重复]

Python共享单车用户租凭数据分析