gpt4 book ai didi

python - 在 Python 中使用 csv 文件使用字典计算字符串中的单词数

转载 作者:行者123 更新时间:2023-11-28 19:10:25 25 4
gpt4 key购买 nike

我有一个代码,它从一个 csv 文件返回一个字典。有了这个,我想计算任何字符串中的单词数。例如,如果我输入:

“这个字符串中有多少个单词来自dict1”

我将如何遍历这个字符串并计算出现在字符串中的 dict1 中的单词?

代码:

import csv

def read_csv(filename, col_list):
"""This function expects the name of a CSV file and a list of strings
representing a subset of the headers of the columns in the file, and
returns a dictionary of the data in those columns."""

with open(filename, 'r') as f:
# Better covert reader to a list (items represent every row)
reader = list(csv.DictReader(f))
dict1 = {}
for col in col_list:
dict1[col] = []
# Going in every row of the file
for row in reader:
# Append to the list the row item of this key
dict1[col].append(row[col])

return dict1

最佳答案

这样的事情怎么样:

str_ = "How many words in this string are from dict1"
dict_1 = dict(a='many', b='words')

# With list comprehension

len([x for x in str_.split() if x in dict_1.values()])
# 2

# These ones don't count duplicates because they use sets

len(set(str_.split()).intersection(dict_1.values()))
# 2
len(set(str_.split()) & set(dict_1.values())) # Equivalent, but different syntax
# 2

# To be case insensitive, do e.g.,
words = str_.lower().split()
dict_words = map(str.lower, dict_1.values())

关于python - 在 Python 中使用 csv 文件使用字典计算字符串中的单词数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41215423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com