Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不带索引的python循环_Python_Loops_Indexing - Fatal编程技术网

不带索引的python循环

不带索引的python循环,python,loops,indexing,Python,Loops,Indexing,我正在学习python,我喜欢在无需不断创建索引变量的情况下通过iterable对象进行循环 但我发现自己一直在创建索引变量,这样我就可以在来自并行对象的调用中引用它们。比如当我比较两个列表,或者一个列表和一本字典 应要求:一些例子 `s= "GAGCCTACTAACGGGAT"# the strings we are evaluating t= "CATCGTAATGACGGCCT" c = 0# c = position of interest within the strings m =

我正在学习python,我喜欢在无需不断创建索引变量的情况下通过iterable对象进行循环

但我发现自己一直在创建索引变量,这样我就可以在来自并行对象的调用中引用它们。比如当我比较两个列表,或者一个列表和一本字典

应要求:一些例子

`s= "GAGCCTACTAACGGGAT"# the strings we are evaluating
t= "CATCGTAATGACGGCCT"

c = 0# c = position of interest within the strings
m = 0 # m = number of mutations found so far. 

for i in s: # cycling through all nucleotides listed in s
if(i == t[c]): #compare s[c] to t[c]
    c +=1 # if equal, increase position counter
else:
    c+=1 # if not equal, increase both position and                 
    m+=1        #mutation counters.`

def allPossibleSubStr(s):#获取字符串的dict,查找最短的字符串,并生成所有可能的子字符串的列表,从而为原始dict的所有元素生成可能的公共子字符串的列表
ks=s.keys()
c=0#计数器
j=0#ks最短字符串的占位符
subSTR=[]
对于ks中的i:#在stringDict中查找最短的条目
如果(s[i]
有办法解决这个问题吗

使用枚举

for i, item in enumerate(myList):
  foo = parallelList[i]
或者拉链

for item, foo in zip(myList, parallelList):
  ...

听起来您编写的代码如下:

for i, item in enumerate(list1):
    if item == list2[i]:
        ...
您仍然不需要显式索引,因为您可以压缩这两个列表并迭代元组列表

for item1, item2 in zip(list1, list2):
    if item1 == item2:
您可以对两个dict执行相同的操作,但由于它们是无序的,您可能希望在排序后压缩它们的键:

for key1, key2 in zip(sorted(dict1), sorted(dict2)):

请举例说明你的意思?请举例说明你需要将索引与什么一起使用。根据您的用例,可能还有其他方法。例如,如果您有两个需要同时迭代的列表,您可以使用
zip()
。我刚刚快速查看了我的多个python项目中的1000多个循环,使用索引的不到10个(使用
enumerate
)。所以,是的,很可能你忽略了一些更好的方式来表达你的意图。但这并不能真正帮助避免索引。(添加了一个zip示例)我不得不使用这些。我以前没有见过这些命令。zip和enumerate。
for key1, key2 in zip(sorted(dict1), sorted(dict2)):