The function should check two seperate lists for two identical values. If the values are identical it should save the position (=index) in a additional list. After the loop return the additional list with the indexes.
I know, pretty simple but I am a beginner :)
can somebody tell me why my output in the second print Statement is wrong?
#Output of my code:
[0, 0, 2, 3, 4]
#Expected Output:
[0, 2, 3, 5]
My code look like as following with double loop:
def same_values(lst1, lst2):
lst3 = []
for index1 in range(0,len(lst1)):
for index2 in range(0,len(lst2)):
if lst1[index1] == lst2[index2]:
lst3.append(index1)
else:
continue
return lst3
print(same_values([5, 1, -10, 3, 3, 1], [5, 10, -10, 3, 5, 1]))
Can somebody give me a hint?
CodePudding user response:
zip
and enumerate
are useful here:
def same_values(lst1, lst2):
result = []
for n, (x,y) in enumerate(zip(list1, lst2)):
if x==y:
result.append(n)
return result
The reason your code is work is that you are iterating over both lists separately, compare the following two snippets:
for x in range(3):
for y in range(3):
print(x,y)
The above will print 9 values, while you are interested in iterating over the two lists using the same index:
for x, y in zip(range(3), range(3)):
print(x,y)
This will print just 3 values.
CodePudding user response:
- Firstly, if you want to run the loop from 0 to (length of List -1), inside range you don't need to put -1, since it by default runs before the number given. For example, the below code prints: 0 1 2 3 4:
for i in range(5):
print(i)
Secondly, Your second loop runs till it finds all the matches from list1's ith index in list2's all indexes. Due to this, any duplicate matches in list2 will force list3 to be appended twice.
You can get it clear from the below code:
def same_values(lst1, lst2):
lst3 = []
for index1 in range(len(lst1)):
for index2 in range(len(lst2)):
if lst1[index1] == lst2[index2]:
lst3.append(index1)
print("List1 index number ",index1," value : ",lst1[index1]," matches List2 index number:",index2)
else:
continue
return lst3
print(same_values([5, 1, -10, 3, 3, 1], [5, 10, -10, 3, 5, 1]))
Output:
List1 index number 0 value : 5 matches List2 index number: 0
List1 index number 0 value : 5 matches List2 index number: 4
List1 index number 1 value : 1 matches List2 index number: 5
List1 index number 2 value : -10 matches List2 index number: 2
List1 index number 3 value : 3 matches List2 index number: 3
List1 index number 4 value : 3 matches List2 index number: 3
List1 index number 5 value : 1 matches List2 index number: 5
[0, 0, 1, 2, 3, 4, 5]
- Lastly, if you want to store the index uniquely, then you should break the second loop once you find a match as shown below:
def same_values(lst1, lst2):
lst3 = []
for index1 in range(len(lst1)):
for index2 in range(len(lst2)):
if lst1[index1] == lst2[index2]:
lst3.append(index1)
print("List1 index number ",index1," value : ",lst1[index1]," matches List2 index number:",index2)
break
else:
continue
return lst3
Output:
List1 index number 0 value : 5 matches List2 index number: 0
List1 index number 1 value : 1 matches List2 index number: 5
List1 index number 2 value : -10 matches List2 index number: 2
List1 index number 3 value : 3 matches List2 index number: 3
List1 index number 4 value : 3 matches List2 index number: 3
List1 index number 5 value : 1 matches List2 index number: 5
[0, 1, 2, 3, 4, 5]
CodePudding user response:
Use a simple list comprehension instead of loops (and you do not need 2 loops):
def same_values(lst1, lst2):
return [i for i in range(len(lst1)) if lst1[i] == lst2[i]]
print(same_values([5, 1, -10, 3, 3, 1], [5, 10, -10, 3, 5, 1]))
# [0, 2, 3, 5]