我想要一个这样的列表:['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, 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])]。我不知道为什么它不起作用。
白衣非少年
智慧大石
相关分类