简体   繁体   English

如何在条件语句中的for循环迭代器上使用比较语句?

[英]How to use a comparison statement on a for loop iterator in a conditional statement?

I'm iterating over a large (300+ columns & 1 000 000+ rows) .txt file (tab delimited). 我正在迭代一个大的(300+列和1 000 000+行).txt文件(制表符分隔)。 file format: 文件格式:

species 1    ...    sample1(11th col)    sample2    ....    sampleN(353th col)
species 2    ...    6046                 5364               ....
species 3    ...    15422                0                  ....

Each row is a species and from column 11 onward each column is a sample. 每行是一个物种,从第11栏开始,每列都是一个样本。 For each sample I want to know how many species in that sample have a value of greater than 0. So what I do is iterate over each line, see for which samples the value is greater than 0, and if so add a 1. So for each sample the total sum of 1s is the total amount of rows that have a value greater than 0. 对于每个样本,我想知道该样本中有多少物种的值大于0.所以我要做的是迭代每一行,看看哪个样本的值大于0,如果是,则添加1.所以对于每个样本,1的总和是值大于0的行的总量。

For that I use following code: 为此,我使用以下代码:

samples = []
OTUnumber = []

with open('all.16S.uniq.txt','r') as file:
     for i,line in enumerate(file): 
        columns = line.strip().split('\t')[11:353] 
        if i == 0: #headers are sample names so first row
            samples = columns #save sample names 
            OTUnumbers = [0 for s in samples] #set starting value as zero
        else:
            for n,v in enumerate(columns):
                if v > 0:
                    OTUnumber[n] = OTUnumber[n] + 1
                else:
                    continue

result = dict(zip(samples,OTUnumbers))

When I run thise code I get following error: TypeError: '>' not supported between instances of 'str' and 'int' This error is raised by if v > 0 . 当我运行这个代码时,我得到以下错误: TypeError: '>' not supported between instances of 'str' and 'int' if v > 0则引发此错误。 Why can't I write this statement? 为什么我不能写这个陈述?

So if v of columns [n] > 0 I want to add 1 to OTUnumber at that index. 因此,如果列[n]> 0的v我想在该索引处向OTUnumber添加1。 If v <0 I want to skip that row and do not add 1 (or add 0). 如果v <0我想跳过那一行而不加1(或加0)。

How can I make this code work? 如何使此代码有效?

When I run thise code I get following error: TypeError: '>' not supported between instances of 'str' and 'int' This error is raised by if v > 0 . 当我运行这个代码时,我得到以下错误: TypeError: '>' not supported between instances of 'str' and 'int'如果v > 0则引发此错误。 Why can't I write this statement? 为什么我不能写这个陈述?

As the error says, you are trying to use the comparison operator > on a string and an int, which is not allowed. 正如错误所示,您正在尝试对字符串和int使用比较运算符> ,这是不允许的。 v is a string, not an integer. v是一个字符串,而不是整数。 Presumably you want to use int(v) > 0 rather than v > 0 , or do the following to begin with. 大概你想使用int(v) > 0而不是v > 0 ,或者开始使用以下内容。

columns = [int(v) for v in line.strip().split('\t')[11:353]] 

try this: 尝试这个:

samples = []
OTUnumbers = []

with open('all.16S.uniq.txt','r') as file:
     for i,line in enumerate(file): 
        columns = line.strip().split('\t')[11:353] 
        if i == 0: #headers are sample names so first row
            samples = columns #save sample names 
            OTUnumbers = [0 for s in samples] #set starting value as zero
        else:
            for n,v in enumerate(columns):
                if int(v) > 0:
                    OTUnumbers[n] = OTUnumbers[n] + 1
                else:
                    continue

result = dict(zip(samples,OTUnumbers))

that's basically 2 fixes: 这基本上是2个修复:

  • casting v to int vint
  • renaming OTUnumber to OTUnumbers in all the code 重命名OTUnumberOTUnumbers中的所有代码

So the thing is that in your text file there are records which are strings and your code is trying to compare an integer to a string which throws a TypeError exception 所以问题是在你的文本文件中有记录是字符串,你的代码试图将一个整数与一个抛出TypeError异常的字符串进行比较

To make the code work you can convert your record to int before comparing ie, int(v) > 0 要使代码工作,您可以在比较之前将记录转换为int,即int(v) > 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM