Python - list view with multiple arguments in

I have this understanding of the current list:

... cur = [[14, k, j] for j, k in rows[14], range(15)] ... 

and this gives me the following error:

 ... cur = [[14, k, j] for j, k in rows[14], range(15)] ValueError: too many values to unpack 

Any help was appreciated as how I would fix it. I just don’t want to write the full cycle or the whole list manually. Thanks !: D

Additional Information:

 rows = [{1: '75'}, {1: '95', 2: '64'}, {1: '17', 2: '47', 3: '82'}, {1: '18', 2: '35', 3: '87', 4: '10'}, {1: '20', 2: '04', 3: '82', 4: '47', 5: '65'}, {1: '19', 2: '01', 3: '23', 4: '75', 5: '03', 6: '34'}, {1: '88', 2: '02', 3: '77', 4: '73', 5: '07', 6: '63', 7: '67'}, {1: '99', 2: '65', 3: '04', 4: '28', 5: '06', 6: '16', 7: '70', 8: '92'}, {1: '41', 2: '41', 3: '26', 4: '56', 5: '83', 6: '40', 7: '80', 8: '70', 9: '33'}, {1: '41', 2: '48', 3: '72', 4: '33', 5: '47', 6: '32', 7: '37', 8: '16', 9: '94', 10: '29'}, {1: '53', 2: '71', 3: '44', 4: '65', 5: '25', 6: '43', 7: '91', 8: '52', 9: '97', 10: '51', 11: '14'}, {1: '70', 2: '11', 3: '33', 4: '28', 5: '77', 6: '73', 7: '17', 8: '78', 9: '39', 10: '68', 11: '17', 12: '57'}, {1: '91', 2: '71', 3: '52', 4: '38', 5: '17', 6: '14', 7: '91', 8: '43', 9: '58', 10: '50', 11: '27', 12: '29', 13: '48'}, {1: '63', 2: '66', 3: '04', 4: '68', 5: '89', 6: '53', 7: '67', 8: '30', 9: '73', 10: '16', 11: '69', 12: '87', 13: '40', 14: '31'}, {1: '04', 2: '62', 3: '98', 4: '27', 5: '23', 6: '09', 7: '70', 8: '98', 9: '73', 10: '93', 11: '38', 12: '53', 13: '60', 14: '04', 15: '23'}] 
+6
source share
3 answers

You need to zip them again like this:

 cur = [[14, k, j] for j, k in zip(rows[14], range(15))] 
+8
source

To explain your code:

 cur = [[14, k, j] for j, k in rows[14], range(15)] 

matches with:

 cur = [[14, k, j] for j, k in (rows[14], range(15))] 

Now we clearly see that you created a tuple and iterate over it. For the first time, a tuple returns rows[14] through the loop, which is a dictionary that contains more than two elements, so it cannot be unpacked into j and k .

As noted by jamylak , the key is in zip two iterations together.

 cur = [[14, k, j] for j,k in zip(rows[14],range(15))] 

You can think of it as lightning:

 zip(a,b) = [ (a[0], b[0]), (a[1], b[1]), (a[2], b[2]), ... } 

you see how the structure looks like a zipper (with a and b are the left and right parts of the zipper. After you fasten, you have mapped the element on the left with the element on the right. Of course, the objects you pass to the zip are not must be indexable (all that matters is that you can iterate over them), and you can “pin” more than two iterations together ...

+4
source

@ Jamylak answer extension: alternatively you can use a map

 cur = [[14, k, j] for j, k in map(None,rows[14], range(15))] 

This will shorten the lists with None.

+1
source

Source: https://habr.com/ru/post/944035/


All Articles