2016-12-19 124 views
12

我想对连续顺序中的元组列表排序,因此每个元组的第一个元素等于前一个元素的最后一个元素。按连续顺序对元组列表进行排序

例如:

input = [(10, 7), (4, 9), (13, 4), (7, 13), (9, 10)] 
output = [(10, 7), (7, 13), (13, 4), (4, 9), (9, 10)] 

我已经开发了这样的搜索:

output=[] 
given = [(10, 7), (4, 9), (13, 4), (7, 13), (9, 10)] 
t = given[0][0] 
for i in range(len(given)): 
     # search tuples starting with element t 
     output += [e for e in given if e[0] == t] 
     t = output[-1][-1] # Get the next element to search 

print(output)  

有一个Python的方式来实现这样的命令? 并有办法做到这一点“就地”(只有一个列表)?

在我的问题,输入可以在使用所有元组的圆形方式被重新排序,所以它是不重要的所选择的第一个元素。

+6

如果一个元组与其他任何元组都不匹配,该怎么办? – Kasramvd

+3

另外,配对是唯一的,还是必须处理回溯:如果您第一次尝试将它们配对不正确? – ShadowRanger

+0

我不认为* sort *或*连续*这两个术语都适用于这个问题。 –

回答

8

假设在list你的元组将是圆形的,你可以使用dict到的复杂性中实现它O(n)的为:

input = [(10, 7), (4, 9), (13, 4), (7, 13), (9, 10)] 
input_dict = dict(input) # Convert list of `tuples` to dict 

elem = input[0][0] # start point in the new list 

new_list = [] # List of tuples for holding the values in required order 

for _ in range(len(input)): 
    new_list.append((elem, input_dict[elem])) 
    elem = input_dict[elem] 
    if elem not in input_dict: 
     # Raise exception in case list of tuples is not circular 
     raise Exception('key {} not found in dict'.format(elem)) 

终值保持由new_list将是:

>>> new_list 
[(10, 7), (7, 13), (13, 4), (4, 9), (9, 10)] 
+1

我喜欢你的'字典(输入)'转换建议,它提高了长输入数组的速度。 – Rockcat

+0

我会很高兴知道倒票的原因。可能是我可以改善它:) –

+0

也许是因为一个'KeyError'异常会提高'如果elem不在input_dict'在检查上方的行中。除了KeyError之外,你可能应该使用'try:\ n elem = ... m] \ n:\ n引发KeyError(...'代替 – wizzwizz4

5

,如果你不怕浪费一些内存,你可以创建一个字典包含启动整数作为键和元组的值start_dict,做这样的事情:

tpl = [(10, 7), (4, 9), (13, 4), (7, 13), (9, 10)] 
start_dict = {item[0]: item for item in tpl} 

start = tpl[0][0] 
res = [] 
while start_dict: 
    item = start_dict[start] 
    del start_dict[start] 
    res.append(item) 
    start = item[-1] 

print(res) 

如果两个元开始用相同的数你会失去其中的一个......如果不是全部开始编号用于循环将不会终止。

但也许这是值得的基础上。

+0

为什么你懒得从字典中删除元素start_dict [start]? – Rockcat

+0

另外,我更喜欢start = item [-1],因为解决方案仍然可以处理多于2个元素的元组。即使变量元组长度为[(1,2),(7,3,1),(2,4,6,7)] – Rockcat

+0

@ user3715819:循环(因为它现在)在'start_dict'为空时终止;这就是为什么我打扰从它删除。但是:有很大的改进空间! –

2

居然还有即将你打算什么有作为输出,以及如果输入列表中具有无效结构做你所需要的许多问题。

假设你已经在仅两次包括在每个数对的输入。所以我们可以把这样的输入看作一个图,其中数字是节点,每一对都是边。而据我理解你的问题,你假定这个图是环状的,看起来像这样:

10 - 7 - 13 - 4 - 9 - 10 (same 10 as at the beginning) 

这说明你,你可以减少列表到图形存储[10, 7, 13, 4, 9]。这里是排序输入列表中的脚本:

# input 
input = [(10, 7), (4, 9), (13, 4), (7, 13), (9, 10)] 

# sorting and archiving 
first = input[0][0] 
last = input[0][1] 
output_in_place = [first, last] 

while last != first: 
    for item in input: 
     if item[0] == last: 
      last = item[1] 
      if last != first: 
       output_in_place.append(last) 

print(output_in_place) 

# output 
output = [] 
for i in range(len(output_in_place) - 1): 
    output.append((output_in_place[i], output_in_place[i+1])) 
output.append((output_in_place[-1], output_in_place[0])) 

print(output) 
2

我会首先创建形式的字典

{first_value: [list of tuples with that first value], ...} 

然后从那里工作:

from collections import defaultdict 

chosen_tuples = input[:1] # Start from the first 

first_values = defaultdict() 
for tup in input[1:]: 
    first_values[tup[0]].append(tup) 

while first_values: # Loop will end when all lists are removed 
    value = chosen_tuples[-1][1] # Second item of last tuple 
    tuples_with_that_value = first_values[value] 
    chosen_tuples.append(tuples_with_that_value.pop()) 
    if not chosen_with_that_value: 
     del first_values[value] # List empty, remove it 
