有没有办法在循环使用for循环时访问列表(或元组,或其他可迭代)的下一个或前一个元素?
l=[1,2,3]
for item in l:
if item==2:
get_previous(l,item)
Run Code Online (Sandbox Code Playgroud) 我正在尝试运行此代码:
import web
urls = (
'/', 'index'
)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
Run Code Online (Sandbox Code Playgroud)
但它每次都给我这个错误
C:\Users\aidke\Desktop>python app.py
Traceback (most recent call last):
File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\utils.py", line 526, in take
yield next(seq)
StopIteration
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "app.py", line 14, in <module>
app = web.application(urls, globals())
File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\application.py", line 62, in __init__
self.init_mapping(mapping)
File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\application.py", line 130, in init_mapping
self.mapping = list(utils.group(mapping, 2))
File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\utils.py", …Run Code Online (Sandbox Code Playgroud) 在审阅时,我有时会遇到这种循环:
i = begin
while ( i != end ) {
// ... do stuff
if ( i == end-1 (the one-but-last element) ) {
... do other stuff
}
increment i
}
Run Code Online (Sandbox Code Playgroud)
然后我问这个问题:你会写这个吗?
i = begin
mid = ( end - begin ) / 2 // (the middle element)
while ( i != end ) {
// ... do stuff
if ( i > mid ) {
... do other stuff
}
increment i
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这超过了编写循环的意图:你循环,因为每个元素都有一些共同点.使用此构造,对于某些元素,您可以执行不同的操作.因此,我总结说,您需要为这些元素单独的循环:
i = begin …Run Code Online (Sandbox Code Playgroud) language-agnostic loops for-loop control-structure while-loop
是否有一种简单的方法来检测最后一次迭代,同时使用iteritems()?
基本上我希望能够在循环迭代中告诉我何时在第N个项目上.有什么想法吗?
d = {1:2, 3:4, 5:6, 7:8, 9:0}
for x in d:
if last item: # <-- this line is psuedo code
print "last item :", x
else:
print x
Run Code Online (Sandbox Code Playgroud) 我正在使用Python 3,并尝试检测项目是否是列表中的最后一项,但有时会重复.这是我的代码:
a = ['hello', 9, 3.14, 9]
for item in a:
print(item, end='')
if item != a[-1]:
print(', ')
Run Code Online (Sandbox Code Playgroud)
我希望这个输出:
hello,
9,
3.14,
9
Run Code Online (Sandbox Code Playgroud)
但我得到这个输出:
hello,
93.14,
9
Run Code Online (Sandbox Code Playgroud)
我理解为什么我得到了我不想要的输出.我宁愿我仍然可以使用循环,但我可以解决它们.(我想用更复杂的代码来使用它)
如果有一个列表,比方说a=[1,2,3],我希望看到if a[4] is null,有没有办法做到这一点,而不使用异常或断言?
我正在编写一段代码,应该输出用逗号分隔的项目列表.该列表使用for循环生成.我用
for x in range(5):
print(x, end=",")
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何摆脱列表中最后一个条目添加的最后一个逗号.它输出这个:
0,1,2,3,4,
Run Code Online (Sandbox Code Playgroud)
如何删除结尾','?
mylist = [{'a':1,'b':2},{'a':3,'b':'10'},.....]
I want to do some special operations for the last itemin loop (iterarale), what to do?
for item in mylist:
do some operations for all items
#I want to execute the next statement only for last item
last_b = item[b]
last_b
Run Code Online (Sandbox Code Playgroud)
执行此操作的最佳方法是什么(使用if语句)
目前,我正在做类似的事情
count = 0
for item in list:
if count == len(list) - 1:
<do something>
else:
<do something else>
count += 1
Run Code Online (Sandbox Code Playgroud)
有没有更多的Python方式可以识别循环的最终迭代-对于列表和字典?
我想迭代文件的行,并为每个文件打印一些输出.,\n除最后一行外,所有打印的行最后都应该有一行.
我的第一种方法是使用查找hasNext()不存在的方法.我知道StopIteration引发了异常,但我不确定如何以Pythonic的方式使用它来实现我想要的.
问题是我得到了 while 循环中的所有元素而不是最终结果。有人知道如何让 python 仅在终端中插入最后一个元素吗?
我尝试插入以下循环的结果:从 74 中减去 6,42 次。我使用以下代码得到的结果应该是 -178:
i = 74
while i > -180:
print(i)
i = i - 6
Run Code Online (Sandbox Code Playgroud)
我希望将结果打印在一个元素(最后一个元素)中,而不是打印到 -178 的所有数字。
python ×12
dictionary ×2
for-loop ×2
list ×2
loops ×2
python-3.x ×2
while-loop ×2
helper ×1
iterator ×1
printing ×1
web ×1