简体   繁体   English

Python - 计算一个列表中有多少个元素出现在 csv 行中

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

相关问题 Python - 计算存储在列表中的关键字在文本中出现的次数 - Python - Count how many times keywords stored in a list appear in text 查找列表 A 中出现的元素数量比在类似但混合的列表 B 中出现的次数 - Finding the count of how many elements of list A appear before than in the similar but mixed list B 计算字符列表在字符串中出现的次数 - Python(无计数或计数器) - Count how many times a list of characters appear in a string - Python (no count or counter) 在 div 内部被刮掉,但不知道如何将元素拆分为列表项,以便它们出现在生成的 csv 中的新行中 - Scraped inside of div but no clue how to split the elements as list item so that they appear in a new line in the csv generated 如何使用 Python 在 csv 行的末尾写入列表的单个元素? - How to write single elements of a list at the end of a csv line with Python? 如何使用python从csv逐行打印列表 - How to make list to be printed line by line from csv using python Python DataFrame 计算有多少不同的元素 - Python DataFrame count how many different elements 如何在一行中从python列表中收集许多元组? - how to collect many tuple from a list in python in one line? 如何在python上的列表中计算列表元素 - How to count list elements within list on python 计算一列中有多少字符出现在另一列中(熊猫) - Count how many characters from a column appear in another column (pandas)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM