2016-12-19 122 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