I have a list of key:
list_date = ["MON", "TUE", "WED", "THU","FRI"]
I have many lists of values that created by codes below:
list_value = list()
for i in list(range(5, 70, 14)):
list_value.append(list(range(i, i+10, 3)))
Rules created that:
first number is 5, a list contains 4 items has value equal x = x + 3, and so on [5, 8, 11, 14]
the first number of the second list equal: x = 5 + 14, and value inside still as above x = x +3
[[5, 8, 11, 14], [19, 22, 25, 28], [33, 36, 39, 42], [47, 50, 53, 56], [61, 64, 67, 70]]
I expect to obtain a dict like this:
collections = {"MON":[5, 8, 11, 14], "TUE" :[19, 22, 25, 28], "WED":[33, 36, 39, 42], "THU":[47, 50, 53, 56], "FRI":[61, 64, 67, 70]}
Then, I used:
zip_iterator = zip(list_date, list_value)
collections = dict(zip_iterator)
To get my expected result.
I tried another way, like using the lambda function.
for i in list(range(5, 70, 14)):
list_value.append(list(range(i,i+10,3)))
couple_start_end[lambda x: x in list_date] = list(range(i, i + 10, 3))
And the output is:
{<function <lambda> at 0x000001BF7F0711F0>: [5, 8, 11, 14], <function <lambda> at 0x000001BF7F071310>: [19, 22, 25, 28], <function <lambda> at 0x000001BF7F071280>: [33, 36, 39, 42], <function <lambda> at 0x000001BF7F0710D0>: [47, 50, 53, 56], <function <lambda> at 0x000001BF7F0890D0>: [61, 64, 67, 70]}
I want to ask there is any better solution to create lists of values with the rules above? and create the dictionary collections without using the zip method?
2 Answers 2
One alternative approach is to take advantage of enumerate() -- which allows
you to iterate over what could be thought of as a zip() of a list's indexes and
values. To my eye, this version seems a bit clearer and simpler.
days = ['MON', 'TUE', 'WED', 'THU', 'FRI']
d = {
day : [
(5 + i * 14) + (j * 3)
for j in range(4)
]
for i, day in enumerate(days)
}
print(d)
-
\$\begingroup\$ Personally, I always use
itertools.productwhenever there is a cartesian product involved:d = {day: (5 + i * 14) + (j * 3) for i,(j,day) in it.product(range(4), enumerate(days))}. \$\endgroup\$Kevin– Kevin2021年04月08日 17:17:37 +00:00Commented Apr 8, 2021 at 17:17 -
\$\begingroup\$ Just noticed the answers do not match. I retract my statement. \$\endgroup\$Kevin– Kevin2021年04月08日 17:19:14 +00:00Commented Apr 8, 2021 at 17:19
-
\$\begingroup\$ @Kevin: excuse me! but what is "it" in "it.product()" \$\endgroup\$Scorpisces– Scorpisces2021年04月08日 23:45:54 +00:00Commented Apr 8, 2021 at 23:45
-
\$\begingroup\$ @Scorpisces
import itertools as it. Butproduct()doesn't really help here. \$\endgroup\$FMc– FMc2021年04月09日 01:07:35 +00:00Commented Apr 9, 2021 at 1:07 -
1\$\begingroup\$ Also you needn't write
3*jat all - you can instead move that toj in range(0, 12, 3)to avoid the multiplication. \$\endgroup\$Reinderien– Reinderien2021年04月10日 04:14:24 +00:00Commented Apr 10, 2021 at 4:14
Another way that, as FMc's, has the advantage of not having to calculate/specify range limits (your 70 and i+10):
from itertools import count, islice
days = ['MON', 'TUE', 'WED', 'THU', 'FRI']
d = {day: list(islice(count(start, 3), 4))
for day, start in zip(days, count(5, 14))}
print(d)