0

What should be instead of ”?” ?

This code searches through lists and sees which values the two lists have in common, what i want to be printed is the number of those common values, for example if they have 2 numbers in common[3,7] then ”2” should be printed out. https://code.sololearn.com/cTq8jia6labi/?ref=app

6th Feb 2022, 4:19 PM
Lenoname
8 Answers
+ 4
Lenoname Either print i or x. If you want number of common elements then increase counter. count = 0 for i in a : for x in g1: if x == i: count += 1 print (count) ------- You can also store common data in new list like this: newList = [] for i in a : for x in g1: if x == i : newList.append(i) print (newList) print (len(newList)) -------- If you don't want loop you can also use set: a = [6,7,3] b = [5,4,3,1,7] setA = set(a) setB = set(b) setC = setA & setB print (setC) print (len(setC))
6th Feb 2022, 4:33 PM
A͢J
A͢J - avatar
+ 3
Lenoname You can check if count > 0: print count else print 0 point
6th Feb 2022, 6:39 PM
A͢J
A͢J - avatar
+ 2
Lenoname Yes you can have but no need of elif. You can just do this without elif: if x != i : continue count+=1
6th Feb 2022, 5:02 PM
A͢J
A͢J - avatar
+ 2
Lenoname continue is use to skip the current iteration so if you don't write elif then that is also fine.
6th Feb 2022, 5:03 PM
A͢J
A͢J - avatar
+ 2
A͢J Thanks!!!it worked!! i dont know if the indentations are right though. a = [6,9,8] g1 = [5,4,3,1,7] count = 0 for i in a : for x in g1: if x == i : count+=1 if count > 0: print(count) else: print("0 point")
6th Feb 2022, 6:59 PM
Lenoname
+ 2
If you want to use list. Then there is an easy way a= [6,9,8] g1 = [5,4,3,1,7] count = 0 For i in a: if i in g1: count += 1 print(count)
8th Feb 2022, 11:26 AM
Anshuman Routray
Anshuman Routray - avatar
+ 1
A͢J can i have the if x!=i: continue Included?
6th Feb 2022, 4:56 PM
Lenoname
+ 1
A͢J lets say the programme didnt find any common values and ”0 points” should be printed, this wouldnt work: a = [6,7,3] g1 = [5,4,3,1,7] count = 0 for i in a : for x in g1: if x == i : count+=1 print(count) else: print("0 point")
6th Feb 2022, 5:13 PM
Lenoname