Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So i have a list:

list1 = [[1, 3, 6, 8, 9, 9, 12], [1, 2, 3, 2, 1, 0, 3, 3]]

but you can also split it into two lists, if it make it any easier. All i have to do is sum every digit with every other digit. Like you know first row:

1+1, 1+2, 1+3, 1+2, 1+1...

second:

3+1... etc.

first = [1, 3, 6, 8, 9, 9, 12]
second = [1, 2, 3, 2, 1, 0, 3, 3]

w = [x + y for x, y in zip(first, second)]

I was trying to do it in this way. But it doesn't work*, any ideas?

*i mean its summing in a wrong way, instead of every possible digits with every possible, just the first one in 1st list with 1st in second list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
384 views
Welcome To Ask or Share your Answers For Others

1 Answer

zip is getting only pairs that sit at the same index. You should instead have a double loop:

[x + y for x in first for y in second]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...