Python equivalent of Ruby's each

This question already has an answer here: Accessing the index in 'for' loops? 17 answers 就像是: for i, v in enumerate(a): print "{} : {}".format(i, v) That'd be enumerate. a=['a','b','c'] for i,v in enumerate(a): print "%i : %s" % (i, v) Prints 0 : a 1 : b 2 : c

相当于Ruby的Python

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 就像是: for i, v in enumerate(a): print "{} : {}".format(i, v) 这将被列举。 a=['a','b','c'] for i,v in enumerate(a): print "%i : %s" % (i, v) 打印 0 : a 1 : b 2 : c

find position of item in for loop over a sequence

Possible Duplicate: Accessing the index in Python for loops list = [1,2,2,3,5,5,6,7] for item in mylist: ... How can I find the index of the item I am looking at, at some point in my loop? I can see there is a index() method for lists but it will always give me the first index of a value, so it won't work for lists with duplicate items 看看枚举>>> for i, season in enumerat

找到项目的位置在for循环一个序列

可能重复: 在Python中访问循环的索引 list = [1,2,2,3,5,5,6,7] for item in mylist: ... 我怎样才能找到我正在看的项目的索引,在我的循环中的某个点? 我可以看到有一个index()的方法lists ,但它总是给我一个值的第一指标,因此它不会与重复的项目列表工作 看看枚举>>> for i, season in enumerate('Spring Summer Fall Winter'.split(), start=1): print i, season 1 Spring 2 Summer 3 Fall 4

Creating a counter inside a Python for loop

This question already has an answer here: Accessing the index in 'for' loops? 17 answers This (creating an extra variable before the for-loop) is not pythonic . The pythonic way to iterate over items while having an extra counter is using enumerate : for index, item in enumerate(iterable): print(index, item) So, for example for a list lst this would be: lst = ["a", "b", "c"]

在Python for循环中创建一个计数器

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 这(在for循环之前创建一个额外的变量)不是pythonic。 在具有额外计数器时迭代项目的pythonic方法是使用enumerate : for index, item in enumerate(iterable): print(index, item) 因此,例如对于一个列表lst这将是: lst = ["a", "b", "c"] for index, item in enumerate(lst): print(index, item) ...并生成输出: 0 a 1 b 2 c

How to get the index of the iterator object?

This question already has an answer here: Accessing the index in 'for' loops? 17 answers Iterators were not designed to be indexed (remember that they produce their items lazily). Instead, you can use enumerate to number the items as they are produced: for index, match in enumerate(it): Below is a demonstration: >>> it = (x for x in range(10, 20)) >>> for index

如何获得迭代器对象的索引?

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 迭代器没有被设计为索引(请记住,他们懒洋洋地制作他们的项目)。 相反,您可以使用enumerate为产生的项目编号: for index, match in enumerate(it): 下面是一个演示: >>> it = (x for x in range(10, 20)) >>> for index, item in enumerate(it): ... print(index, item) ... 0 10 1 11 2 12 3 13 4 14 5 15 6 16 7

How to output an index while iterating over an array in python

This question already has an answer here: Accessing the index in 'for' loops? 17 answers 喜欢这个: for index, g in enumerate(games[0:4]): g.output(index) 使用内置的enumerate方法: for i,a in enumerate(['cat', 'dog']): print '%s is %d' % (a, i) # output: # cat is 0 # dog is 1

如何在python中迭代数组时输出索引

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 喜欢这个: for index, g in enumerate(games[0:4]): g.output(index) 使用内置的enumerate方法: for i,a in enumerate(['cat', 'dog']): print '%s is %d' % (a, i) # output: # cat is 0 # dog is 1

How to get list index and element simultaneously in Python?

This question already has an answer here: Accessing the index in 'for' loops? 17 answers You can use enumerate : for k,i in enumerate(mylist): #do something with index k #do something with element i More information about looping techniques. Edit: As pointed out in the comments, using other variable names like for i, item in enumerate(mylist): makes it easier to read

