Putting multiple element values in a for statement

Asked 2 years ago, Updated 2 years ago, 14 views

a = ["Eulji"]
b = ["2"]
c = ["10","20","30","80"]
d = ["31","52","33","52"]
{Region: "Gangnam", Person: "2", Time: "10", Land number: "31"}
{Region: "Gangnam", Person: "2", Time: 20", Land number: "52"}
{Region: "Gangnam", Person: "2", Time: "30", Land number: "33"}
{Region: "Gangnam", Person: "2", Time: "80", Land number: "33"}

Hello, I had a problem while studying by myself Like the above, when there is a list called ab cd, turn the for door to the value in the dictionary I want to put it in like above, but if I use the zip, the value at the end of the c and d list is cut, so I'm asking you a question How else should we approach it? Is there any other way besides the for door? Help me!

python

2022-09-22 19:19

1 Answers

There can be many ways. Do it yourself first.

It is a problem that can be implemented if the for statement is used as a superposition anyway.

In [1]: import itertools as it

In [2]: a = ["Eulji"]
   ...: ...: b = ["2"]
   ...: ...: c = ["10","20","30","80"]
   ...: ...: d = ["31","52","33","52"]

In [3]: for row in it.product(a, b, zip(c, d)):
   ...:     print ({'region':row[0], 'person':row[1], 'time':row[2][0], 'lot number':row[2][1]})
   ...:
{'Region': 'Eulji', 'Person': '2', 'Time': '10', 'Location': '31'}
{'Region': 'Eulji', 'Person': '2', 'Time': 20', 'Location number': '52'}
{'Region': 'Eulji', 'Person': '2', 'Time': '30', 'Location': '33'}
{'Region': 'Eulji', 'Person': '2', 'Time': '80', 'Location': '52'}


2022-09-22 19:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.