社区首页 >问答首页 >为什么我的double for循环不起作用?

为什么我的double for循环不起作用?
EN

Stack Overflow用户
提问于 2013-01-26 19:36:00
回答 1查看 155关注 0票数 0

我已经尝试了这个不起作用的double for循环。(请参见下文。)

基本上,我有一个构造列表和一个引物列表。引物通过“构建号”和“部件号”与构建物相关联。(每个构件由多个部分组成。)对于每个部分,都有一个“正向”和一个“反向”入门。对于那些对分子生物学感兴趣的人来说,我基本上是在写一个脚本来帮助我处理PCR。

我想要做的是:我想在引物列表中搜索那些应该与构建部分关联的引物,并将它们连接到一个主列表中。例如,如果我有一个包含EMP792 ( fw )和EMP793 ( re )的列表(它们在不同的行上),并且它们与我的构建列表中的构建#1的part #2相关联,我希望能够在"primers_list“中搜索相应的fw和re引物。如果构建部分的列表中没有关联的引物,我想先跳过这些构建。

我使用的策略是:我做了一个嵌套的for循环。对于构建列表中的每个构建,我希望它在引物列表中搜索fw和re引物。我知道这很低效,但作为一个初学者,这是我唯一能想到的方法。我加入了一些条件,通过检查与引物相关的构造号和部件号,来检查这些结构是否存在引物。

我面临的问题是:对于列表中的每个结构,循环都不会搜索整个primer_list。它似乎会自动跳过之前比较的所有引物,而只比较下一个尚未比较的引物。这导致了处理过程中的问题,如果您使用关联的数据集(我也粘贴在代码下面)运行代码,您会发现应该打印出关联引物的结构却没有关联的引物,这让我非常头疼,试图找出哪里出了问题(哈哈……)!

如果能帮上忙,我将不胜感激!

代码:

代码语言:javascript
代码运行次数:0
复制
with open('constructs-to-make-shortened2.csv', 'rU') as constructs:
    construct_list = csv.DictReader(constructs)

    with open('primers-with-notes-names.csv', 'rU') as primers:
        primers_list = csv.DictReader(primers)

        #make list of constructs for checking later on#
##        construct_numbers_list = []
##        for row in primers_list:
##            construct_numbers_list.append(row['construct number'])
##
##        print(construct_numbers_list)


        for construct in construct_list:
##            print('Currently at construct number ' + construct['Construct'])
##            print('Construct counter at ' + str(construct_counter))
##            print('Part number counter is at ' + str(part_number))
            master_row = {}
            master_row['construct'] = construct['Construct']
            master_row['strategy'] = construct['Strategy']
            master_row['construct name'] = construct['Construct Name']
            master_row['sequence'] = construct['Sequence']
            master_row['source'] = construct['Source']
            master_row['content'] = construct['Content']


            print('We are at construct number ' + str(construct['Construct']))
            print('Construct counter is at ' + str(construct_counter))
            is_next_construct = (int(construct['Construct']) > construct_counter)
            print('Are we at the next construct?')
            print(is_next_construct)

            if is_next_construct:
                part_number = 1
                construct_counter = int(construct['Construct'])
            print('Part number is now ' + str(part_number))

            for primer in primers_list:
                print(primer)


##                    print('Is primer ' + str(primer['name']) + ' associated with the construct?')
                is_associated_with_construct = bool(primer['construct number'] == construct['Construct'] and str(primer['part number']) == str(part_number))
##                    print(is_associated_with_construct)
                if(is_associated_with_construct == False):
                    break

                is_forward = bool(primer['construct number'] == construct['Construct'] and str(primer['part number']) == str(part_number) and primer['direction'] == 'fw primer')

                print('Primer ' + str(primer['name']) + ' is a forward primer?')
                print(is_forward)

                is_reverse = bool(primer['construct number'] == construct['Construct'] and str(primer['part number']) == str(part_number) and primer['direction'] == 're primer')

                print('Primer ' + str(primer['name']) + ' is a reverse primer?')
                print(is_reverse)

                if is_forward:
                    master_row['primer1'] = primer['name']
                    master_row['primer1 sequence'] = primer['primer sequence']
                    master_row['primer1 description'] = primer['notes']
                    master_row['primer1 length'] = primer['length']
