+ 1

Fix my python code, please.

I'm writing a code where i input a set of data A and a corresponding set of data B that are related. a[0] is connected to b[0] I want to know which elements in B have their corresponding A element repeated an odd number of times (3,5,7,etc). The output i want is (7, 8, 9), but instead it gives me (7, 7, 7) and i don't know why! CODE: a = [1, 1, 1] b = [7, 8, 9] for x in a: z = a.count(x) if z%2 and z>1: print(b[a.index(x)])

19th Dec 2023, 2:42 PM
Mohammad Abuhassan
Mohammad Abuhassan - avatar
2 odpowiedzi
+ 4
The problem is that a.index(x) always returns the index of the first occurrence of x. Here's the corrected code: a = [1, 1, 1] b = [7, 8, 9] for i in range(len(a)): if a.count(a[i]) % 2 and a.count(a[i]) > 1: print(b[i])
19th Dec 2023, 4:47 PM
卂ㄚㄩ丂卄
卂ㄚㄩ丂卄 - avatar
+ 1
Thank you brother
19th Dec 2023, 5:51 PM
Mohammad Abuhassan
Mohammad Abuhassan - avatar