列表理解 [英] list comprehension

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

问题描述

您好,


尝试更改字符串(x,y值),例如:


s =" 114320,69808 114272 ,69920 113568,71600 113328,72272


到(x,-y):


out =" 114320,-69808 114272 ,-69920 113568,-71600 113328,-72272"


我试过这个:


print [(a [0], - a [1] for x.split('',''))for x in e]


但它不起作用。任何人都可以建议为什么或建议另一种方式

方式?文字字符串比这大得多,所以表演

很重要。


TIA,


Guy

解决方案

文章< c7 ********** @ lust.ihug.co.nz>,

Guy Robinson< gu*@NOSPAM.red.co.nz>写道:


尝试更改字符串(x,y值),例如:

s =" 114320,69808 114272,69920 113568,71600 113328 ,72272

到(x,-y):

out =" 114320,-69808 114272,-69920 113568,-71600 113328,-72272"

我试过这个:

打印[(a [0], - a [1] for x.split('',''))for x in e]

但它不起作用。任何人都可以建议为什么或建议另类
方式?文本字符串比这大得多,所以性能很重要。




如有疑问,请将问题分成更小的步骤。让我玩Socrates

一分钟:使用数据集的第一步是什么?

-

Aahz(aa **@pythoncraft.com)< *> http://www.pythoncraft.com/


采用流程 - 停止杀死所有孩子!


这项工作我只是想知道是否可以写更多内容

简明扼要希望更快:


s =" 114320,69808 114272,69920 113568,71600 113328,72272"

