I have a set of strings in a list, the list is given below in the code. I would like to compare each string to its previous strings. Obviously the first positioned string will not be compared with previous string since there isn't any. The logic is basically: 2nd positioned string to be compared with 1st positioned string, 3rd positioned string to be compared with 1st and 2nd positioned string, ... ...
s = ["avocado", "banana", "carrot", "avocado", "carrot", "grapes", "orange"]
for i in range(2,len(s)):
for j in range(i,2, -1):
if s[i] == s[j]:
print (s[i])
Now, if there is a match found, the string name with the positions will be shown. Such as avocado found in position 4 and 1
. I am stuck at this code. How should I proceed?
Another approach is that you could make a dictionary of the items to positions
from collections import defaultdict
d = defaultdict(list)
for i in range(len(s)):
d[s[i]].append(i + 1) # '+ 1' since we count from 1-index
for item, positions in d.items():
if len(positions) > 1:
print("{} found at positions {}".format(item, positions))
s = ["avocado", "banana", "carrot", "avocado", "carrot", "grapes", "orange"]
for i in range(1, len(s)):
for j in range(i):
if s[i] == s[j]:
print(s[i])
You were close. Use range(i)
to count from 0 to i. Use an index of 1 to get the second item in the list (lists start at 0).
This seems to fit a general use case:
a = ['a','b','c','d','e','a','b','e','d']
for i in list(set(a)):
b = [j for j, e in enumerate(a) if e == i]
if len(b) > 1:
print(i," found in positions:",b )
Output:
b found in positions: [1, 6]
a found in positions: [0, 5]
d found in positions: [3, 8]
e found in positions: [4, 7]
First, you have to determine the positions of all words, eg in a dictionary. Then you can print the words with positions:
from collections import defaultdict
positions = defaultdict(list)
s = ["avocado", "banana", "carrot", "avocado", "carrot", "grapes", "orange"]
for position, word in enumerate(s):
positions[word].append(position)
for word, position in positions.items():
print(f"{word} found at positions {' and '.join(position)}")
You can use the combinations function from itertools to efficiently go through all pairs:
s = ["avocado", "banana", "carrot", "avocado", "carrot", "grapes", "orange"]
from itertools import combinations
matches = [(w1,p1,p2) for (p1,w1),(p2,w2) in combinations(enumerate(s),2) if w1==w2]
print("\n".join(f"{word} found in position {p1} and {p2}" for word,p1,p2 in matches))
# avocado found in position 0 and 3
# carrot found in position 2 and 4
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.