久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

交錯(cuò)不同長(zhǎng)度的列表,消除重復(fù),并保持順序

Interleave different length lists, elimating duplicates, and preserve order(交錯(cuò)不同長(zhǎng)度的列表,消除重復(fù),并保持順序)
本文介紹了交錯(cuò)不同長(zhǎng)度的列表,消除重復(fù),并保持順序的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有兩個(gè)列表,比如說:

I have two lists, let's say:

keys1 = ['A', 'B', 'C', 'D', 'E',           'H', 'I']
keys2 = ['A', 'B',           'E', 'F', 'G', 'H',      'J', 'K']

如何創(chuàng)建一個(gè)沒有重復(fù)的合并列表,保留兩個(gè)列表的順序,在它們所屬的位置插入缺失的元素?像這樣:

How do I create a merged list without duplicates that preserve the order of both lists, inserting the missing elements where they belong? Like so:

merged = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']

請(qǐng)注意,元素可以根據(jù)相等性進(jìn)行比較,但不是有序的(它們是復(fù)雜的字符串).元素不能通過比較來排序,但它們有一個(gè)基于它們?cè)谠剂斜碇谐霈F(xiàn)的順序.

Note that the elements can be compared against equality but not ordered (they are complex strings). The elements can't be ordered by comparing them, but they have an order based on their occurrence in the original lists.

如果出現(xiàn)矛盾(兩個(gè)輸入列表中的順序不同),任何包含所有元素的輸出都是有效的.當(dāng)然,如果解決方案在保留大部分訂單方面顯示出常識(shí)",則可以加分.

In case of contradiction (different order in both input lists), any output containing all elements is valid. Of course with bonus points if the solution shows 'common sense' in preserving most of the order.

再一次(正如一些評(píng)論仍然爭(zhēng)論的那樣),列表通常不會(huì)在公共元素的順序方面相互矛盾.如果他們這樣做了,算法需要優(yōu)雅地處理該錯(cuò)誤.

Again (as some comments still argue about it), the lists normally don't contradict each other in terms of the order of the common elements. In case they do, the algorithm needs to handle that error gracefully.

我從一個(gè)版本開始,它使用 .next() 遍歷列表以僅推進(jìn)包含不匹配元素的列表,但 .next() 只是不知道何時(shí)停止.

I started with a version that iterates over the lists with .next() to advance just the list containing the unmatched elements, but .next() just doesn't know when to stop.

merged = []
L = iter(keys1)
H = iter(keys2)
l = L.next()
h = H.next()

for i in range(max(len(keys1, keys2))):
  if l == h:
    if l not in merged:
      merged.append(l)
    l = L.next()
    h = H.next()

  elif l not in keys2:
    if l not in merged:
      merged.append(l)
    l = L.next()

  elif h not in keys1:
    if h not in merged:
      merged.append(h)
    h = H.next()

  else: # just in case the input is badly ordered
    if l not in merged:
      merged.append(l)
    l = L.next()
    if h not in merged:
      merged.append(h)
    h = H.next()   

print merged

這顯然不起作用,因?yàn)?.next() 會(huì)導(dǎo)致最短列表異常.現(xiàn)在我可以更新我的代碼以在每次調(diào)用 .next() 時(shí)捕獲該異常.但是代碼已經(jīng)很不符合pythonic了,這顯然會(huì)破滅泡沫.

This obviously doesn't work, as .next() will cause an exception for the shortest list. Now I could update my code to catch that exception every time I call .next(). But the code already is quite un-pythonic and this would clearly burst the bubble.

有沒有人更好地了解如何遍歷這些列表以組合元素?

Does anyone have a better idea of how to iterate over those lists to combine the elements?

如果我可以一次完成三個(gè)列表,則可以獲得獎(jiǎng)勵(lì)積分.

Bonus points if I can do it for three lists in one go.

推薦答案

您需要的基本上是任何合并實(shí)用程序所做的:它嘗試合并兩個(gè)序列,同時(shí)保持每個(gè)序列的相對(duì)順序.您可以使用 Python 的 difflib 模塊來區(qū)分這兩個(gè)序列,并將它們合并:

What you need is basically what any merge utility does: It tries to merge two sequences, while keeping the relative order of each sequence. You can use Python's difflib module to diff the two sequences, and merge them:

from difflib import SequenceMatcher

def merge_sequences(seq1,seq2):
    sm=SequenceMatcher(a=seq1,b=seq2)
    res = []
    for (op, start1, end1, start2, end2) in sm.get_opcodes():
        if op == 'equal' or op=='delete':
            #This range appears in both sequences, or only in the first one.
            res += seq1[start1:end1]
        elif op == 'insert':
            #This range appears in only the second sequence.
            res += seq2[start2:end2]
        elif op == 'replace':
            #There are different ranges in each sequence - add both.
            res += seq1[start1:end1]
            res += seq2[start2:end2]
    return res

例子:

>>> keys1 = ['A', 'B', 'C', 'D', 'E',           'H', 'I']
>>> keys2 = ['A', 'B',           'E', 'F', 'G', 'H',      'J', 'K']
>>> merge_sequences(keys1, keys2)
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']

請(qǐng)注意,您期望的答案不一定是唯一可能的答案.例如,如果我們?cè)谶@里改變序列的順序,我們會(huì)得到另一個(gè)同樣有效的答案:

Note that the answer you expect is not necessarily the only possible one. For example, if we change the order of sequences here, we get another answer which is just as valid:

>>> merge_sequences(keys2, keys1)
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'I']

這篇關(guān)于交錯(cuò)不同長(zhǎng)度的列表,消除重復(fù),并保持順序的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時(shí)出現(xiàn)問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點(diǎn))
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲(chǔ)在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對(duì)象)
主站蜘蛛池模板: 亚洲一二三在线观看 | 亚洲福利在线观看 | 日韩中文字幕一区二区 | 国产精品美女久久久久久免费 | 亚洲香蕉在线视频 | 亚洲一区国产精品 | 日韩欧美高清 | 在线视频亚洲 | 中文字幕日韩欧美 | 亚洲精品第一国产综合野 | 国产成人精品网站 | 欧美激情久久久 | 毛片免费看的 | 久久久一区二区 | 一级片av| 国产精品久久久av | 国产精品精品久久久 | 一区二区精品 | 热re99久久精品国产99热 | 99精品视频一区二区三区 | 国产一区免费视频 | 国产精品久久国产精品 | 欧美在线观看免费观看视频 | 亚洲福利av | 亚洲综合免费 | 日韩高清国产一区在线 | 一区二区三区欧美 | 久久精品久久综合 | 国产欧美日韩综合精品一区二区 | 一区二区在线 | 久久草在线视频 | 久久久久久国产精品 | 九七午夜剧场福利写真 | 日韩欧美精品在线 | 日韩在线资源 | 亚洲成人av | 久久精品久久久久久 | 国产99久久久国产精品 | 精品久久香蕉国产线看观看亚洲 | 91极品尤物在线播放国产 | 国产精品视屏 |