转向功能编程 [英] Moving to functional programming

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

问题描述

大家好,


有一个简单的问题变成了一个有趣的解决方案我想b $ b我想在这里分享它。


我有一个元组列表,我需要从中获取第一个值,并且

生成一个列表。


tuple_list =(

(''John'',''Doe''),

(''马克'',''梅森''),

(''杰夫'',''史蒂文斯''),

(''蝙蝠'',''男'')




#我用C语言或其他程序语言做什么

result_list = []

tuple_list中的项目:

result_list.append(item [0])


#第一次使用理解的Pythonic尝试

result_list = [x [0] for x in tuple_list]


#最终的功能方式

[result_list,_] = zip(* tuple_list)


我真的很喜欢Python允许我做我觉得最自然的解决方案(对于a经验丰富的程序程序员)同时允许

a满足更实用的方法。


干杯,

詹姆斯

解决方案

James Fassett:


#第一次使用理解的Pythonic尝试

result_list = [x [0] for x in tuple_list]


#最终的功能方式

[result_list,_] = zip(* tuple_list)


我真的很喜欢Python允许我做我觉得最自然的解决方案(对于经验丰富的程序员程序员)同时允许

a满足了更实用的方法。



列表理解对我来说更具可读性,所以我建议你使用它来获得
。它可能是在Python中执行它的默认方式。


如果你想要功能代码这是另一种方式(我没有测试过

相对性能但可能很快):


>> tuple_list =(



....(''John'',''Doe''),

... ('''马克'',''梅森''),

......(''杰夫''''''史蒂文斯''),

。 ...(''蝙蝠'',''男'')

....)

< blockquote class =post_quotes>
>> from operator import itemgetter
map(itemgetter(0),tuple_list)



[''John'',''Mark'',''Jeff'',''Bat'']


再见,

熊形虫


7月11日,3:36 * am,bearophileH ... @ lycos.com写道:


James Fassett:


#第一次使用理解的Pythonic尝试

result_list = [x [0] for x in tuple_list]


#最终的功能方式

[result_list,_] = zip(* tuple_list)

< blockquote class =post_quotes>
我真的很喜欢Python允许我做什么,我认为是最好的b $ b b自然解决方案(适合经验丰富的程序程序员),同时允许

a满足了更实用的方法。



列表理解对我来说更具可读性,所以我建议你

来使用它。它可能是在Python中执行它的默认方式。


如果你想要功能代码这是另一种方式(我没有测试过

相对性能但可能很快):


> tuple_list =(

< br $>
... * *(''John'',''Doe''),

... * *(''''''''''''''''''''''' '),

... * *(''杰夫'''''史蒂文斯''),

...... * *(''''',''''', ''男'')

... *)>>来自运营商导入项目资格


> map(itemgetter(0),tuple_list)



[''John'',''Mark'',''Jeff'' ,''蝙蝠'']


再见,

bearophile



功能程序员喜欢模式匹配(我认为这是匹配的/>
代码更容易理解):


[x代表tuple_list中的(x,y)]





map(lambda(x,y):x,tuple_list)


7月11日,12:00 * pm,James Fassett< ja ... @reggieband.comwrote:


tuple_list =(

* *(''John' ',''Doe''),

* *(''Mark'',''Mason''),

* *('''Jeff'', ''史蒂文斯''),

* *(''蝙蝠'',''男'')

*)


#我用C或其他程序语言做什么

result_list = []

tuple_list中的项目:

* * result_list.append(item [0])



以下是一些不同的功能解决方案。选择最合适的一个

你的问题最好:


result_list = [fn for fn,ln in tuple_list]


result_generator =(fn for fn,ln in tuple_list)


result_list = map(lambda(fn,ln):fn,result_list)


result_generator = itertools.imap(lambda(fn,ln):fn,result_list)


Hi all,

Had a simple problem that turned into an interesting solution and I
thought I would share it here.

I had a list of tuples that I needed to get the first value from and
generate a list.

tuple_list = (
(''John'', ''Doe''),
(''Mark'', ''Mason''),
(''Jeff'', ''Stevens''),
(''Bat'', ''Man'')
)

# what I''d do in C or other procedural languages
result_list = []
for item in tuple_list:
result_list.append(item[0])

# the first Pythonic attempt using comprehensions
result_list = [x[0] for x in tuple_list]

# the final functional way
[result_list, _] = zip(*tuple_list)

I really like how Python allows me to do what I feel is the most
natural solution (for a seasoned procedural programmer) while allowing
a satisfying path towards a more functional approach.

Cheers,
James

解决方案

James Fassett:

# the first Pythonic attempt using comprehensions
result_list = [x[0] for x in tuple_list]

# the final functional way
[result_list, _] = zip(*tuple_list)

I really like how Python allows me to do what I feel is the most
natural solution (for a seasoned procedural programmer) while allowing
a satisfying path towards a more functional approach.

The list comprehension is quite more readable to me, so I suggest you
to use it. It''s probably the default way to do it in Python.

If you want functional code this is another way (I have not tested the
relative performance but it may be quick):

>>tuple_list = (

.... (''John'', ''Doe''),
.... (''Mark'', ''Mason''),
.... (''Jeff'', ''Stevens''),
.... (''Bat'', ''Man'')
.... )

>>from operator import itemgetter
map(itemgetter(0), tuple_list)

[''John'', ''Mark'', ''Jeff'', ''Bat'']

Bye,
bearophile


On Jul 11, 3:36*am, bearophileH...@lycos.com wrote:

James Fassett:

# the first Pythonic attempt using comprehensions
result_list = [x[0] for x in tuple_list]

# the final functional way
[result_list, _] = zip(*tuple_list)

I really like how Python allows me to do what I feel is the most
natural solution (for a seasoned procedural programmer) while allowing
a satisfying path towards a more functional approach.


The list comprehension is quite more readable to me, so I suggest you
to use it. It''s probably the default way to do it in Python.

If you want functional code this is another way (I have not tested the
relative performance but it may be quick):

>tuple_list = (


... * * (''John'', ''Doe''),
... * * (''Mark'', ''Mason''),
... * * (''Jeff'', ''Stevens''),
... * * (''Bat'', ''Man'')
... * )>>from operator import itemgetter

>map(itemgetter(0), tuple_list)


[''John'', ''Mark'', ''Jeff'', ''Bat'']

Bye,
bearophile


Functional programmers love pattern matching (which I think makes the
code much easier to understand):

[x for (x,y) in tuple_list]

or

map(lambda (x,y):x, tuple_list)


On Jul 11, 12:00*pm, James Fassett <ja...@reggieband.comwrote:

tuple_list = (
* * (''John'', ''Doe''),
* * (''Mark'', ''Mason''),
* * (''Jeff'', ''Stevens''),
* * (''Bat'', ''Man'')
* )

# what I''d do in C or other procedural languages
result_list = []
for item in tuple_list:
* * result_list.append(item[0])

Here are some various ''functional'' solutions. Pick the one that fits
your problem best:

result_list = [fn for fn,ln in tuple_list]

result_generator = (fn for fn,ln in tuple_list)

result_list = map(lambda (fn,ln): fn, result_list)

result_generator = itertools.imap(lambda (fn,ln): fn, result_list)


这篇关于转向功能编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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