计算列表列表中的元素

时间:2019-11-16 10:35:49

标签: python python-3.x list dictionary

我有一个列表列表,如下所示

[
 ['DDX11L1', 'lincRNA', 'chr1', 11869, 14409],
 ['WASH7P', 'lincRNA', 'chr1', 14404, 29570],
 ['MIR1302-2HG', 'lincRNA', 'chr1', 29554, 31109],
 ['FAM138A', 'lincRNA', 'chr1', 34554, 36081],
 ['OR4G4P', 'unprocessed_pseudogene', 'chr2', 52473, 53312],
 ['DDX11L1', 'transcribed_unprocessed_pseudogene', 'chr2', 11869, 14409],
 ['WASH7P', 'lincRNA', 'chr2', 14404, 29570],
 ['MIR1302-2HG', 'lincRNA', 'chr1', 29554, 31109],
 ['FAM138A', 'lincRNA', 'chr3', 34554, 36081],
 ['OR4G4P', 'unprocessed_pseudogene', 'chr2', 52473, 53312]
]

内部列表'chr'的不同之处在于'chr1''chr2''chrs'。取决于那个chr,我想计算出有多少'lincRNA'。(并非所有'chrs'都会有'lincRNA'

答案示例:

我想要的结果如下:

There are 5 'lincRNA' with 'chr1'
There are 1 'lincRNA' with 'chr2'

预先感谢

4 个答案:

答案 0 :(得分:1)

您需要分别跟踪每个“ chrX”的数量,默认字典会很好地工作:

from collections import defaultdict

count = defaultdict(int)

for ele in lst:
    if ele[1] == 'lincRNA':
        count[ele[2]] += 1

for k, v in count.items():
    print("There are {} 'lincRNA' with '{}'".format(v, k))

结果符合预期:

There are 1 'lincRNA' with 'chr3'
There are 1 'lincRNA' with 'chr2'
There are 5 'lincRNA' with 'chr1'

答案 1 :(得分:1)

当lincRNA不是任何子列表的第二个元素并且任何子列表中有多个linRNA时,此代码也可以正常工作。

my_list = [
 ['DDX11L1', 'lincRNA', 'chr1', 11869, 14409],
 ['WASH7P', 'lincRNA', 'chr1', 14404, 29570],
 ['MIR1302-2HG', 'lincRNA', 'chr1', 29554, 31109],
 ['FAM138A', 'lincRNA', 'chr1', 34554, 36081],
 ['OR4G4P', 'unprocessed_pseudogene', 'chr2', 52473, 53312],
 ['DDX11L1', 'transcribed_unprocessed_pseudogene', 'chr2', 11869, 14409],
 ['WASH7P', 'lincRNA', 'chr2', 14404, 29570],
 ['MIR1302-2HG', 'lincRNA', 'chr1', 29554, 31109],
 ['FAM138A', 'lincRNA', 'chr3', 34554, 36081],
 ['OR4G4P', 'unprocessed_pseudogene', 'chr2', 52473, 53312]
]
chrs = ["chr1","chr2","chr3"]
output = {"chr1":0,"chr2":0,"chr3":0}
for l in my_list:
    for _chr in chrs:
        if l.count(_chr) > 0:
            output[_chr] += l.count("lincRNA")
for k,v in output.items():
    print("There are {} 'lincRNA' with {}".format(v,k))

答案 2 :(得分:0)

那很容易做到。您必须像这样做一些for循环:

for i in list:
    for b in i:
        print(str(b) + " " str(i))

这应该访问每个子列表中的每个项目。现在,您需要计算一些if块:

if b == "lincRNA":
    count_lincRNA += 1

您可以对要计数的每个项目执行相同的操作。在for循环完成后,您可以打印(或任意)两个计数器变量

另一种方法(因为如果字符串“ lincRNA”上有尾随空格,这将失败),但是可以使用RegEx。

答案 3 :(得分:0)

使用字典解决方案


if __name__ == "__main__":
    main()