##                        print(master_row)
                    continue

                elif is_reverse:
                    master_row['primer2'] = primer['name']
                    master_row['primer2 sequence'] = primer['primer sequence']
                    master_row['primer2 description'] = primer['notes']
                    master_row['primer2 length'] = primer['length']
##                        print(master_row)
                    part_number += 1
                    print('Part number now = ' + str(part_number) + '\n')
                    master_list.append(master_row)
                    break

数据子集(构造)(消除精确序列以保持在SO字符限制内):

代码语言:javascript
代码运行次数:0
复制
{'Sequence': '', 'Construct': '12', 'Strategy': 'Gibson', 'Content': 'Amp resistance marker', 'Source': 'pEM096', 'Construct Name': 'T7 RNAP core on BAC ori only with AmpR'}
{'Sequence': '', 'Construct': '12', 'Strategy': 'Gibson', 'Content': 'BAC origin and T7 RNAP core', 'Source': 'THSS301', 'Construct Name': 'T7 RNAP core on BAC ori only with AmpR'}
{'Sequence': '', 'Construct': '13', 'Strategy': 'Cut Gibson', 'Content': 'lycopene pathway (crtE.B.I.dxs.idi)', 'Source': 'KT-537', 'Construct Name': 'Combined vio and lyc plasmid'}
{'Sequence': '', 'Construct': '13', 'Strategy': 'Cut Gibson', 'Content': 'vioABE pathway and pSC101 ori and CmR;  digest with EcoRI and XbaI', 'Source': 'KT-587', 'Construct Name': 'Combined vio and lyc plasmid'}
{'Sequence': '', 'Construct': '14', 'Strategy': 'Cut Gibson', 'Content': 'lycopene pathway (crtE.B.I.dxs.idi)', 'Source': 'KT-537', 'Construct Name': 'Combined vio and lyc plasmid, with lyc in reverse direction'}
{'Sequence': '', 'Construct': '14', 'Strategy': 'Cut Gibson', 'Content': 'vioABE pathway and pSC101 ori and CmR;  digest with EcoRI and XbaI', 'Source': 'KT-587', 'Construct Name': 'Combined vio and lyc plasmid, with lyc in reverse direction'}
{'Sequence': '', 'Construct': '15', 'Strategy': 'Gibson', 'Content': 'vioABE pathway with random nucleotide spacers', 'Source': 'KT-587', 'Construct Name': 'Combined vio and lyc plasmid made by high GC polymerase'}
{'Sequence': '', 'Construct': '15', 'Strategy': 'Gibson', 'Content': 'lycopene pathway (crtE.B.I.dxs.idi)', 'Source': 'KT-537', 'Construct Name': 'Combined vio and lyc plasmid made by high GC polymerase'}
{'Sequence': '', 'Construct': '15', 'Strategy': 'Gibson', 'Content': 'pSC101 origin of replication and CmR resistance marker', 'Source': 'KT-537', 'Construct Name': 'Combined vio and lyc plasmid made by high GC polymerase'}
{'Sequence': '', 'Construct': '16', 'Strategy': 'Gibson', 'Content': 'P(tac)-SynZip18-T7 fragment', 'Source': 'THSS303', 'Construct Name': 'P(tac)-T7 fragment controller'}
{'Sequence': '', 'Construct': '16', 'Strategy': 'Gibson', 'Content': 'IncW backbone and TpR resistance and lacIq', 'Source': 'pEM103', 'Construct Name': 'P(tac)-T7 fragment controller'}
{'Sequence': '', 'Construct': '17', 'Strategy': 'Gibson', 'Content': 'P(tac)-SynZip18-T3 fragment', 'Source': 'THSS304', 'Construct Name': 'P(tac)-T3 fragment controller'}
{'Sequence': '', 'Construct': '17', 'Strategy': 'Gibson', 'Content': 'IncW backbone and TpR resistance and lacIq', 'Source': 'pEM103', 'Construct Name': 'P(tac)-T3 fragment controller'}

