python - Compare only part of two of three items in triple -


i have list goes this, , new content added in loop.

list = [("banana", "a", 0), ("banana", "b", 1), ("coconut", "a", 2)] 

in loop want add items so:

list.append(("strawberry", "b", 4)) 

however, cannot occur if first , second item in sequence in list together. instance, following list cannot added list because first item contains "banana" "a".

("banana", "a", 5) # should not appended ("banana", "c", 6) # should appended ("strawberry", "a", 7) # should appended 

in regular list we'd following avoid duplicates:

if not item in list:   list.append(item) 

but note case involve partial duplicate, i.e. first 2 items cannot identical between sublists.

i looking efficient solution because list can contain thousands of items.

i highly recommend using dictionary type of data coupling structure, along o(1) look-up times, you'll implementing better design. however, current data structure using following:

sample output:

with current structure:

l = [ ("banana", "a", 0), ("banana", "b", 1), ("coconut", "a", 2) ] items_to_add = [("banana", "a", 5), ("banana", "c", 6), ("strawberry", "a", 7)]  item_to_add in items_to_add:     if not item_to_add[:2] in [i[:2] in l]:         l.append(item_to_add) print l >>> [('banana', 'a', 0), ('banana', 'b', 1), ('coconut', 'a', 2),  ('banana', 'c', 6), ('strawberry', 'a', 7)] 

other wise, can use dictionary (factor out 2 first elements key):

with dictionary:

d = { ("banana", "a") : 0, ("banana", "b") : 1, ("coconut", "a") : 2 } items_to_add = [("banana", "a", 5), ("banana", "c", 6), ("strawberry", "a", 7)]  item_to_add in items_to_add:     key = item_to_add[:2]     value = item_to_add[-1]     if not key in d:         d[key] = value  print d >>> {('coconut', 'a'): 2, ('strawberry', 'a'): 7, ('banana', 'c'): 6,  ('banana', 'a'): 0, ('banana', 'b'): 1} 

a dictionary works in scenario you're trying leverage properties of key/value data structure. unique keys ensured, , efficient route well.


Comments