遍历字典并获取 TypeError: 'in <string>' requires string

我正在尝试遍历字符串列表,并根据单词字典匹配/打印出这些字符串中的任何一个。我似乎收到以下错误,但不太确定原因。


错误:TypeError:'in' 需要字符串作为左操作数,而不是列表


这是我正在使用的当前代码:


data = ["Great price on the dewalt saw", "cool deal, love it", "nice find", "definitely going to buy"]

words = {'price': ['price', 'compare', '$', 'percent', 'money']}


for d in data:

    for word in words.values():

        if word in d:

            print('Results:')

            print(d)

理想情况下,我想打印出包含任何价格键值的所有字符串。


一只甜甜圈
浏览 134回答 3
3回答

桃花长相依

word 正在返回 a list。您需要循环/迭代该列表(单词)。您可以通过以下方式完成它 -data = ["Great price on the dewalt saw", "cool deal, love it", "nice find", "definitely going to buy"]words = {'price': ['price', 'compare', '$', 'percent', 'money']}for d in data:&nbsp; &nbsp; for word in words.values():&nbsp; &nbsp; &nbsp; &nbsp; for s in word :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if s in d:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Results:')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(d)words.values()上面的代码将查找字典单词中的值数组(即 - 中的任何列表)中的任何字符串是否是任何字符串的一部分data。希望能帮助到你 !

不负相思意

您在这里遇到的问题是您有一个列表作为值,因此您的调用words.values()返回一个列表,该列表在内部包含另一个列表。您可以将其更改为for word in words['price']if you will only have a price key,或者您可以这样更改它:>>> words = {'price': ['price', 'compare', '$', 'percent', 'money']}>>> [word for wordlist in words.values() for word in wordlist]['price', 'compare', '$', 'percent', 'money']

慕田峪9158850

可能是提高接受答案效率的好主意。下面是一个提高时间复杂度的伪代码(基于输入)。visited_dict = {}results = {}for d in data:&nbsp; &nbsp; for k, word in words.items():&nbsp; &nbsp; &nbsp; &nbsp; can_stop = False&nbsp; &nbsp; &nbsp; &nbsp; res_val = []&nbsp; &nbsp; &nbsp; &nbsp; for s in word:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # if same words from another key is searched&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Ex: words = {'price': ['price', 'compare', '$', 'percent', 'money'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'price2':[ 'price', 'something', 'somethingelse']}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # In this case - get the result from visited dictionary to avoid&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if s in visited_dict and s not in visited_dict[s]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # save it to results&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # skip further steps&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if s in d and s not in res_val:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # store it results&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res_val.append(d)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # update visited dict&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visited_dict[s] = d&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Save the result to final key to result map&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results[k] = res_val&nbsp; # {'price': 'Great price on the dewalt saw'}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # To avoid duplicate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if res_val:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break注意:它没有经过测试或完全实现的代码。是的,一旦正确实施,它可能会增加空间复杂性。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python