Is it possible to tell a For loop to start from a specific location?
是否可以告诉For循环从特定位置开始?
>>> languages = ["C", "C++", "Perl", "Python"]
>>> for x in languages:
... print(x)
...
C
C++
Perl
Python
>>>
How can I get the script to begin from "Perl"? I don't need it to loop back around.
如何让脚本从“Perl”开始?我不需要它循环回来。
6 个解决方案
#1
2
languages = ["C", "C++", "Perl", "Python"]
# add start index range by the index of "Perl"
for x in languages[languages.index("Perl"):]:
print(x)
# Perl
# Python
for x in languages[languages.index("C++"):]:
print(x)
# C+=
# Perl
# Python
#2
2
Lists can be sliced by their indices using myList[start:stop:skip]
. Starting from the third index (zero-indexing), no stopping, and no skipping of indices, would be:
可以使用myList [start:stop:skip]通过索引对列表进行切片。从第三个索引(零索引)开始,没有停止,也没有跳过索引,将是:
for x in languages[2::]:
print x
would give you:
会给你:
Perl
Python
See also: Explain Python's slice notation
另请参阅:解释Python的切片表示法
#3
1
You could use itertools.dropwhile
for this:
您可以使用itertools.dropwhile:
>>> import itertools
>>> languages = ["C", "C++", "Perl", "Python"]
>>> for language in itertools.dropwhile(lambda x: x != 'Perl', languages):
... print(language)
...
Perl
Python
>>>
itertools.dropwhile
takes two arguments, a function that determines whether a list element is equal to your desired starting value, and the iterable - a list in your case - that you want to loop over. dropwhile
will skip elements until it finds one that satisfies the function, then it will output all elements until the iterable is exhausted.
itertools.dropwhile接受两个参数,一个确定列表元素是否等于所需起始值的函数,以及你想要循环的iterable(在你的情况下是一个列表)。 dropwhile将跳过元素,直到找到满足该函数的元素,然后它将输出所有元素,直到iterable耗尽为止。
This is useful if your criteria for selecting a start point are more complex than simple equality. For example, if you wanted to start from the first element that starts with "P" you can do:
如果选择起点的标准比简单相等更复杂,则此选项非常有用。例如,如果您想从以“P”开头的第一个元素开始,您可以执行以下操作:
>>> for language in itertools.dropwhile(lambda x: not x.startswith('P'), languages):
... print(language)
...
Perl
Python
>>>
The reverse of this is itertools.takewhile
, which will iterate for as long as the provided function is satisfied.
与此相反的是itertools.takewhile,只要满足所提供的函数,它就会迭代。
#4
1
Try this. I hope this will help u
尝试这个。我希望这会对你有所帮助
languages = ["C", "C++", "Perl", "Python"]
for k in range(0,len(languages)):
print(languages[k-2])
and the output is :
输出是:
Perl
Python
C
C++
In python the for loop function is (for k in x(start,stop,step)). Here x is your list or string variable and
在python中,for循环函数是(对于x中的k(开始,停止,步骤))。这里x是你的列表或字符串变量
start : Indicates where the for loop should start and the index starts from zero.
stop : Indicates where the for loop ends. It takes up to (n-1) elements.
For example :
例如 :
for k in range(0,100):
it gives output till 99 from zero and if u want output of 100. U should mention like this :
它从零开始输出到99,如果你想输出100.你应该这样提到:
n = 100
for k in range(n+1):
print k
the output is from 0 to 100. In this case for loop will take indexes (starts) from zero.
输出从0到100.在这种情况下,for循环将从零开始索引(起始)。
step : Indicates how many steps should do in for loop. By default step is one in for loop.
for example:
例如:
for k in range(0,10,1):
print k
#output is 0,1,2,3,4,5,6,7,8,9
for k in range(0,10,2):
print k
#output is 0,2,4,6,8
for k in range(0,10,3):
print k
#output is 0,3,6,9
#5
1
You can try :
你可以试试 :
languages = ["C", "C++", "Perl", "Python"]
for i in range(languages.index("Perl"),len(languages)):
# 1st parameter of range will find Perl's index from list languages
#2nd parameter - Length of list
print languages[i]
Perl
Python
#6
0
If you want elements from start of perl to the end of list
如果你想要从perl开始到列表末尾的元素
languages = ["C", "C++", "Perl", "Python"]
print languages
Its very easy by list slicing
通过列表切片非常容易
print languages[2:]
output :
输出:
['Perl', 'Python']
you can loop this list and get elements on separate line.
你可以循环这个列表并在单独的行上获取元素。
And Even if you want to get the loopback elements from perl to back-to-perl then also you can use list slicing
即使你想要从perl到back-to-perl的环回元素,你也可以使用列表切片
a = []
a.extend(languages[2:])
a.extend(languages[:2])
print a
output:
输出:
['Perl', 'Python', 'C', 'C++']
#1
2
languages = ["C", "C++", "Perl", "Python"]
# add start index range by the index of "Perl"
for x in languages[languages.index("Perl"):]:
print(x)
# Perl
# Python
for x in languages[languages.index("C++"):]:
print(x)
# C+=
# Perl
# Python
#2
2
Lists can be sliced by their indices using myList[start:stop:skip]
. Starting from the third index (zero-indexing), no stopping, and no skipping of indices, would be:
可以使用myList [start:stop:skip]通过索引对列表进行切片。从第三个索引(零索引)开始,没有停止,也没有跳过索引,将是:
for x in languages[2::]:
print x
would give you:
会给你:
Perl
Python
See also: Explain Python's slice notation
另请参阅:解释Python的切片表示法
#3
1
You could use itertools.dropwhile
for this:
您可以使用itertools.dropwhile:
>>> import itertools
>>> languages = ["C", "C++", "Perl", "Python"]
>>> for language in itertools.dropwhile(lambda x: x != 'Perl', languages):
... print(language)
...
Perl
Python
>>>
itertools.dropwhile
takes two arguments, a function that determines whether a list element is equal to your desired starting value, and the iterable - a list in your case - that you want to loop over. dropwhile
will skip elements until it finds one that satisfies the function, then it will output all elements until the iterable is exhausted.
itertools.dropwhile接受两个参数,一个确定列表元素是否等于所需起始值的函数,以及你想要循环的iterable(在你的情况下是一个列表)。 dropwhile将跳过元素,直到找到满足该函数的元素,然后它将输出所有元素,直到iterable耗尽为止。
This is useful if your criteria for selecting a start point are more complex than simple equality. For example, if you wanted to start from the first element that starts with "P" you can do:
如果选择起点的标准比简单相等更复杂,则此选项非常有用。例如,如果您想从以“P”开头的第一个元素开始,您可以执行以下操作:
>>> for language in itertools.dropwhile(lambda x: not x.startswith('P'), languages):
... print(language)
...
Perl
Python
>>>
The reverse of this is itertools.takewhile
, which will iterate for as long as the provided function is satisfied.
与此相反的是itertools.takewhile,只要满足所提供的函数,它就会迭代。
#4
1
Try this. I hope this will help u
尝试这个。我希望这会对你有所帮助
languages = ["C", "C++", "Perl", "Python"]
for k in range(0,len(languages)):
print(languages[k-2])
and the output is :
输出是:
Perl
Python
C
C++
In python the for loop function is (for k in x(start,stop,step)). Here x is your list or string variable and
在python中,for循环函数是(对于x中的k(开始,停止,步骤))。这里x是你的列表或字符串变量
start : Indicates where the for loop should start and the index starts from zero.
stop : Indicates where the for loop ends. It takes up to (n-1) elements.
For example :
例如 :
for k in range(0,100):
it gives output till 99 from zero and if u want output of 100. U should mention like this :
它从零开始输出到99,如果你想输出100.你应该这样提到:
n = 100
for k in range(n+1):
print k
the output is from 0 to 100. In this case for loop will take indexes (starts) from zero.
输出从0到100.在这种情况下,for循环将从零开始索引(起始)。
step : Indicates how many steps should do in for loop. By default step is one in for loop.
for example:
例如:
for k in range(0,10,1):
print k
#output is 0,1,2,3,4,5,6,7,8,9
for k in range(0,10,2):
print k
#output is 0,2,4,6,8
for k in range(0,10,3):
print k
#output is 0,3,6,9
#5
1
You can try :
你可以试试 :
languages = ["C", "C++", "Perl", "Python"]
for i in range(languages.index("Perl"),len(languages)):
# 1st parameter of range will find Perl's index from list languages
#2nd parameter - Length of list
print languages[i]
Perl
Python
#6
0
If you want elements from start of perl to the end of list
如果你想要从perl开始到列表末尾的元素
languages = ["C", "C++", "Perl", "Python"]
print languages
Its very easy by list slicing
通过列表切片非常容易
print languages[2:]
output :
输出:
['Perl', 'Python']
you can loop this list and get elements on separate line.
你可以循环这个列表并在单独的行上获取元素。
And Even if you want to get the loopback elements from perl to back-to-perl then also you can use list slicing
即使你想要从perl到back-to-perl的环回元素,你也可以使用列表切片
a = []
a.extend(languages[2:])
a.extend(languages[:2])
print a
output:
输出:
['Perl', 'Python', 'C', 'C++']