数据子集(引物):

代码语言:javascript
代码运行次数:0
复制
{'part number': '1', 'direction': 'fw primer', 'name': 'EMP790', 'primer sequence': 'gtttgtcggtgaactaattCttattaccaatgcttaatcagggaggcacctatctcagcg', 'notes': 'Fw Gibson primer on pEM096 to extract Amp resistance marker', 'length': '60', 'construct number': '12'}
{'part number': '1', 'direction': 're primer', 'name': 'EMP787', 'primer sequence': 'gatgaggatcgtttcgcatgctaaatacattcaaatatctatccgctcatgagacaataa', 'notes': 'Re Gibson primer on pEM096 to extract Amp resistance marker', 'length': '60', 'construct number': '12'}
{'part number': '2', 'direction': 'fw primer', 'name': 'EMP788', 'primer sequence': 'agatatttgaatgtatttagcatgcgaaacgatcctcatcctgtctcttgatcagatctt', 'notes': 'Fw Gibson primer on THSS301 to extract BAC and R6K origins and T7 RNAP core', 'length': '60', 'construct number': '12'}
{'part number': '2', 'direction': 're primer', 'name': 'EMP791', 'primer sequence': 'tgattaagcattggtaataaGaattagttcaccgacaaacaacagataaaacgaaaggcc', 'notes': 'Re Gibson primer on THSS301 to extract BAC origin and T7 RNAP core', 'length': '60', 'construct number': '12'}
{'part number': '1', 'direction': 'fw primer', 'name': 'EMP792', 'primer sequence': 'aaggaatattcagcaatttgGTTGGGGATAGCGCTAGCTATAATAactaTCACTATAGGG', 'notes': 'Fw Gibson primer on KT-587 to extract vioABE pathway with random nucleotide spacers', 'length': '60', 'construct number': '15'}
{'part number': '1', 'direction': 're primer', 'name': 'EMP793', 'primer sequence': 'gggcctttcttcggcacgggGTTGTAGCAGGCGTCTTTGTCAAAAAACCCCTCAAGACCC', 'notes': 'Re Gibson primer on KT-587 to extract vioABE pathway with random nucleotide spacers', 'length': '60', 'construct number': '15'}
{'part number': '2', 'direction': 'fw primer', 'name': 'EMP794', 'primer sequence': 'ACAAAGACGCCTGCTACAACcccgtgccgaagaaaggcccacccgtgaaggtgagccagt', 'notes': 'Fw Gibson primer on KT-537 to extract lycopene pathway (crtE.B.I.dxs.idi)', 'length': '60', 'construct number': '15'}
{'part number': '2', 'direction': 're primer', 'name': 'EMP795', 'primer sequence': 'gaggtcattactggatctaTcccgtgccgaagaaaggcccacccgtgaaggtgagccagt', 'notes': 'Re Gibson primer on KT-537 to extract lycopene pathway (crtE.B.I.dxs.idi)', 'length': '60', 'construct number': '15'}
{'part number': '3', 'direction': 'fw primer', 'name': 'EMP796', 'primer sequence': 'gggcctttcttcggcacgggAtagatccagtaatgacctcagaactccatctggatttgt', 'notes': 'Fw Gibson primer on KT-537 to extract pSC101 origin of replication and CmR resistance marker', 'length': '60', 'construct number': '15'}
{'part number': '3', 'direction': 're primer', 'name': 'EMP797', 'primer sequence': 'TAGCTAGCGCTATCCCCAACcaaattgctgaatattccttttcttagacgtcaggtggca', 'notes': 'Re Gibson primer on KT-537 to extract pSC101 origin of replication and CmR resistance marker', 'length': '60', 'construct number': '15'}
{'part number': '1', 'direction': 'fw primer', 'name': 'EMP798', 'primer sequence': 'aaatattctgaaatgagctgttgacaattaatcatcggctcgtataatgtgtggaattgt', 'notes': 'Fw Gibson primer on THSS303 to extract P(tac)-SynZip18-T7 fragment', 'length': '60', 'construct number': '16'}
{'part number': '1', 'direction': 're primer', 'name': 'EMP799', 'primer sequence': 'attaccgcctttgagtgagccccaatgataaccccaagggaagttttagtcaaaagcctc', 'notes': 'Re Gibson primer on THSS303 to extract P(tac)-SynZip18-T7 fragment', 'length': '60', 'construct number': '16'}
{'part number': '2', 'direction': 'fw primer', 'name': 'EMP800', 'primer sequence': 'cccttggggttatcattggggctcactcaaaggcggtaatcagataaaaaaaatccttag', 'notes': 'Fw Gibson primer on pEM103 to extract IncW backbone and TpR resistance and lacIq', 'length': '60', 'construct number': '16'}
{'part number': '2', 'direction': 're primer', 'name': 'EMP801', 'primer sequence': 'agccgatgattaattgtcaacagctcatttcagaatatttgccagaaccgttatgatgtc', 'notes': 'Re Gibson primer on pEM103 to extract IncW backbone and TpR resistance and lacIq', 'length': '60', 'construct number': '16'}
{'part number': '1', 'direction': 'fw primer', 'name': 'EMP798', 'primer sequence': 'aaatattctgaaatgagctgttgacaattaatcatcggctcgtataatgtgtggaattgt', 'notes': 'Fw Gibson primer on THSS303 to extract P(tac)-SynZip18-T7 fragment', 'length': '60', 'construct number': '17'}
{'part number': '1', 'direction': 're primer', 'name': 'EMP799', 'primer sequence': 'attaccgcctttgagtgagccccaatgataaccccaagggaagttttagtcaaaagcctc', 'notes': 'Re Gibson primer on THSS303 to extract P(tac)-SynZip18-T7 fragment', 'length': '60', 'construct number': '17'}
{'part number': '2', 'direction': 'fw primer', 'name': 'EMP800', 'primer sequence': 'cccttggggttatcattggggctcactcaaaggcggtaatcagataaaaaaaatccttag', 'notes': 'Fw Gibson primer on pEM103 to extract IncW backbone and TpR resistance and lacIq', 'length': '60', 'construct number': '17'}
{'part number': '2', 'direction': 're primer', 'name': 'EMP801', 'primer sequence': 'agccgatgattaattgtcaacagctcatttcagaatatttgccagaaccgttatgatgtc', 'notes': 'Re Gibson primer on pEM103 to extract IncW backbone and TpR resistance and lacIq', 'length': '60', 'construct number': '17'}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-26 19:42:49

