简体   繁体   中英

create a new list, L3, that sums the two numbers if the number from L1 is greater than 10 and the number from L2 is less than 5

I am working on this problem and cannot seem to get it.

Here is L1 and L2 which was provided:

L1 = [1, 5, 2, 16, 32, 3, 54, 8, 100]
L2 = [1, 3, 10, 2, 42, 2, 3, 4, 3]

Beow is the solution that I came up with (L3) but it is not skipping the numbers > 10 and numbers < 5, Please help and explain!

L3 = [x1 + x2 for (x1, x2) in zip(L1, L2) if x1 > 10 or x2 < 5]

Here is the output:

Actual: [2, 8, 18, 74, 5, 57, 12, 103]
Expected: [18, 57, 103]

The only thing wrong in your code is that you should have used and for the condition instead of an or :

L3 = [x1 + x2 for (x1, x2) in zip(L1, L2) if x1 > 10 and x2 < 5]
>>> L3
[18, 57, 103]

this worked for me L3 = [(x+y) for (x,y) in zip(L1,L2) if x > 10 and y < 5 ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM