Rakshith G B
Rakshith G B

Reputation: 826

How to obtain the smallest value in this list of lists which contain tuples?

I have a list of lists that goes something like this:

tup = [[(1,3),(2,4),[some data],3],[(3,4),(5,6),[some data],4],[(1,3),(7,9),[some data],2]]

I need to reduce the number of lists based on the last value and first value of each list. That is to keep that list which has the smallest last value when the first tuple of the list matches with another first tuple of another list.

In the above example there is (1,3) in two lists but the last values are 3 and 2. So it has to retain the one with the value 2 and discard the one with 3.

So the new list of lists would be like this:

tup = [[(3,4),(5,6),[some data],4],[(1,3),(7,9),[some data],2]]

This is how I tried it:

tup2 = copy.deepcopy(tup)
for objects in tup:
        small = objects
        a = objects[0]
        for objects2 in tup2:
            b = objects2[0]
            if(all(i == j for i, j in zip(a,b))):
                if(objects[3]<objects2[3] or objects[3]==objects2[3]):
                    small = objects
                else:
                    small = objects2
        tup3.append(small)
        print(small)

This doesn't seem to work. Whats wrong in this logic? How can this task be achieved?

The actual data set this has to work on is this:

tup = [[(1, 3), (2, 4), [(2, 5)], 2], [(2, 4), (1, 3), [(2, 5)], 2], [(2, 3), (7, 4), [(4, 4), (5, 4), (6, 4), (7, 4), (8, 4)], 6], [(7, 4), (2, 3), [(8, 4), (7, 4), (6, 4), (5, 4), (4, 4)], 6], [(2, 4), (1, 3), [(2, 5)], 2], [(1, 3), (2, 4), [(2, 5)], 2], [(5, 1), (7, 2), [(6, 3), (7, 3)], 3], [(7, 2), (5, 1), [(7, 3), (6, 3)], 3], [(5, 1), (8, 3), [(6, 3), (6, 4), (7, 4), (8, 4)], 5], [(8, 3), (5, 1), [(8, 4), (7, 4), (6, 4), (6, 3)], 5], [(7, 2), (5, 1), [(7, 3), (6, 3)], 3], [(5, 1), (7, 2), [(6, 3), (7, 3)], 3], [(7, 2), (8, 3), [(8, 4)], 2], [(8, 3), (7, 2), [(8, 4)], 2], [(7, 4), (2, 3), [(8, 4), (7, 4), (6, 4), (5, 4), (4, 4)], 6], [(2, 3), (7, 4), [(4, 4), (5, 4), (6, 4), (7, 4), (8, 4)], 6], [(8, 3), (5, 1), [(8, 4), (7, 4), (6, 4), (6, 3)], 5], [(5, 1), (8, 3), [(6, 3), (6, 4), (7, 4), (8, 4)], 5], [(8, 3), (7, 2), [(8, 4)], 2], [(7, 2), (8, 3), [(8, 4)], 2]]

Upvotes: 0

Views: 56

Answers (2)

Ouro17
Ouro17

Reputation: 26

Maybe you are overthinking it, your code have a very high complexity. Try to separate your ideas and make a simple schema before coding. I'm not an expert in python but I made this little code that seems to work:

def find(tple, arr): 
  result = -1
  found = False
  i = 0

  while not found and (i < len(arr)):
    if arr[i][0] == tple:
      result = i
      found = True
    else:
      i += 1

  return result

#actual data set
tup = [
        [(1, 3), (2, 4), [(2, 5)], 2], 
        [(2, 4), (1, 3), [(2, 5)], 2],
        [(2, 3), (7, 4), [(4, 4), (5, 4), (6, 4), (7, 4), (8, 4)], 6], 
        [(7, 4), (2, 3), [(8, 4), (7, 4), (6, 4), (5, 4), (4, 4)], 6], 
        [(2, 4), (1, 3), [(2, 5)], 2], 
        [(1, 3), (2, 4), [(2, 5)], 2], 
        [(5, 1), (7, 2), [(6, 3), (7, 3)], 3],
        [(7, 2), (5, 1), [(7, 3), (6, 3)], 3],
        [(5, 1), (8, 3), [(6, 3), (6, 4), (7, 4), (8, 4)], 5], 
        [(8, 3), (5, 1), [(8, 4), (7, 4), (6, 4), (6, 3)], 5],
        [(7, 2), (5, 1), [(7, 3), (6, 3)], 3], 
        [(5, 1), (7, 2), [(6, 3), (7, 3)], 3], 
        [(7, 2), (8, 3), [(8, 4)], 2], 
        [(8, 3), (7, 2), [(8, 4)], 2], 
        [(7, 4), (2, 3), [(8, 4), (7, 4), (6, 4), (5, 4), (4, 4)], 6], 
        [(2, 3), (7, 4), [(4, 4), (5, 4), (6, 4), (7, 4), (8, 4)], 6], 
        [(8, 3), (5, 1), [(8, 4), (7, 4), (6, 4), (6, 3)], 5], 
        [(5, 1), (8, 3), [(6, 3), (6, 4), (7, 4), (8, 4)], 5], 
        [(8, 3), (7, 2), [(8, 4)], 2], 
        [(7, 2), (8, 3), [(8, 4)], 2]
      ]

#tup = [[(1,3),(2,4),[1,2],3],[(3,4),(5,6),[1,2],4],[(1,3),(7,9),[1,2],2]]

#tup =[
#      [(5, 1), (7, 2), [(6, 3), (7, 3)], 3],
#      [(5, 1), (8, 3), [(6, 3), (6, 4), (7, 4), (8, 4)], 5]
#     ]

result = []

for rec in tup:
  index = find(rec[0], result)

  if index != -1 and (rec[-1] < result[index][-1]):
    result[index] = rec
  elif index == -1:
    result.append(rec)

print(result)

What I'm doing here is to add the element in the result array as fast as possible, and subtitute it if I found a new one that have a last element smaller than the actual value in the result array.

Maybe this can lead you to a succeful result. Regards.

Upvotes: 1

MMF
MMF

Reputation: 5921

You can try the following using a dictionary :

result = [] # return list
check_dic = {}
for t1, t2, data1, val1 in [l_ for l_ in tup]:
    check_dic[t1] = [t1, t2, data1, val1] 
    for t1_, t2_, data2, val2 in [l_ for l_ in tup]:
        if t1==t1_: # if the first tuple is the same
            if val2 < check_dic[t1][3]:
                check_dic[t1] = [t1_, t2_, data2, val2]

result.extend([t for t in check_dic.itervalues()])

Output :

>> [[(1, 3), (7, 9), [some data], 2], [(3, 4), (5, 6), [some data], 4]]

Upvotes: 1

Related Questions