[英]How to do this (coded in java) in python/jython?
I'm still learning python/jython, so sorry if I ask silly questions. 我仍在学习python / jython,请问傻问题,对不起。
I have this for loop in java, but I have no idea how to code it in Python/Jython, since it insists using "in"... 我在Java中有这个for循环,但是我不知道如何在Python / Jython中编码,因为它坚持使用“ in”。
for(String effectString : config.getStringList("string.list") {
// Do something
I tried: 我试过了:
for effectString = config.getStringList("string.list"):
and 和
for effectString in config.getStringList("string.list"):
but then I realised that I didn't defined effectString and, actually, effectString is config.getStringList("string.list")... 但是后来我意识到我没有定义effectString,实际上effectString是config.getStringList(“ string.list”)...
Thanks for explanation in advance, Amar! 预先感谢您的解释,Amar!
You can use a for
loop in python to iterate over iterables
. 您可以在python中使用
for
循环对iterables
进行iterables
。
Iterables can be a list
, dict
, generator
objects, etc. 可迭代对象可以是
list
, dict
, generator
对象等。
For example 例如
>>> for num in [1, 2, 3, 4]:
... print num
1
2
3
4
>>> my_nums = ("0", "1", "2")
>>> for num in my_nums:
... print num
0
1
2
>>> my_classes = [int, float, abs, str]
>>> for cls in my_classes:
... print cls
<type 'int'>
<type 'float'>
<built-in function abs>
<type 'str'>
For more details, have a look at the Python Docs . 有关更多详细信息,请查看Python Docs 。
This syntax: 这个语法:
for effectString in config.getStringList("string.list"):
is correct. 是正确的。 When using
for
loops in Python you implicitly create a local variable. 在Python中使用
for
循环时,您会隐式创建一个局部变量。
In some languages, for (i=0; i<10; i++)
might be how a for
loop would look. 在某些语言中,
for (i=0; i<10; i++)
可能是for
循环的外观。 There, you explicitly initialize the variable i
, to be used in the body of the loop. 在那里,您显式初始化变量
i
,以在循环体中使用。 In Python, for something in iterable
creates the local variable something
. 在Python中,
for something in iterable
创建局部变量something
。
It is also worth noting that the variable persists after the loop is finished: 还值得注意的是,变量在循环完成后仍然存在:
>>> for i in range(10):
pass
>>> print(i)
9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.