如何迭代序列和非序列? [英] how to iterate over sequence and non-sequence ?

查看:59
本文介绍了如何迭代序列和非序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我动态生成一系列值,

但是这个序列是也可以有1或甚至长度0.


所以我得到一些形式的行:

line =''(2,3,4 )''

line =''''

line =''(2)''

(事实上这些不是常数,但所有类型的整数

变量,来自整个程序,从树中选择,

显示所有可到达变量)


所以实际上我从exec语句中得到了这个值,比如这个

exec''signals =''+ line


现在我想迭代信号,如果有2个b $ b或更多的信号,这是完美的,

但是当我没有或只有1个信号时它会失败。

信号值:

做点什么


因为这意味着实时信号,我想要它快,所以(我认为)我

无法承受大量测试。


那里有任何智能解决方案吗?


谢谢,

S. tef Mientki

hello,

I generate dynamically a sequence of values,
but this "sequence" could also have length 1 or even length 0.

So I get some line in the form of:
line = ''(2,3,4)''
line = ''''
line = ''(2)''
(in fact these are not constant numbers, but all kind of integer
variables, coming from all over the program, selected from a tree, that
shows all "reachable" variables)

So in fact I get the value from an exec statement, like this
exec ''signals = '' + line

Now I want to iterate over "signals", which works perfect if there are 2
or more signals,
but it fails when I have none or just 1 signal.
for value in signals :
do something

As this meant for real-time signals, I want it fast, so (I think) I
can''t afford extensive testing.

Any smart solution there ?

thanks,
Stef Mientki

推荐答案

10月19日上午12:24,stef mientki< stef.mien ... @ gmail.comwrote:
On Oct 19, 12:24 am, stef mientki <stef.mien...@gmail.comwrote:

我动态生成一系列值,

但是这个序列是也可以有1或甚至长度0.


所以我得到一些形式的行:

line =''(2,3,4 )''

line =''''

line =''(2)''

(事实上这些不是常数,但所有类型的整数

变量,来自整个程序,从树中选择,

显示所有可到达变量)


所以实际上我从exec语句中得到了这个值,比如这个

exec''signals =''+ line


现在我想迭代信号,如果有2个b $ b或更多的信号,这是完美的,

但是当我没有或只有1个信号时它会失败。

信号值:

做点什么


因为这意味着实时信号,我想要它快,所以(我认为)我

无法承受大量测试。


那里有任何智能解决方案吗?
I generate dynamically a sequence of values,
but this "sequence" could also have length 1 or even length 0.

So I get some line in the form of:
line = ''(2,3,4)''
line = ''''
line = ''(2)''
(in fact these are not constant numbers, but all kind of integer
variables, coming from all over the program, selected from a tree, that
shows all "reachable" variables)

So in fact I get the value from an exec statement, like this
exec ''signals = '' + line

Now I want to iterate over "signals", which works perfect if there are 2
or more signals,
but it fails when I have none or just 1 signal.
for value in signals :
do something

As this meant for real-time signals, I want it fast, so (I think) I
can''t afford extensive testing.

Any smart solution there ?



首先:不要将数据收集到字符串中 - python有许多容器

类型你可以使用。


接下来,你的字符串看起来像是应该包含元组。实际上,元组有点尴尬,因为你必须使用

''(a,'')来表示一个元素的元组 - (2)isn' '长度为1的元组,

它与2相同。要么应对这种特殊情况,要么使用列表。

无论哪种方式,你都会必须使用()或[]作为空序列。


-

Paul Hankin

First: don''t collect data into strings - python has many container
types which you can use.

Next, your strings look like they''re supposed to contain tuples. In
fact, tuples are a bit awkward sometimes because you have to use
''(a,'') for a tuple with one element - (2) isn''t a tuple of length one,
it''s the same as 2. Either cope with this special case, or use lists.
Either way, you''ll have to use () or [] for an empty sequence.

--
Paul Hankin


On Fri,2007年10月19日01:24:09 +0200,stef mientki写道:
On Fri, 19 Oct 2007 01:24:09 +0200, stef mientki wrote:

你好,


我动态地生成一系列值,但是这个序列是可以

也有长度1或甚至长度为0.


所以我得到一些形式的行:

line = ''(2,3,4)''