e = s.split('''')

out =''''

for d in e:

d = d.split('','')

out + =''%s,%d''%(d [0], - int(d [1]))

打印出来


Guy


Aahz写道:

文章< c7 ********** @ lust。 ihug.co.nz>,
Guy Robinson< gu*@NOSPAM.red.co.nz>写道:

试图更改字符串(x,y值),例如:

s =" 114320,69808 114272,69920 113568,71600 113328 ,72272

到(x,-y):

out =" 114320,-69808 114272,-69920 113568,-71600 113328,-72272"

我试过这个:

打印[(a [0], - a [1] for x.split('',''))for x in e]

但它不起作用。任何人都可以建议为什么或建议另类
方式?文本字符串比这大得多,因此性能很重要。



如有疑问,请将问题分解为更小的步骤。让我玩Socrates
一分钟:使用数据集的第一步是什么?



5月10日星期一, 2004年12:32:20 PM +1200,Guy Robinson写道:

你好,

试图改变一个字符串(x,y值),如:

对于(x,-y):

out =" 114320 ,-69808 114272,-69920 113568,-71600 113328,-72272"

我试过这个:

打印[(a [0], - a [1] for a in x.split('','')for x in e]

但它不起作用。任何人都可以建议为什么或建议另类
方式?文本字符串比这大得多,所以性能很重要。




它有几个语法错误; (a [0], - a [1] for x.split('',''))不是

有效表达式,因为列表推导被方括号括起来

括号,而不是括号。此外,列表理解的第一部分,

计算每个元素的表达式,如果它具有

a逗号,则需要在parens中,以便解析器可以消除歧义它来自一个普通的清单。


我也不知道你从哪里来的'e''。它是''',还是's.split()''?

如果列表comprenhensions变得笨拙,只需使用for循环。他们可能比一个列表理解更容易阅读,这需要你花十分钟时间来完成,并且性能几乎完全相同。然而,为了回答你的问题,这里的表达符合你的要求:

< blockquote class =post_quotes> s =" 114320,69808 114272,69920 113568,71600 113328,72272"
s.replace('','','', - '')
' '114320,-69808 114272,-69920 113568,-71600 113328,-72272''


你可以用列表推导来做到这一点,例如:

' '''.join([''%s, - %s''%tuple(x)代表[pair.split('','')中x对于s.split('''')]中的对)



''114320,-69808 114272,-69920 113568,-71600 113328,-72272''


但是我没有看到这一点,因为你已经描述了

问题。


-Andrew。

Hello,

Trying to change a string(x,y values) such as :

s = "114320,69808 114272,69920 113568,71600 113328,72272"

into (x,-y):

out = "114320,-69808 114272,-69920 113568,-71600 113328,-72272"

I tried this:

print [(a[0],-a[1] for a in x.split('','')) for x in e]

But it doesn''t work. Can anyone suggest why or suggest an alternative
way? The text strings are significantly bigger than this so performance
is important.

TIA,

Guy

解决方案

In article <c7**********@lust.ihug.co.nz>,
Guy Robinson <gu*@NOSPAM.r-e-d.co.nz> wrote:


Trying to change a string(x,y values) such as :

s = "114320,69808 114272,69920 113568,71600 113328,72272"

into (x,-y):

out = "114320,-69808 114272,-69920 113568,-71600 113328,-72272"

I tried this:

print [(a[0],-a[1] for a in x.split('','')) for x in e]

But it doesn''t work. Can anyone suggest why or suggest an alternative
way? The text strings are significantly bigger than this so performance
is important.



When in doubt, break a problem into smaller steps. Let me play Socrates
for a minute: what''s the first step in working with your dataset?
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

Adopt A Process -- stop killing all your children!


This works I was just wondering if something could be written more
concisely and hopefully faster:

s = "114320,69808 114272,69920 113568,71600 113328,72272"
e = s.split('' '')
out =''''
for d in e:
d =d.split('','')
out +=''%s,%d '' %(d[0],-int(d[1]))
print out

Guy

Aahz wrote:

In article <c7**********@lust.ihug.co.nz>,
Guy Robinson <gu*@NOSPAM.r-e-d.co.nz> wrote:

Trying to change a string(x,y values) such as :

s = "114320,69808 114272,69920 113568,71600 113328,72272"

into (x,-y):

out = "114320,-69808 114272,-69920 113568,-71600 113328,-72272"

I tried this:

print [(a[0],-a[1] for a in x.split('','')) for x in e]

But it doesn''t work. Can anyone suggest why or suggest an alternative
way? The text strings are significantly bigger than this so performance
is important.


When in doubt, break a problem into smaller steps. Let me play Socrates
for a minute: what''s the first step in working with your dataset?



On Mon, May 10, 2004 at 12:32:20PM +1200, Guy Robinson wrote:

Hello,

Trying to change a string(x,y values) such as :

s = "114320,69808 114272,69920 113568,71600 113328,72272"

into (x,-y):

out = "114320,-69808 114272,-69920 113568,-71600 113328,-72272"

I tried this:

print [(a[0],-a[1] for a in x.split('','')) for x in e]

But it doesn''t work. Can anyone suggest why or suggest an alternative
way? The text strings are significantly bigger than this so performance
is important.



It has several syntax errors; (a[0],-a[1] for a in x.split('','')) is not a
valid expression because list comprehensions are bracketed by square
brackets, not parentheses. Also, the first part of a list comprehension,
the expression to calculate each element, needs to be in parens to if it has
a comma, so that the parser can disambiguate it from an ordinary list.

I also don''t know where you got ''e'' from. Is it ''s'', or ''s.split()''?

If list comprenhensions grow unwieldy, just use a for loop. They''re
probably easier to read than a list comprehension that takes you ten minutes
to concoct, and performance is almost identical. For the sake of answering
your question, though, here''s a expression that does what you ask:

s = "114320,69808 114272,69920 113568,71600 113328,72272"
s.replace('','','',-'') ''114320,-69808 114272,-69920 113568,-71600 113328,-72272''

You could do this with list comprehensions, e.g.:
'' ''.join([''%s,-%s'' % tuple(x) for x in [pairs.split('','') for pairs in s.split('' '')]])


''114320,-69808 114272,-69920 113568,-71600 113328,-72272''

But I don''t really see the point, given the way you''ve described the
problem.

-Andrew.


这篇关于列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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