1

你可以试试这个:

input = [(10, 7), (4, 9), (13, 4), (7, 13), (9, 10)] 

output = [input[0]] # output contains the first element of input 
temp = input[1:] # temp contains the rest of elements in input 

while temp: 
    item = [i for i in temp if i[0] == output[-1][1]].pop() # We compare each element with output[-1] 
    output.append(item) # We add the right item to output 
    temp.remove(item) # We remove each handled element from temp 

输出:

>>> output 
[(10, 7), (7, 13), (13, 4), (4, 9), (9, 10)] 
0

这是一个(以下efficien牛逼比字典版本)的变体,其中列表被就地改变:

tpl = [(10, 7), (4, 9), (13, 4), (7, 13), (9, 10)] 

for i in range(1, len(tpl)-1): # iterate over the indices of the list 
    item = tpl[i] 
    for j, next_item in enumerate(tpl[i+1:]): # find the next item 
               # in the remaining list 
     if next_item[0] == item[1]: 
      next_index = i + j 
      break 
    tpl[i], tpl[next_index] = tpl[next_index], tpl[i] # now swap the items 

这里是同样的想法的更高效的版本:

tpl = [(10, 7), (4, 9), (13, 4), (7, 13), (9, 10)] 
start_index = {item[0]: i for i, item in enumerate(tpl)} 

item = tpl[0] 
next_index = start_index[item[-1]] 
for i in range(1, len(tpl)-1): 
    tpl[i], tpl[next_index] = tpl[next_index], tpl[i] 
    # need to update the start indices: 
    start_index[tpl[next_index][0]] = next_index 
    start_index[tpl[i][0]] = i 
    next_index = start_index[tpl[i][-1]] 
print(tpl) 

就地名单发生变化;该字典只包含列表中元组的起始值及其索引。

0

下面是使用sorted功能和自定义按键功能强大的解决方案:

input = [(10, 7), (4, 9), (13, 4), (7, 13), (9, 10)] 

def consec_sort(lst): 
    def key(x): 
     nonlocal index 
     if index <= lower_index: 
      index += 1 
      return -1 
     return abs(x[0] - lst[index - 1][1]) 
    for lower_index in range(len(lst) - 2): 
     index = 0 
     lst = sorted(lst, key=key) 
    return lst 

output = consec_sort(input) 
print(output) 

原始列表不会被修改。请注意,sorted被称为您的input长度为5的列表的3次。在每次调用中,都会正确放置一个附加元组。第一个元组保持它的原始位置。

我已经使用了nonlocal关键字,这意味着此代码仅适用于Python 3(可以使用global来代替合法的Python 2代码)。

0

我的两分钱:

def match_tuples(input): 
    # making a copy to not mess up with the original one 
    tuples = input[:]   # [(10,7), (4,9), (13, 4), (7, 13), (9, 10)] 
    last_elem = tuples.pop(0) # (10,7) 

    # { "first tuple's element": "index in list"} 
    indexes = {tup[0]: i for i, tup in enumerate(tuples)} # {9: 3, 4: 0, 13: 1, 7: 2} 

    yield last_elem # yields de firts element 

    for i in range(len(tuples)): 
     # get where in the list is the tuple which first element match the last element in the last tuple 
     list_index = indexes.get(last_elem[1]) 
     last_elem = tuples[list_index] # just get that tuple 
     yield last_elem 

输出

input = [(10,7), (4,9), (13, 4), (7, 13), (9, 10)] 
print(list(match_tuples(input))) 
# output: [(10, 7), (7, 13), (13, 4), (4, 9), (9, 10)] 
0

要获得O(n)算法一个需要确保一个没有做一个双回路阵列上。一种方法是通过在某种查询表中保留已处理的值(dict将是一个不错的选择)。

例如这样的事情(我希望内联评论能够很好地解释功能)。这将就地修改列表,并应避免在列表中循环不必要的(甚至是隐含的):

inp = [(10, 7), (4, 9), (13, 4), (7, 13), (9, 10)] 

# A dictionary containing processed elements, first element is 
# the key and the value represents the tuple. This is used to 
# avoid the double loop 
seen = {} 

# The second value of the first tuple. This must match the first 
# item of the next tuple 
current = inp[0][1] 

# Iteration to insert the next element 
for insert_idx in range(1, len(inp)): 
    # print('insert', insert_idx, seen) 
    # If the next value was already found no need to search, just 
    # pop it from the seen dictionary and continue with the next loop 
    if current in seen: 
     item = seen.pop(current) 
     inp[insert_idx] = item 
     current = item[1] 
     continue 

    # Search the list until the next value is found saving all 
    # other items in the dictionary so we avoid to do unnecessary iterations 
    # over the list. 
    for search_idx in range(insert_idx, len(inp)): 
     # print('search', search_idx, inp[search_idx]) 
     item = inp[search_idx] 
     first, second = item 
     if first == current: 
      # Found the next tuple, break out of the inner loop! 
      inp[insert_idx] = item 
      current = second 
      break 
     else: 
      seen[first] = item