0

I am new here (and new to programming). I have tried to locate the relevant questions with no luck.

Basically, I got some 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 would like to use list items of indices 1, 2 and 3 as the comparison key and search within all the lists available with the given key.

If found, then I need it the output to be made up of the list items of indices 0, 4, 5 and -1.

So, with the given lists, the desired procedure is shown below.

First round:

  • comparison key : 'a', 1, 3
  • go through the available lists
  • found the 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 the output

    0, 3.3, 220, 22.27

    1, 3.5, 200, 20.02

    2, 3.3, 250, 26.86

I have no idea how to code this yet, so could anyone help me please?

Thanks in advance

Tonechas
  • 13,398
  • 16
  • 46
  • 80
wagaman
  • 61
  • 6
  • 1
    What have you tried so far? Please provide any code you got so far. Are you looking for groups of three, only? – albert Jul 21 '16 at 16:48
  • I would try to break this problem down a bit conceptually and then build each part up in sections. 1. You want to be able to search a list to see if it contain a subset 2. You want to be able remove a subset if it is contained in a list 3. You want to run this filter over multiple lists 4. You want to print the output of a set of lists. Try solving the parts individually and then combine the individual parts to form the full solution – Selecsosi Jul 21 '16 at 16:51

2 Answers2

0

@wagaman: Hi Pal, Here a little snippet to do this 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):
        for l in universe:
            valid = True
            for i 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 and Matches List length must be equal.')


extract_from_lists(universe)

If you need further information about it, let me know, it's a very simple comparison and use basic concepts of python as default values of arguments, iteration and so.

Rafael Aguilar
  • 3,084
  • 1
  • 25
  • 31
0

Let us first define the input data:

some_lists = [[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]]

We have to iterate over the sublists of some_lists. To that end we will use an index variable, say x. For each sublist x we need to check whether x[1] == 'a' and x[2] == 1 and x[3] == 3 is fulfilled or not. This logical expression can be simplified through slice notation to x[1:4] == ['a', 1, 3]. For those x that match the condition, we create the desired output by selecting the specified items, namely [x[0], x[4], x[5], x[-1]]. All this logic can be readily implemented through a list comprehension in just one line of code:

In [253]: [[x[0], x[4], x[5], x[-1]] for x in some_lists if x[1:4] == ['a', 1, 3]]              
Out[253]: [[0, 3.3, 220, 22.27], [1, 3.5, 200, 20.02], [2, 3.3, 250, 26.86]]
Community
  • 1
  • 1
Tonechas
  • 13,398
  • 16
  • 46
  • 80