搜索列表以查看它是否包含存储在python中不同列表中的字符串

2024-05-16 08:59:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个列表中有一个单词列表(单词列表),我创建了另一个列表,它只是一行文章标题(headline col)。标题是由许多单词组成的字符串,而单词列表是单个单词。我想搜索标题,看看它们是否包含我的单词列表中的任何单词,如果包含,请在另一个列表(slam_list)中添加标题

我已经查过了,我看到的所有东西都只是将一个精确的字符串与另一个相同的字符串进行匹配。例如,查看条目是否完全是“apple”,而不是查看条目是否在“john今天吃了一个苹果”中

我尝试过使用集合,但我只能在有匹配项的情况下让它返回True,我不知道如何让它附加slam_列表,甚至只是打印条目。这就是我所拥有的。我如何使用它来获得我需要的东西

import csv

word_list = ["Slam", "Slams", "Slammed", "Slamming",
             "Blast", "Blasts", "Blasting", "Blasted"]

slam_list = []
csv_data = []

# Creating the list I need by opening a csv and getting the column I need
with open("website_headlines.csv", encoding="utf8") as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        data.append(row)

headline_col = [headline[2] for headline in csv_data]

Tags: csvthecsvfile字符串标题列表data条目
2条回答

在这里,由于您正在阅读csv,因此使用pandas来实现您的目标可能会更容易

您要做的是通过其索引来标识列,它看起来像是2。然后在word_list中找到第三列的值

import pandas as pd

df = pd.read_csv("website_headlines.csv")
col = df.columns[2]
df.loc[df[col].isin(word_list), col]

考虑下面的例子

import numpy as np
import pandas as pd

word_list = ["Slam", "Slams", "Slammed", "Slamming",
             "Blast", "Blasts", "Blasting", "Blasted"]

# add some extra characters to see if limited to exact matches
word_list_mutated = np.random.choice(word_list + [item + '_extra' for item in word_list], 10)

data = {'a': range(1, 11), 'b': range(1, 11), 'c': word_list_mutated}
df = pd.DataFrame(data)
col = df.columns[2]

>>>df.loc[df[col].isin(word_list), col]
    a   b               c
0   1   1           Slams
1   2   2           Slams
2   3   3   Blasted_extra
3   4   4          Blasts
4   5   5     Slams_extra
5   6   6  Slamming_extra
6   7   7            Slam
7   8   8     Slams_extra
8   9   9            Slam
9  10  10        Blasting

所以,正如你提到的,使用集合绝对是一种方法。这是因为在集合中查找要比在列表中查找快得多。如果你想知道原因,可以在谷歌上快速搜索哈希。要进行此更改,只需将word_列表中的方括号更改为大括号

你需要处理的真正问题是“标题是由许多单词组成的字符串,而单词列表是单个单词”

你需要做的是重复许多单词。我假设headline_col是一个标题列表,其中headline是一个包含一个或多个单词的字符串。我们将遍历所有标题,然后遍历标题中的每个单词

word_list = {"Slam", "Slams", "Slammed", "Slamming", "Blast", "Blasts", "Blasting", "Blasted"}

# Iterate over each headline
for headline in headline_col:

    # Iterate over each word in headline
    # Headline.split will break the headline into a list of words (breaks on whitespace)
    for word in headline.split():

        # if we've found our word
        if word in word_list:
            # add the word to our list
            slam_list.append(headline)
            # we're done with this headline, so break from the inner for loop
            break

相关问题 更多 >