可能重复:
for循环中的Python循环计数器
我知道我可以在循环之前设置一个整数,并将它与循环一起递增.
我在问是否有另一种方式
例如
for object in objects:
print counter somehow
Run Code Online (Sandbox Code Playgroud) 我想遍历列表并引用我所在的迭代编号。我可以使用一个简单的计数器来完成此操作,但是有内置函数吗?
List= list(range(0,6))
count = 0
for item in List:
print "This is interation ", str(count)
count += 1
Run Code Online (Sandbox Code Playgroud) 所以我有一个字符串source,我用迭代器循环:
iterator = iter(source)
for char in iterator:
do stuff
Run Code Online (Sandbox Code Playgroud)
但是,现在说我有一个签入do stuff,我将迭代器的值与'h'进行比较.然后我想以某种方式看看'h'是否后跟"ello",然后将前十个字符添加到列表中.
为此,我自己的想法是找出哪个索引与迭代器的当前位置相对应,以便我可以说:
indIt = index(char)
if source[indIt + 1: indIt + 6] == "ello ":
someList.append(source[indIt + 7:indIt + 16])
indIt += 17
char = indIt #which may also be fun to know how it can be done, if
Run Code Online (Sandbox Code Playgroud)
这意味着对于给定的输入hello Sandra, oh and hello Oscar, i welcome you both!,someList将包含["Sandra,oh","Oscar,i w"].
那么,我可以通过某种方式确定迭代器当前位置对应的索引吗?
我一直在这里阅读CSV模块上的Python文档:https://docs.python.org/2/library/csv.html
我注意到它没有提到任何方式让读者对象返回它当前正在读取的行数,或者它当前处于哪个迭代.这似乎是一个广泛需要的功能.所以,我想知道如何做到这一点:
reader = csv.DictReader(readFile)
for row in reader:
# do something
# now want to find the line number that row corresponds to
Run Code Online (Sandbox Code Playgroud)
或者,如果不可能,为什么会这样.
我今天早些时候在 Python 中尝试了 for 循环和列表,但我对这可能非常简单的一件事有点困惑......这是我的代码:
animals = ["hamster","cat","monkey","giraffe","dog"]
print("There are",len(animals),"animals in the list")
print("The animals are:",animals)
s1 = str(input("Input a new animal: "))
s2 = str(input("Input a new animal: "))
s3 = str(input("Input a new animal: "))
animals.append(s1)
animals.append(s2)
animals.append(s3)
print("The list now looks like this:",animals)
animals.sort()
print("This is the list in alphabetical order:")
for item in animals:
count = count + 1
print("Animal number",count,"in the list is",item)
Run Code Online (Sandbox Code Playgroud)
无论出于何种原因,计数变量都不起作用,我试图搜索此问题,但找不到任何内容。它说它没有定义,但是如果我输入一个普通数字或一个字符串,它就可以正常工作。(我现在也生病了,所以我无法正常思考,所以这可能很简单,我只是没有抓住它)我是否必须创建一个新的 for 循环?因为当我这样做时:
for item in animal:
for i in range(1,8):
print("Animal …Run Code Online (Sandbox Code Playgroud) 出于某种原因,我在 python 3.9.x 中得到了一个未解决的引用 'i' 错误
如何修改我的代码以使其正常运行?
for card in range(len(cards)):
card = cards[i]
print(f"{i+1} - {card}")
# get choice
choice = get_int_input(1, len(cards))
# get card
return cards[choice-1] if choice else None
Run Code Online (Sandbox Code Playgroud)
'''
我有2个清单
lst = [1,2,3,4,5,1,2,1,2,3]
lst2 = [1,2,3]
Run Code Online (Sandbox Code Playgroud)
我试图看到索引元素lst在lst2.目前我在做;
ind = []
for x in lst2:
if x in lst:
ind.append(lst.index(x))
Run Code Online (Sandbox Code Playgroud)
我意识到问题是lst.index(x)只返回第一次出现的元素,所以x=1总会返回lst.index(1) = 0.
有没有办法返回包含该元素的所有索引?
我发现自己需要借助for循环来遍历列表。我最终要做的是:
L = ['A','B','C','D']
n = 0
for i in L:
print(L[n])
n += 1
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好的方法来执行此操作,而不必n每次都声明一个额外的变量?
请记住,这只是一个简化的示例。这样的解决方案是不够的(尽管在此示例中,结果是相同的):
L = ['A','B','C','D']
for i in L:
print(i)
Run Code Online (Sandbox Code Playgroud)