问题是您正在迭代一个csv.DictReader对象,它不是一个列表,而是一个迭代器。

两者的不同之处在于,使用迭代器时,您不能“回到开头”。内部循环的每一步,在primer_list上的迭代都从上次停止的地方开始。

如果您希望能够多次迭代所有项,并且有足够的内存,请将它们存储在列表中:

代码语言:javascript
代码运行次数:0
复制
primers_list = list(csv.DictReader(primers))

如果希望保持较低的内存使用量,则可以在每次循环内从头开始创建DictReader对象。但是,这会增加一些(可能是很小的)执行时间开销,您应该注意通过将with语句移到循环中来关闭文件。

另一种方法是在循环体的末尾执行primers.seek(0),以便它在下一次迭代时从文件的开头开始读取,但我不确定这是不是一个好办法。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14540767

复制
相关文章
为什么我的样式不起作用?
大概看一下代码,是有一个Parent的父组件,蓝底白字。还有一个Child的子组件,红底黑字。 那么实际渲染出的样式是什么样子的呢。如下图:
w候人兮猗
2020/07/01
4.2K0
为什么我的模型准确率都 90% 了,却不起作用?
二元分类中有一类情况,原始数据集中的两个类出于问题性质的原因,导致其中数据点分布不平衡。举例来说,在处理用户流失(指用户在一段时间之后不再继续使用公司产品的情况)这类市场问题预测时,流失用户所占的百分比一般都会远低于留存用户的。如果说这个例子里分类是八比二的话,那么只会有 20% 的用户终止了与公司继续接触,剩下 80% 的用户则会继续使用公司产品。
深度学习与Python
2022/03/23
1.9K0
为什么我的模型准确率都 90% 了,却不起作用?
Double为什么会丢失精度
在工作中,谈到有小数点的加减乘除都会想到用BigDecimal来解决,但是有很多人对于double或者float为啥会丢失精度一脸茫然。还有BigDecimal是怎么解决的?话不多说,我们开始。
用户4283147
2022/10/08
2.4K0
Double为什么会丢失精度
innodb为什么需要double write?
因为Innodb的数据页一般是16K,但是磁盘的页一般是4K,所以写一次磁盘数据,会有4次写磁盘的原子操作,在极端情况下就可能在磁盘写完前面4K后系统断点,此时4K是新数据,后面的12K是老数据,出现了数据被破坏的情况,这就叫做页分裂。
十毛
2021/02/02
1.3K0
html的base标签为什么不起作用
我今天尝试编译一个Angular4的应用,并部署到服务器的一个路径上去,由于不是根路径因此我使用了下面的语句: ng build -prod -bh /rel 自然的Angular应用index.html里的语句就变成了: <base href="/rel"> 但,当我用http://localhost:8080/rel打开网页时却提示Loading...,打开调试发现js路径没找到,base标签没有生效,上网自学一番收获如下: https://stackoverflow.com/questions/115
孙亖
2018/06/07
1.9K0
我和Double Lift的故事(五)——升华篇​
本来这个系列写到第二篇时,写完了我学习各种Double Lift的使用方法的进步以及应用的逻辑以后,基本上也就江郎才尽了。不料最近灵感突发,决定狗尾续貂一波,再度升华一把这个扑克手法的经典。十多年的打磨和思考,总是能够不经意中给我一些意想不到的收获。
magic2728
2020/02/17
5080
为什么 strace 在 Docker 中不起作用?
在编辑“容器如何工作”爱好者杂志的能力页面时,我想试着解释一下为什么 strace 在 Docker 容器中无法工作。
用户8639654
2021/09/18
6.4K0
MYSQL Double Write 我关掉行不?
这个问题是在某个群里面,看见有人问的,已经2020年了,到底Double write 能不能关,这是一个好问题。因为有些数据库压根没有 Double write 也就没有性能上的损耗了。那为什么MYSQL 要有DOUBLE WRITE ,并且可以关吗?
AustinDatabases
2020/04/10
2.1K0
MYSQL  Double Write 我关掉行不?
我和Double Lift的故事(三)——应用篇
在前面的文章中,我们介绍了Double Lift的手法进化过程,以及相应的步骤总结和数学模型描述,这也是我自己的学习旅程,分享出来希望对大家的魔术学习,思维锻炼有所帮助。而谈到手法,最重要的特性是自然,要有一个真实的动作与之相似和对应。希望大家能从前一篇分析中以小见大,能用更合理实用的方式看待和练习魔术手法。相关内容回顾见:
magic2728
2020/01/15
4160
我和Double Lift的故事(四)——实战篇
在上一篇中,我们借Double Lift手法,介绍了如何从一个基本手法变成一个魔术表演的过程。其核心就是魔术效果的制造与呈现的分离,在后来我与郭玉文老师的探讨中,他说,这也叫“声东击西,顺手牵羊”。在手法的基础上,以及上一篇的应用理论下,我们今天来看三个作品进一步理解这里面的精髓。部分作品曾经已经分享过,但是,今天主要从Double Lift作为流程的组成成分以及如何把手法组合成魔术的角度来分析其实战中的使用。
magic2728
2020/02/17
4180
前端小知识:为什么你写的 height:100% 不起作用?
作者:JiaXinYi https://segmentfault.com/a/1190000012707337 这个知识不算冷门的,但是用的时候可能还是会有些懵逼,不能生效时搜一搜就能找到答案了,但是你真的懂了吗?为什么想要设置一个全屏元素的时候,高度不受%的控制? 1.百分比宽高的设定 按照w3c中的width和height属性,可以明确%设定宽高是根据父元素的宽高来的: http://www.w3school.com.cn/cssref/prdimwidth.asp http://www.w3schoo
企鹅号小编
2018/02/08
1.9K0
前端小知识:为什么你写的 height:100% 不起作用?
史上最强解读:Oracle里面为什么没有double write?
导读:MySQL有double write机制,PostgreSQL有full page write机制,那么Oracle里面为什么没有类似机制呢?
数据和云
2020/04/14
1.8K0
史上最强解读:Oracle里面为什么没有double write?
循环 | 这篇文章,我循环看了很久...
今天我们继续讲述关于Python的 循环语句 Python循环语句 众所周知 程序在一般情况下是按顺序执行的 Python编程语言提供了 各种控制结构以允许更复杂的执行路径 循环语句 允许我们执行一个
潘永斌
2020/02/18
3290
我是这样挑战不用 for 循环的
学Python最简单的方法是什么?推荐阅读:Python开发工程师成长魔法 为什么要挑战自己在代码里不写for loop?因为这样可以迫使你去使用比较高级、地道的语法或库。文中以python为例子,讲了不少大家其实在别人的代码里都见过、但自己很少用的语法。 这是一个挑战。我要你避免在任何情况下写for循环。同样的,我也要你找到一种场景——除了用for循环以外,用其他方法写都太难。请分享你的发现,我非常想听到这些 距离我开始探索超棒的Python语言特性已经有一段时间了。一开始,这只是我给自己的一个挑战,练
小小科
2018/05/04
8070
Android Layout的layout_height等属性为什么会不起作用?
有的时候,我们配置好的布局文件,在加载完成添加到我们的Activity中后发现,并没有安装我们设置的属性来布局,比为我们设置了android:layout_marginTop=“100dip”,但是运行程序后发现一点作用都没有,相似的还有layout_height等以android:layout_开头的属性设置都没有作用,这类问题以我们使用Adapter的作为数据源的时候作用居多,因为Adapter里有一个方法是getView,这个返回的VIew是一个从XML布局里加载的,一般如下:
飞雪无情
2018/08/28
1.3K0
为什么你的自定义View wrap_content不起作用?
在使用自定义View时,View宽 / 高的wrap_content属性不起自身应有的作用,而且是起到与match_parent相同作用。
Carson.Ho
2019/02/22
2.4K0
小心double的比较
对于double的比较,存在一些可能的坑,大家应该都清楚。比如容易注意的是double==double的精度问题。但是比较少注意的是double.NaN的比较
黄腾霄
2020/06/10
1.4K0
The method assertEquals(double, double) from the type Assert is deprecated
assertEquals(double expected,double actul) 被废弃了,换成assertEquals(double expected,double actul,double delta)
全栈程序员站长
2022/09/14
6740
float double取值范围_double float区别
要说清楚Java浮点数的取值范围与其精度,必须先了解浮点数的表示方法,浮点数的结构组成,之所以会有这种所谓的结构,是因为机器只认识01,你想表示小数,你要机器认识小数点这个东西,必须采用某种方法,比如,简单点的,float四个字节,前两个字节表示整数位,后两个字节表示小数位(这就是一种规则标准),这样就组成一个浮点数。而Java中浮点数采用的是IEEE 754标准。
全栈程序员站长
2022/11/03
1.9K0
为什么我的BERT不行?
这节只列举问题,思路放下一章。这里是给大家去定位问题的思路,通过这些渠道能发现一些问题,而不是对问题束手无策了。
lyhue1991
2023/02/23
1.2K0
为什么我的BERT不行?

相似问题

为什么我的double for循环不能正确运行?

225

为什么我不能将double[]传递给Double[]

42

Double For循环

31

为什么'operator<<(cout,double)‘不起作用?

50

我如何向量化这个double for循环?

216
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档