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
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))
+ 3
Lenoname
You can check if count > 0: print count else print 0 point
+ 2
Lenoname
Yes you can have but no need of elif. You can just do this without elif:
if x != i :
continue
count+=1
+ 2
Lenoname
continue is use to skip the current iteration so if you don't write elif then that is also fine.
+ 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")
+ 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)
+ 1
A͢J can i have the
if x!=i: continue
Included?
+ 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")