使用现有列表打印新列表

时间:2018-11-11 13:53:59

标签: python list sorting tuples

我想要一个这样的列表:['5','0 1','1 2','1 8','2 3']并返回一个新的由元组组成的列表,如下所示:[ (0,[1]),(1,[0,2,8]),(2 [1,3]),(3,[2]),(8,[1])]。每个元组的第一个元素是整数,第二个元素是整数列表,它出现在原始列表的旁边。我不能使用字典,集合,双端队列,二等分模块。

def create_network(file_name):
    friends = open(file_name).read().splitlines()
    network=[]

    for strings in friends:
        relationship=strings.strip().split(' ')
        if len(relationship)==2:
             a,b=relationship
             a=int(a)
             b=int(b)
             if a>=len(network):
                 network.append((a,[b]))
             else:
                 wow=network[a]
                 wow[1].append(b)
                 network[a]=wow


    return network

这是我到目前为止所拥有的。我希望它返回: [(0,[1,2,3]),(1,[0,4,6,7,9]),(2,[0,3,6,8,9]],(3,[0 ,2,8,8,9]),(4,[1,6,7,8]),(5,[9]),(6,[1,2,4,8]],(7,[1 ,4、8]),(8,[2、3、4、6、7]),(9,[1、2、3、5])],但它正在返回 [(0,[1,2,3]),(1,[4,6,7,9]),(2,[3,6,8,9]),(3,[8,9]) ,(4,[6,7,8]),(5,[9]),(6,[8]),(7,[8])]。我不知道为什么它不起作用。

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

from itertools import combinations


def create_network(lst):
    seen = {}

    for e in lst:
        for s, t in combinations(map(int, e.split()), 2):
            seen.setdefault(s, set()).add(t)
            seen.setdefault(t, set()).add(s)

    return [(k, sorted(values)) for k, values in seen.items()]


data = ['5', '0 1', '1 2', '1 8', '2 3']
result = create_network(data)

print(result)

输出

[(0, [1]), (1, [0, 2, 8]), (2, [1, 3]), (3, [2]), (8, [1])]

通常的想法是创建一个字典,其中的键是整数,值是一组整数,接下来将出现 至。语句map(int, e.split())创建一个可迭代的整数,然后使用combinations选择每个可能的对 从迭代器中,将每对添加到字典中,最后返回对值进行排序的元组。

更新 (不使用任何内置模块)

def combinations(m, lst):
    if m == 0:
        return [[]]
    return [[x] + suffix for i, x in enumerate(lst) for suffix in combinations(m - 1, lst[i + 1:])]


def create_network(lst):
    uniques = []
    for s in lst:
        for e in map(int, s.split()):
            if e not in uniques:
                uniques.append(e)

    result = []
    for number in uniques:
        seen = []
        for e in lst:
            values = list(map(int, e.split()))
            for s, t in combinations(2, values):
                if s == number:
                    if t not in seen:
                        seen.append(t)
                elif t == number:
                    if s not in seen:
                        seen.append(s)
        if seen:
            result.append((number, sorted(seen)))

    return sorted(result)


data = ['5', '0 1', '1 2', '1 8', '2 3']
network = create_network(data)
print(network)

输出

[(0, [1]), (1, [0, 2, 8]), (2, [1, 3]), (3, [2]), (8, [1])]

上面的代码不使用set,字典或任何外部模块。警告它可能会很慢。

答案 1 :(得分:0)

您可以使用列表理解:

d = ['5', '0 1', '1 2', '1 8', '2 3']
def find_edges(_d, c):
  return [(a if b == c else b) for a, b in _d if c in [a, b]]

new_d = [[int(c) for c in i.split()] for i in d if len(i) > 1]
_final = []
for i in [h for d in new_d for h in d]:
   if not any(j == i for j, _ in _final):
      _final.append((i, find_edges(new_d, i)))

输出:

[(0, [1]), (1, [0, 2, 8]), (2, [1, 3]), (8, [1]), (3, [2])]