簡體   English   中英

用另一個列表中的項目索引替換列表中所有出現的單詞

[英]replace all word occurrence in list with index of items in another list

我有一個清單-

A=["hi how are you","have good day","where are you going ","do you like the place"]

和另一個列表-

B=["how","good","where","going","like","place"]

列表B包含列表A中存在的某些單詞。我想用列表B中的索引替換列表A中出現的所有列表B中的單詞。如果不存在該單詞,請將其替換為0

因此,替換后的清單A應該是

["0 1 0 0","0 2 0","3 0 0 4","0 0 5 0 6"]

我嘗試使用for循環,但由於列表長度> 10000,所以效率不高。我也嘗試使用map函數,但未成功

這是我的嘗試:

for item in list_A:
    words=sorted(item.split(), key=len,reverse=True)
    for w in word:
        if w.strip() in list_B:
            item=item.replace(w,str(list_B.index(w.strip())))
        else:
            item=item.replace(w,0)

您可以做的是創建一個字典,將列表B中的每個單詞映射到其索引。 然后,您只需要遍歷第一個列表一次。

就像是

B = ["how","yes"]
BDict = {}
index = 0
for x in B:
    Bdict[x] = index
    index += 1

for sentence in A:
     for word in sentence:
         if word in BDict:
              #BDict[word] has the index of the current word in B
         else:
              #Word does not exist in B

這將大大減少運行時間,因為字典具有O(1)訪問時間。 但是,根據B的大小,字典可能會變得很大

編輯:您的代碼有效,之所以變慢,是因為使用列表時inindex運算符必須執行線性搜索。 因此,如果B變大,則速度可能會大大降低。 但是,字典需要一定的時間來查看字典中是否存在鍵以及獲取值。 通過使用字典,您可以將2個O(n)操作替換為O(1)操作。

您應該定義一個函數以返回第二個列表中的單詞索引:

def get_index_of_word(word):
    try:
        return str(B.index(word) + 1)
    except ValueError:
        return '0'

然后,您可以使用嵌套列表推導來生成結果:

[' '.join(get_index_of_word(word) for word in sentence.split()) for sentence in A]

更新

from collections import defaultdict

index = defaultdict(lambda: 0, ((word, index) for index, word in enumerate(B, 1))

[' '.join(str(index[word]) for word in sentence.split()) for sentence in A]

您可以嘗試以下方法:

A=["hi how are you","have good day","where are you going ","do you like the place"]
A = map(lambda x:x.split(), A)
B=["how","good","where","going","like","place"]
new = [[c if d == a else 0 for c, d in enumerate(i)] for i in A for a in B]

final = map(' '.join, map(lambda x: [str(i) for i in x], new))

print final

這是在Python 3.x中

A=["hi how are you","have good day","where are you going ","do you like the place"]
B=["how","good","where","going","like","place"]
list(map(' '.join, map(lambda x:[str(B.index(i)+1) if i in B else '0' for i in x], [i.split() for i in A])))

輸出:

['0 1 0 0', '0 2 0', '3 0 0 4', '0 0 5 0 6']

您好,您的解決方案正在進行太多查找。

這是我的:

A=["hi how are you",
   "have good day",
   "where are you going ",
   "do you like the place"]

B=["how","good","where","going","like","place"]

# I assume B contains only unique elements.

gg = { word: idx for (idx, word) in enumerate(B, start=1)}
print(gg)

lookup = lambda word: str(gg.get(word, 0)) # Buils your index and gets you efficient search with proper object types.

def translate(str_):
    return ' '.join(lookup(word) for word in str_.split())        

print(translate("hi how are you")) # check for one sentence.


translated =  [translate(sentence) for sentence in A] # yey victory.

print(translated)

# Advanced usage

class  missingdict(dict):
    def __missing__(self, key):
        return 0

miss = missingdict(gg)

def tr2(str_):
    return ' '.join(str(miss[word]) for word in str_.split())


print([tr2(sentence) for sentence in A])

當您在python中更加自信時,您可能還會使用yield關鍵字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM