将列表中的所有单词替换为另一个列表中的项目索引

时间:2017-07-09 14:54:16

标签: python list replace

我有一个清单 -

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.我也尝试使用地图功能,但我没有成功

这是我的尝试:

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)

5 个答案:

答案 0 :(得分:1)

你可以做的是创建一个字典,将列表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变大,这可能是一个很大的减速。然而,字典具有查看字典中是否存在密钥以及用于检索值所需的恒定时间。通过使用字典,您可以用O(1)操作替换2 O(n)操作。

答案 1 :(得分: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]

答案 2 :(得分:0)

你可以试试这个:

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

答案 3 :(得分:0)

这是在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']

答案 4 :(得分:0)

您的解决方案正在进行(太多)查找。

这是我的:

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关键字。