I have following info tuple, how do i find the index of the of tuple[?] from name tuple?
info = [('Water SPF 15', '12B', '#f2c9b5', 31, '-100.1', '-100.1', '-100.1', '12N', '', '', '27B'), ('Water Spectrum SPF 15', '12B', '#f2c9b5', 31, '7..7', '7..7', '7..7', '12N', '', '', '27B'), ('Water Foundation SPF 15', '12N', '#e5be9d', 44, ' ', ' ', ' ', '', '12B', '', '15N'), ('FACE15', '12N', '#e5be9d', 44, '**', '**', '**', '12S', '14H', '', '16N'), ('FACE', '12B', '#f2c9b5', 31, 'asd', 'asd', 'asd', '14H', '', '8B', '18B'), ('hydrator', '10N', '#e5be9d', 44, '', '', '', '', '', '', '13N'), ('APE', '12B', '#e7cbb3', 39, '-100', '-100', '-100', '', '', '', '')]
name = [('Water Spectrum SPF 15', '12B', '#f2c9b5'),('FACE15', '12N', '#e5be9d')]
matched_index = []
n = 0
for i in info :
if name[n][0] and name[n][1] and name[n][2] == i[n][0] and i[n][1] and i[n][2]:
print("match")
matched_index.append(n)
n = n + 1
else:
n = n + 1
print("DONE")
Is there a way to match only the first 3 elements like above? I will get error when n reached 3...
something like the below
infos = [('Water SPF 15', '12B', '#f2c9b5', 31, '-100.1', '-100.1', '-100.1', '12N', '', '', '27B'),
('Water Spectrum SPF 15', '12B', '#f2c9b5', 31, '7..7', '7..7', '7..7', '12N', '', '', '27B'),
('Water Foundation SPF 15', '12N', '#e5be9d', 44, ' ', ' ', ' ', '', '12B', '', '15N'),
('FACE15', '12N', '#e5be9d', 44, '**', '**', '**', '12S', '14H', '', '16N'),
('FACE', '12B', '#f2c9b5', 31, 'asd', 'asd', 'asd', '14H', '', '8B', '18B'),
('hydrator', '10N', '#e5be9d', 44, '', '', '', '', '', '', '13N'),
('APE', '12B', '#e7cbb3', 39, '-100', '-100', '-100', '', '', '', '')]
names = [('Water Spectrum SPF 15', '12B', '#f2c9b5'), ('FACE15', '12N', '#e5be9d')]
for name in names:
for idx, info in enumerate(infos):
if name[0] == info[0] and name[1] == info[1] and name[2] == info[2]:
print(f'{name} {idx}')
output
('Water Spectrum SPF 15', '12B', '#f2c9b5') 1
('FACE15', '12N', '#e5be9d') 3
You could also consider using list comprehension which is preferred over traditional looping in Python as:
Get only indices:
lst = [idx for idx, info in enumerate(infos) for name in names if name == info[:3]]
print(lst)
Output: [1, 3]
As a list of dictionary:
lst = [{name:idx} for idx, info in enumerate(infos) for name in names if name == info[:3]]
print(lst)
Output:
[{('Water Spectrum SPF 15', '12B', '#f2c9b5'): 1}, {('FACE15', '12N', '#e5be9d'): 3}]
You can avoid nested loops by first converting the tuples in info
into a dictionary keyed by the first 3 items of each tuple with its index as the value. Then a simple dictionary lookup provides the results. Overall that will give a run time complexity of O(n) vs O(n 2 ) for a nested loop:
info = [('Water SPF 15', '12B', '#f2c9b5', 31, '-100.1', '-100.1', '-100.1', '12N', '', '', '27B'), ('Water Spectrum SPF 15', '12B', '#f2c9b5', 31, '7..7', '7..7', '7..7', '12N', '', '', '27B'), ('Water Foundation SPF 15', '12N', '#e5be9d', 44, ' ', ' ', ' ', '', '12B', '', '15N'), ('FACE15', '12N', '#e5be9d', 44, '**', '**', '**', '12S', '14H', '', '16N'), ('FACE', '12B', '#f2c9b5', 31, 'asd', 'asd', 'asd', '14H', '', '8B', '18B'), ('hydrator', '10N', '#e5be9d', 44, '', '', '', '', '', '', '13N'), ('APE', '12B', '#e7cbb3', 39, '-100', '-100', '-100', '', '', '', '')]
name = [('Water Spectrum SPF 15', '12B', '#f2c9b5'),('FACE15', '12N', '#e5be9d')]
lookup = {t[:3]: i for i,t in enumerate(info)}
for n in name:
if n in lookup:
print(n, lookup[n])
Output:
('Water Spectrum SPF 15', '12B', '#f2c9b5') 1
('FACE15', '12N', '#e5be9d') 3
If you only want the indices stored in a list:
lookup = {t[:3]: i for i,t in enumerate(info)}
matched_index = [lookup[n] for n in name if n in lookup]
print(matched_index)
Output
[1, 3]
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.