line =''''

line =''(2)''

(实际上这些不是常数,而是所有类型的整数

变量,来自整个程序,从树中选择,

显示所有可达 ;变量)


所以实际上我得到了一个exec语句的值,比如这个

exec''signals =''+ line
hello,

I generate dynamically a sequence of values, but this "sequence" could
also have length 1 or even length 0.

So I get some line in the form of:
line = ''(2,3,4)''
line = ''''
line = ''(2)''
(in fact these are not constant numbers, but all kind of integer
variables, coming from all over the program, selected from a tree, that
shows all "reachable" variables)

So in fact I get the value from an exec statement, like this
exec ''signals = '' + line



然后,有一天,不喜欢你的人会在你的输入数据中添加以下



" 0;进口口; os.system(''rm#-rf /'')"


[孩子们:不要在家里试试!说真的,运行该命令将对你的计算机的健康状况造成不利影响。或者至少它会,如果我没有在它上面加上一个

的峰值。 ]


除非你知道自己在做什么,否则不要在生产代码中使用exec。在

中,不要在生产代码中使用exec。

And then, one day, somebody who doesn''t like you will add the following
to your input data:

"0; import os; os.system(''rm # -rf /'')"

[ Kids: don''t try this at home! Seriously, running that command will be
bad for your computer''s health. Or at least it would, if I hadn''t put a
spike in it. ]

Don''t use exec in production code unless you know what you''re doing. In
fact, don''t use exec in production code.


现在我想迭代信号 ;,如果有2个
或更多的信号,这是完美的,

但是当我没有或只有1个信号时它会失败。

for信号中的值:

做点什么
Now I want to iterate over "signals", which works perfect if there are 2
or more signals,
but it fails when I have none or just 1 signal.
for value in signals :
do something



不,我会说它在它到达之前就已经失败了。


No, I would say it already failed before it even got there.


>> line =''''
exec''signals =''+ line
>>line = ''''
exec ''signals = '' + line



Traceback(最近一次调用最后一次):

文件"< stdin>",line 1,在?

文件"< string>",第1行

signals =

^

语法错误:解析时意外的EOF


这是处理数据的正确方法:


i nput_data =""" (2,3,4)


(2)

(3,4,5)

(1,2, 3)

"""


for input_data.split(''\ n'')中的行:

line = line.strip()。strip(''()'')

values = line.split('','')

for value in值:

value = value.strip()

如果值:

print(value)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 1
signals =
^
SyntaxError: unexpected EOF while parsing

This is the right way to deal with your data:

input_data = """ (2, 3 , 4)

(2)
(3,4,5)
( 1, 2,3)
"""

for line in input_data.split(''\n''):
line = line.strip().strip(''()'')
values = line.split('','')
for value in values:
value = value.strip()
if value:
print(value)


因为这意味着实时信号,我想要它快,所以(我认为)我不能承受大量的测试。
As this meant for real-time signals, I want it fast, so (I think) I
can''t afford extensive testing.



不要猜测,测试它,看它是否足够快。一些加速:


如果你正在读取一个文件,你可以说:for line in file:

而不是将整个部分扼杀成一个巨大的字符串,然后

分割换行符。


如果你可以保证文件中没有额外的空格,你

可以换行


line = line.strip()。strip(''()'')

到以下内容:


line = line.strip(''\ n()'')


并保存一个smidgen每个循环的时间。同样,删除value =

value.strip()在内循环中。

-

史蒂文。

Don''t guess, test it and see if it is fast enough. Some speed ups:

If you''re reading from a file, you can just say: "for line in file:"
instead of slurping the whole lot into one enormous string, then
splitting over newlines.

If you can guarantee that there is no extra whitespace in the file, you
can change the line

line = line.strip().strip(''()'')

to the following:

line = line.strip(''\n()'')

and save a smidgen of time per loop. Likewise, drop the "value =
value.strip()" in the inner loop.
--
Steven.


10月19日上午10:58,史蒂文D''Aprano

< ste ... @ REMOVE.THIS.cybersource.com.auwrote:
On Oct 19, 10:58 am, Steven D''Aprano
<ste...@REMOVE.THIS.cybersource.com.auwrote:

星期五,10月19日2007 01:24:09 +0200,stef mientki写道:
On Fri, 19 Oct 2007 01:24:09 +0200, stef mientki wrote:

