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

2 Answers 2

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\$ Commented Apr 8, 2021 at 17:17
  • \$\begingroup\$ Just noticed the answers do not match. I retract my statement. \$\endgroup\$ Commented Apr 8, 2021 at 17:19
  • \$\begingroup\$ @Kevin: excuse me! but what is "it" in "it.product()" \$\endgroup\$ Commented Apr 8, 2021 at 23:45
  • \$\begingroup\$ @Scorpisces import itertools as it. But product() doesn't really help here. \$\endgroup\$ Commented Apr 9, 2021 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\$ Commented Apr 10, 2021 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\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.