[英]Python - Count how many elements from a list appear in a csv line
I have a large list of many strings such as ':0001959' etc... I also have a file with many csv lines and some of these lines contain the strings I have in this list.我有很多字符串的大列表,例如 ':0001959' 等......我还有一个包含许多 csv 行的文件,其中一些行包含我在此列表中的字符串。 What I need to do, is to count (for each line) how many elements from this list are matched to each line.
我需要做的是计算(对于每一行)这个列表中有多少元素与每一行匹配。
split_string = line.split(",")
for split_string in line:
if split_string[0] in Lethality:
count = count +1
The above code is a quick look at what I already have.上面的代码是快速浏览一下我已经拥有的。 Thanks
谢谢
You need to go through the elements in the split_string
list which contains your csv splitted data, you are going to override split_string
with the characters that are in the original csv line with this for loop, so you can modify your code as :您需要
split_string
包含 csv split_string
数据的split_string
列表中的元素,您将使用此 for 循环使用原始 csv 行中的字符覆盖split_string
,因此您可以将代码修改为:
split_string = line.split(",")
for element in split_string:
if element in Lethality:
count = count + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.