Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.6 - reading the the values of rows from two lists after a conditional statement

Tags:

python

count

Hi here is my code that looks for values that repeat 3 or more times in list a:

a = ['1','1','1','2','2','3']
b = ['4','5','1','7','8','4']

d = [item for item in a if a.count(item) >= 3]

print(d)
# ['1', '1', '1']

So my question is how can I also read the corresponding values in list b. Also list a and b are always the same size. My desired output should be:

output: [['1', '1', '1'], ['4', '5', '1']]

thank you!

like image 917
F. Lee Avatar asked Mar 14 '26 00:03

F. Lee


1 Answers

You can solve this using zip:

>>> list(zip(*[(ai, bi) for ai, bi in zip(a, b) if a.count(ai) >= 3]))
[('1', '1', '1'), ('4', '5', '1')]
like image 59
Jonas Adler Avatar answered Mar 15 '26 13:03

Jonas Adler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!