3
\$\begingroup\$

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?

\$\endgroup\$
5
\$\begingroup\$

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)
\$\endgroup\$
8
  • \$\begingroup\$ Personally, I always use itertools.product whenever 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
    Apr 8 at 17:17
  • \$\begingroup\$ Just noticed the answers do not match. I retract my statement. \$\endgroup\$
    – Kevin
    Apr 8 at 17:19
  • \$\begingroup\$ @Kevin: excuse me! but what is "it" in "it.product()" \$\endgroup\$
    – Scorpisces
    Apr 8 at 23:45
  • \$\begingroup\$ @Scorpisces import itertools as it. But product() doesn't really help here. \$\endgroup\$
    – FMc
    Apr 9 at 1:07
  • 1
    \$\begingroup\$ Also you needn't write 3*j at all - you can instead move that to j in range(0, 12, 3) to avoid the multiplication. \$\endgroup\$
    – Reinderien
    Apr 10 at 4:14
1
\$\begingroup\$

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)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.