hello,
hello,


我动态地生成一系列值,但是这个序列是可以

也有长度1或甚至长度为0.
I generate dynamically a sequence of values, but this "sequence" could
also have length 1 or even length 0.


所以我得到一些形式的行:

line =''(2,3,4)''

line =''''

line =''(2)''

(实际上这些不是常数,而是所有类型的整数

变量,来自整个程序,从树中选择,

显示所有可到达变量)
So I get some line in the form of:
line = ''(2,3,4)''
line = ''''
line = ''(2)''
(in fact these are not constant numbers, but all kind of integer
variables, coming from all over the program, selected from a tree, that
shows all "reachable" variables)


所以实际上我从exec语句中获取值,就像这样

exec''signals =''+ line
So in fact I get the value from an exec statement, like this
exec ''signals = '' + line



然后,有一天,有人不喜欢你会添加以下内容

输入数据:


" 0;进口口; os.system(''rm#-rf /'')"


[孩子们:不要在家里试试!说真的,运行该命令将对你的计算机的健康状况造成不利影响。或者至少它会,如果我没有在它上面加上一个

的峰值。 ]


除非你知道自己在做什么,否则不要在生产代码中使用exec。在

中,不要在生产代码中使用exec。


And then, one day, somebody who doesn''t like you will add the following
to your input data:

"0; import os; os.system(''rm # -rf /'')"

[ Kids: don''t try this at home! Seriously, running that command will be
bad for your computer''s health. Or at least it would, if I hadn''t put a
spike in it. ]

Don''t use exec in production code unless you know what you''re doing. In
fact, don''t use exec in production code.


现在我想迭代信号,这是有效的完美的,如果有2个b $ b或更多的信号,

但是当我没有信号或只有1个信号时它就会失败。

信号值:

做点什么
Now I want to iterate over "signals", which works perfect if there are 2
or more signals,
but it fails when I have none or just 1 signal.
for value in signals :
do something



不,我会说它在它到达之前已经失败了。


No, I would say it already failed before it even got there.


> line =''''
exec''signals =''+ line
>line = ''''
exec ''signals = '' + line



回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

文件"< string> ;",第1行

信号=

^

语法错误:解析时意外的EOF


这是处理您数据的正确方法:


input_data =""" (2,3,4)


(2)

(3,4,5)

(1,2, 3)

"""


for input_data.split(''\ n'')中的行:

line = line.strip()。strip(''()'')

values = line.split('','')

for value in值:

value = value.strip()

如果值:

print(value)


Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 1
signals =
^
SyntaxError: unexpected EOF while parsing

This is the right way to deal with your data:

input_data = """ (2, 3 , 4)

(2)
(3,4,5)
( 1, 2,3)
"""

for line in input_data.split(''\n''):
line = line.strip().strip(''()'')
values = line.split('','')
for value in values:
value = value.strip()
if value:
print(value)


因为这意味着实时信号,我想要它快,所以(我认为)我没有b $ b不能进行大量测试。
As this meant for real-time signals, I want it fast, so (I think) I
can''t afford extensive testing.



不要猜测,测试它,看看它是否足够快。一些加速:


如果你正在读取一个文件,你可以说:for line in file:

而不是将整个部分扼杀成一个巨大的字符串,然后

分割换行符。


如果你可以保证文件中没有额外的空格,你

可以换行


line = line.strip()。strip(''()'')

到以下内容:


line = line.strip(''\ n()'')


并保存一个smidgen每个循环的时间。同样,删除value =

value.strip()在内循环中。


-

史蒂文。


Don''t guess, test it and see if it is fast enough. Some speed ups:

If you''re reading from a file, you can just say: "for line in file:"
instead of slurping the whole lot into one enormous string, then
splitting over newlines.

If you can guarantee that there is no extra whitespace in the file, you
can change the line

line = line.strip().strip(''()'')

to the following:

line = line.strip(''\n()'')

and save a smidgen of time per loop. Likewise, drop the "value =
value.strip()" in the inner loop.

--
Steven.



为什么不:

why not:


>> for e in eval(''(1,2,3)''):
>>for i in eval(''(1,2,3)''):



... 。打印i

1

2

3

.... print i
1
2
3


这篇关于如何迭代序列和非序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