简体   繁体   English

创建既在给定字符串中又在给定字典中的所有单词的列表

[英]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.

相关问题 我如何获得给定两个单词的所有组合列表? - how do i get a list of all combinations for both words given? 从 python 中的给定单词列表创建所有可能组合的列表 - creating a list of all possible combination from a given list of words in python 对于给定的字符串列表,计数单词数 - For a given list of string count no of words 根据列表和字典的值创建字典 - Creating a dictionary given values from a list and dictionary 给定列表中的字符串,创建所有可能的组合 - Creating all possible combinations given a string against a list 给定字典和字母列表,找到可以用字母创建的所有可能单词 - Given a Dictionary and a list of letters, find all possible words that can be created with the letters 根据列表中的给定单词拆分字符串 - Split string based on given words from list 给定字符串和(列表)单词,返回包含字符串的单词(最优算法) - Given string and (list) of words, return words that contain string (optimal algorithm) 如果给定的键和值从字典列表中匹配,则获取所有字典 - Fetch all dictionary if given key and value matches in from a list of dictionary 给定一个单词列表,请标识长度为4或更大的所有相同子字符串 - Given a list of words, identify all identical substrings of length 4 or greater
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM