有没有一种Python式的方法可以知道for中的第一个和最后一个循环何时被传递?
我有一个模板,其中放置了 5 个表格,但除了第一个表格外,所有表格都无法发布。仅当我单击首先启用下一个表单的按钮时,才能填写下一个表单。
我正在寻找一种方法,在验收测试中的 for 循环中实现类似 Django 的 forloop.last templatetag 变量,以决定是否执行启用下一个表单的方法。
基本上我需要做的是这样的:
for form_data in step.hashes:
# get and fill the current form with data in form_data
if not forloop.last:
# click the button that enables the next form
# submit all filled forms
I have a template in which I placed, let's say 5 forms, but all disabled to be posted except for the first one. The next form can only be filled if I click a button that enables it first.
I'm looking for a way to implement a Django-like forloop.last templatetag variable in a for loop inside an acceptance test to decide whether to execute a method that enables the next form or not.
Basically what I need to do is something like:
for form_data in step.hashes:
# get and fill the current form with data in form_data
if not forloop.last:
# click the button that enables the next form
# submit all filled forms
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我不知道任何内置的东西,但是您可以轻松编写一个生成器来为您提供所需的信息:
I don't know of anything built-in, but you can easily write a generator to give you the required information:
您可以使用
enumerate
并将计数器与列表的长度进行比较:You could use
enumerate
and compare the counter with the length of the list:不喜欢重复
获取并使用 form_data 中的数据填充当前表单
?定义一个函数。Don't like the repetition of
get and fill the current form with data in form_data
? Define a function.带缓冲器的发生器。
压缩两个序列
Generator with buffer.
Zipping two sequences
我认为他希望在迭代器周围有一个包装器来提供第一个/最后一个查询,
参数也可以是一个迭代器,所以所有类型的 len() 都会失败
这就是我到目前为止所想到的,技巧是使用双迭代器,一个向前看的迭代器
第一步:
返回:
I think he wants to have a wrapper around the iterator that provides first / last queries,
also the parameter could be an iterator so all sort of len() would faild
Here it is what I came up so far, the trick is to use a double iterator, one that looks ahead
one step of the first:
returns:
如果我正确理解你的问题,你想要一个简单的测试来看看你是在列表的开头还是结尾?
如果是这种情况,可以这样做:
对于列表中的第一项,您可以将“-1”替换为 0。
If I am understanding your question correctly, you want a simple test for whether you are at the beginning or end of the list?
If that's the case, this would do it:
For the first item in the list, you would replace "-1" with 0.
在任何人削尖火把或点燃干草叉之前,我对什么是 Pythonic 并不是专家,也就是说,在我看来,如果
first
和/或last
是从列表中需要的,在循环内的iffirst
或iflast
中,似乎期望super
类,并添加所需的功能...也许,因此,接下来的内容完全是预阿尔法版本 e-1^11% 的代码,如果在周二以正确的方式查看,可能会造成严重破坏……而且它可能比那位朋友的灯具更麻烦公开谈论亲密接触,他们知道自己是谁......但这确实做了一些漂亮的技巧
示例输入一
示例输出一
示例输入二
示例输出二
如果你觉得你的大脑变得清新,但它有点不是完全没问题,这是它自己的一切都更好......对我来说,这是最Pythonic
享受的,也许如果有兴趣,我会将其推送到 GitHub 以供所有人 Pull 和 Fork。
Before anyone goes sharpening their torches or igniting the pitch-forks I'm no expert in what is Pythonic, that stated, seems to me that if
first
and/orlast
is wanted from a list, in the since ofif first
orif last
within a loop, it seems expected tosuper
the class, and add-in the functionality that is desired... maybe, so what follows is totally pre-alpha version e-1^11% sorta code that may cause havoc if looked at on a Tuesday just the right way...... and it maybe buggier than the light fixtures of that one friend that talked to publicly of close encounters, they know who they are... but this does do some nifty tricks
Example input one
Example output one
Example input two
Example output two
If you feel your brains go all minty fresh, but it kinda isn't totally okay and that is in it's own way all the better... to me that be the most Pythonic
Enjoy, and perhaps if there be interest I'll push this to GitHub for all to Pull and Fork.