如何在Python中同时获取列表索引和元素?

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 你可以使用enumerate : for k,i in enumerate(mylist): #do something with index k #do something with element i 有关循环技术的更多信息。 编辑: 正如在评论中指出的那样,使用其他变量名称 for i, item in enumerate(mylist): 使得从长远来看更容易阅读和理解你的代码。 通常你应该使用i , j , k来表示数字和有意义的变量

Python For loop get index

This question already has an answer here: Accessing the index in 'for' loops? 17 answers 使用enumerate()函数生成索引以及要循环的序列元素: for index, w in enumerate(loopme): print "CURRENT WORD IS", w, "AT CHARACTER", index Do you want to iterate over characters or words? For words, you'll have to split the words first, such as for index, word in enumerate(loopme.split("

Python for循环获取索引

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 使用enumerate()函数生成索引以及要循环的序列元素: for index, w in enumerate(loopme): print "CURRENT WORD IS", w, "AT CHARACTER", index 你想迭代字符或单词吗? 换句话说,你必须首先分词,例如 for index, word in enumerate(loopme.split(" ")): print "CURRENT WORD IS", word, "AT INDEX", index 这将打印单词的索引。

Loop through list with both content and index

This question already has an answer here: Accessing the index in 'for' loops? 17 answers 使用enumerate内置函数:http://docs.python.org/library/functions.html#enumerate 使用enumerate() : >>> S = [1,30,20,30,2] >>> for index, elem in enumerate(S): print(index, elem) (0, 1) (1, 30) (2, 20) (3, 30) (4, 2) Like everyone else: for i, val in enumerate(data):

循环播放内容和索引列表

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 使用enumerate内置函数:http://docs.python.org/library/functions.html#enumerate 使用enumerate() : >>> S = [1,30,20,30,2] >>> for index, elem in enumerate(S): print(index, elem) (0, 1) (1, 30) (2, 20) (3, 30) (4, 2) 像其他人一样: for i, val in enumerate(data): print i, val 但也 for i, val

Only index needed: enumerate or (x)range?

If I want to use only the index within a loop, should I better use the range/xrange function in combination with len() a = [1,2,3] for i in xrange(len(a)): print i or enumerate ? Even if I won't use p at all? for i,p in enumerate(a): print i 我会使用enumerate因为它更通用 - 例如它可以在迭代和序列上工作,而仅仅返回对象引用的开销并不是什么大不了的事 - 虽然xrange(len(something))

只需要索引:枚举或(x)范围?

如果我只想在循环中使用索引,我应该更好地将range/xrange函数与len()结合使用 a = [1,2,3] for i in xrange(len(a)): print i 或enumerate ? 即使我根本不使用p ? for i,p in enumerate(a): print i 我会使用enumerate因为它更通用 - 例如它可以在迭代和序列上工作,而仅仅返回对象引用的开销并不是什么大不了的事 - 虽然xrange(len(something))虽然(对我来说)按照您的意图更容易读取 - 将不支持len对象打

Should you always favor xrange() over range()?

为什么或者为什么不? For performance, especially when you're iterating over a large range, xrange() is usually better. However, there are still a few cases why you might prefer range() : In python 3, range() does what xrange() used to do and xrange() does not exist. If you want to write code that will run on both Python 2 and Python 3, you can't use xrange() . range() can actually b

如果你总是喜欢xrange()超过范围()?

为什么或者为什么不? 为了提高性能,特别是在迭代大范围时, xrange()通常更好。 但是,仍然有一些情况为什么你可能更喜欢range() : 在Python 3中, range()执行xrange()用于执行的操作,而xrange()不存在。 如果您想编写可在Python 2和Python 3上运行的代码,则不能使用xrange() 。 range()在某些情况下实际上可能更快 - 例如。 如果多次迭代相同的序列。 xrange()必须每次重建整数对象,但range()将具有实数整数对象