如何获取包含元组的列表列表中的最小值?

时间:2016-12-13 10:58:52

标签: python-2.7 list tuples

我有一个列表如下:

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

我需要根据每个列表的最后一个值和第一个值减少列表数量。这是为了保持列表的第一个元组与另一个列表的另一个元组匹配时具有最小最后值的列表。

在上面的例子中,有两个列表中有(1,3)但最后的值是3和2.所以它必须保留值为2的那个并丢弃一个带有3的那个。

所以新的列表列表将是这样的:

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

这是我尝试的方式:

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)

这似乎不起作用。这个逻辑有什么不对吗?如何实现这项任务?

这必须处理的实际数据集是:

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]]

2 个答案:

答案 0 :(得分:1)

也许你是在思考它,你的代码有很高的复杂性。在编码之前尝试分离您的想法并制作简单的架构。 我不是python的专家,但我制作了这个似乎有用的小代码:

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)

我在这里做的是尽可能快地在结果数组中添加元素,如果我找到一个最后一个元素小于结果数组中实际值的新元素,则将其删除。 / p>

也许这会让你获得成功的结果。 问候。

答案 1 :(得分:1)

您可以使用字典尝试以下操作:

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()])

输出:

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