简体   繁体   English

循环浏览列表时跳过迭代-Python

[英]Skip an iteration while looping through a list - Python

Is there a way to skip the first iteration in this for-loop, so that I can put a for-loop inside a for-loop in order to compare the first element in the list with the rest of them. 有没有一种方法可以跳过此for循环中的第一次迭代,因此我可以将for循环放入for循环中,以便将列表中的第一个元素与其余元素进行比较。

from collections import Counter
vowelCounter = Counter()
vowelList = {'a','e','i','o','u'}
userString = input("Enter a string ")
displayed = False
for letter in userString:
    letter = letter.lower()
    if letter in vowelList:
        vowelCounter[letter] +=1
    for vowelCount1 in vowelCounter.items():
        char, count = vowelCount1
        for vowelCount2 in vowelCounter.items(STARTING AT 2)
            char2, count2 = vowelCount2
            if count > count2 : CONDITION

How would the syntax go for this? 语法将如何处理? I only need to do a 5 deep For-loop. 我只需要做一个5深度的For循环。 So the next would Start at 3, then start at 4, then 5, the the correct print statement depending on the condition. 因此下一个将从3开始,然后从4开始,然后从5开始,这取决于条件,是正确的打印语句。 Thanks 谢谢

You could do: 您可以这样做:

for vowelCount2 in vowelCounter.items()[1:]:

This will give you all the elements of vowelCounter.items() except the first one. 除第一个元素外,这将为您提供vowelCounter.items()所有元素。

The [1:] means you're slicing the list and it means: start at index 1 instead of at index 0 . [1:]意味着您要对列表进行切片,这意味着:从索引1而不是索引0 As such you're excluding the first element from the list. 因此,您将从列表中排除第一个元素。

If you want the index to depend on the previous loop you can do: 如果您希望索引依赖于上一个循环,则可以执行以下操作:

for i, vowelCount1 in enumerate(vowelCounter.items()):
    # ...
    for vowelCount2 in vowelCounter.items()[i:]:
        # ...

This means you're specifying i as the starting index and it depends on the index of vowelCount1 . 这意味着您将i指定为起始索引,并且取决于vowelCount1的索引。 The function enumerate(mylist) gives you an index and an element of the list each time as you're iterating over mylist . 每次在mylist迭代时,函数enumerate(mylist)都会为您提供索引和列表的元素。

It looks like what you want is to compare each count to every other count. 看起来您想要的是将每个计数与其他每个计数进行比较。 While you can do what you suggested, a more succinct way might be to use itertools.combinations : 尽管您可以按照建议进行操作,但更简洁的方法可能是使用itertools.combinations

for v1,v2 in itertools.combinations(vowelCounter, 2):
    if vowelCounter[v1] > vowelCounter[v2]:
        # ...

This will iterate over all pairs of vowels for comparison. 这将遍历所有成对的元音以进行比较。 Doing it this way, you may also want to check if vowelCounter[v2] > vowelCounter[v1] as you won't see these two again (this goes for this method or the nested for loop method). 这样,您可能还需要检查vowelCounter[v2] > vowelCounter[v1]因为您将不会再看到这两个(这适用于此方法或嵌套的for循环方法)。 Or, you can use the itertools.permutations function with the same arguments and just one check would suffice. 或者,您可以将itertools.permutations函数与相同的参数一起使用,只需执行一项检查即可。

To skip an iteration you can use the continue keyword eg: 要跳过迭代,可以使用continue关键字,例如:

list = [1,2,3,4,5,6,7,8,9,10]

for value in list:
  if value == list[0]:
    continue
  print(value)

Would give you: 会给你:

2
3
4
5
6
7
8
9
10

I hope this answers your question. 我希望这回答了你的问题。

Slicing a list with [1:] as suggested by a few others creates a new array. 像其他一些人建议的那样用[1:]分割列表会创建一个新数组。 It is faster and more economic to use a slice iterator with itertools.islice() itertools.islice()中使用切片迭代器更快,更经济

from itertools import islice
for car in islice(cars, 1, None):
    # do something

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

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