[英]Creating a list of all words that are both in a given string and in a given dictionary
I'm working with a string and a dictionary in Python, trying to loop through the string in order to create a list of the words which appear both in the string and amongst the keys of the dictionary.我正在使用 Python 中的字符串和字典,尝试遍历字符串以创建出现在字符串中和字典键中的单词列表。 What I have currently is:我目前拥有的是:
## dictionary will be called "dict" below
sentence = "is this is even really a sentence"
wordsinboth = []
for w in sentence.split():
if w in dict:
wordsinboth += w
Instead of returning a list of words split by whitespace, however, this code returns a list of every character in the sentence.但是,此代码返回的是句子中每个字符的列表,而不是返回由空格分隔的单词列表。 The same thing occurs even when I attempt to create a list of split words before looping, as seen below:即使我尝试在循环之前创建拆分单词列表时也会发生同样的事情,如下所示:
sentence = "is this is even really a sentence"
wordsinboth = []
sent = sentence.split()
for w in sent:
if w in dict:
wordsinboth += w
I guess I'm not able to specify "if w in dict" and still split by whitespace?我想我无法指定“if w in dict”并且仍然被空格分割? Any suggestions on how to fix this?对于如何解决这个问题,有任何的建议吗?
Use append
instead of +=
:使用append
而不是+=
:
sentence = "is this is even really a sentence"
wordsinboth = []
for w in sentence.split():
if w in dict:
wordsinboth.append(w)
The +=
operator doesn't work as you'd expect: +=
操作符不像你期望的那样工作:
a = []
myString = "hello"
a.append(myString)
print(a) # ['hello']
b = []
b += myString
print(b) # ['h', 'e', 'l', 'l', 'o']
If you're interested on why this happens, the following questions are a good read:如果您对发生这种情况的原因感兴趣,请阅读以下问题:
Also, note that using list comprehensions might result in a more elegant solution to your problem:另请注意,使用列表推导式可能会为您的问题提供更优雅的解决方案:
wordsinboth = [word for word in sentence.split() if word in dict]
You can use +=
on a list, but you must add a list to it, not a value, otherwise the value gets converted to a list before being added.您可以在列表上使用+=
,但您必须向其中添加一个列表,而不是一个值,否则该值在添加之前会被转换为一个列表。 In your case, the w
strings are being converted to a list of all the characters in them (eg 'if'
=> ['i', 'f']
).在您的情况下, w
字符串被转换为其中所有字符的列表(例如'if'
=> ['i', 'f']
)。 To work around that, make the value into a list by adding []
around it:要解决此问题,请通过在其周围添加[]
将值放入列表中:
for w in sentence.split():
if w in dict:
wordsinboth += [w]
Use list comprehensions it's more shortest and elegant way for your case:使用列表推导式对于您的案例来说是更短、更优雅的方式:
wordsinboth = [word for word in sentence.split() if w in dict]
Problem in your cycle that you have to use append
for adding new item to wordsinboth
instead of +
operator, also please keep in mind that it can create duplicates, if you need uniq items you can wrap your result to set
which gives you uniq words.循环中的问题是您必须使用append
将新项目添加到wordsinboth
而不是+
运算符,还请记住它可以创建重复项,如果您需要 uniq 项目,您可以将结果包装到set
,从而为您提供 uniq 单词。
Like this:像这样:
wordsinboth = {word for word in sentence.split() if w in dict}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.