How to use certain elements in a list to compare elements in other lists and output a certain element if duplicates are found in Python? -


i new here (and new programming). have tried locate relevant questions no luck.

basically, got lists shown below:

[0, 'a', 1, 3, 3.3, 220, 22.27] [0, 'b', 1, 13, 3.3, 220, 23.19] [0, 'c', 1, 23, 3.3, 220, 24.11] [1, 'a', 1, 3, 3.5, 200, 20.02] [1, 'b', 1, 43, 3.3, 220, 25.94] [2, 'a', 1, 3, 3.3, 250, 26.86] 

i use list items of indices 1, 2 , 3 comparison key , search within lists available given key.

if found, need output made of list items of indices 0, 4, 5 , -1.

so, given lists, desired procedure shown below.

first round:

  • comparison key : 'a', 1, 3
  • go through available lists
  • found same key in

    [0, 'a', 1, 3, 3.3, 220, 22.27]

    [1, 'a', 1, 3, 3.5, 200, 20.02]

    [2, 'a', 1, 3, 3.3, 250, 26.86]

  • then create output

    0, 3.3, 220, 22.27

    1, 3.5, 200, 20.02

    2, 3.3, 250, 26.86

i have no idea how code yet, me please?

thanks in advance

@wagaman: hi pal, here little snippet job:

universe = [] universe.append([0, 'a', 1, 3, 3.3, 220, 22.27]) universe.append([0, 'b', 1, 13, 3.3, 220, 23.19]) universe.append([0, 'c', 1, 23, 3.3, 220, 24.11]) universe.append([1, 'a', 1, 3, 3.5, 200, 20.02]) universe.append([1, 'b', 1, 43, 3.3, 220, 25.94]) universe.append([2, 'a', 1, 3, 3.3, 250, 26.86])   def extract_from_lists(universe, keys=[1,2,3], matches=['a',1,3]):     if len(keys) == len(matches):         l in universe:             valid = true             in range(len(keys)):                 if l[keys[i]] != matches[i]:                     valid = false                     break             if valid:                 print("{},{},{},{}".format(l[0],l[4],l[5],l[-1]))     else:         print('keys list , matches list length must equal.')   extract_from_lists(universe) 

if need further information it, let me know, it's simple comparison , use basic concepts of python default values of arguments, iteration , so.


Comments