reduce()异常? [英] reduce() anomaly?

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

问题描述

这似乎应该可行,根据减少()的

描述,但它没有。这是

a bug,还是我错过了什么?


Python 2.3.2(#1,2003年10月20日,01:04:35)

[GCC 3.2.2 20030222(Red Hat Linux 3.2.2-5)] on linux2

输入help,copyright,credit等等。或许可证或更多信息。

d1 = {'''':1}
d2 = {''b' ':2}
d3 = {''c'':3}
l = [d1,d2,d3]
d4 = reduce(lambda x,y:x.update(y) ),l)
回溯(最近一次调用最后一次):

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

文件" < stdin>",第1行,< lambda>

AttributeError:''NoneType''对象没有属性''update''d4 = reduce(lambda x,y:x。更新(y),l,{})



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

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

文件"< stdin>",第1行,< lambda>

AttributeError:''NoneType ''对象没有属性''更新''

- 史蒂夫。

解决方案

Stephen C.沃特伯里写道:

根据对reduce()的描述,这似乎应该有效,但它并没有。这是一个错误,还是我错过了什么?


后者。

Python 2.3.2(#1,2003年10月20日,01:04:35)
[GCC 3.2.2 20030222(Red Hat Linux 3.2.2-5)] on linux2
输入help,copyright,credit等。或许可证或更多信息。

>>> d1 = {''a'':1}
>>> d2 = {''b'':2}
>>> d3 = {''c'':3}
>>> l = [d1,d2,d3]
>>> d4 = reduce(lambda x,y:x.update(y),l)


更新方法返回None。

Traceback(最近的呼叫最后一次):
文件"< stdin>",第1行,在?
文件"< stdin>",第1行,< lambda>
AttributeError:''NoneType''对象没有属性''update''




对。

>>> d4 = reduce(lambda x,y:x.update(y),l,{})


Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
文件"< stdin>",第1行,< lambda>
AttributeError:''NoneType''对象没有属性''更新''




同样的问题。


如果你想为此目的不惜一切代价滥用减少量,

reduce(lambda x,y:x.update(y)或x,l)可能有效。

Alex


Stephen C. Waterbury写道:

根据对reduce()的描述,这似乎应该有效,但事实并非如此。这是一个错误,还是我错过了什么?

Python 2.3.2(#1,2003年10月20日,01:04:35)
[GCC 3.2.2 20030222(Red Hat Linux 3.2.2-5)] on linux2
输入help,copyright,credit等。或许可证或更多信息。

>>> d1 = {''a'':1}
>>> d2 = {''b'':2}
>>> d3 = {''c'':3}
>>> l = [d1,d2,d3]
>>> d4 = reduce(lambda x,y:x.update(y),l)Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
文件中"< stdin>",第1行,< lambda>
AttributeError:''NoneType''对象没有属性''update''>>> d4 = reduce(lambda x,y:x.update(y),l,{})


Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
文件"< stdin>",第1行,< lambda>
AttributeError:''NoneType''对象没有属性''更新''

- 史蒂夫。




更新方法更新字典,但返回无。

因此,在第一次调用x.update(y)之​​后,reduce试图调用

x.update(y),其中x等于None。因此错误。


替代方案包括


def rupdate(d,other):

d。更新(其他)

返回d


减少(rupdate,l)





d = {}

map(lambda x:d.update(x),l)


David


Stephen C. Waterbury写道:

这似乎应该有效,根据减少的描述(),但它不是。这是一个错误,还是我错过了什么?

Python 2.3.2(#1,2003年10月20日,01:04:35)
[GCC 3.2.2 20030222(Red Hat Linux 3.2.2-5)] on linux2
输入help,copyright,credit等。或许可证或更多信息。

>>> d1 = {''a'':1}
>>> d2 = {''b'':2}
>>> d3 = {''c'':3}
>>> l = [d1,d2,d3]
>>> d4 = reduce(lambda x,y:x.update(y),l)Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
文件中"< stdin>",第1行,< lambda>
AttributeError:''NoneType''对象没有属性''update''>>> d4 = reduce(lambda x,y:x.update(y),l,{})Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,in?
文件"< stdin>",第1行,< lambda>
AttributeError:''NoneType''对象没有属性''更新''

- 史蒂夫。




没有bug,你的lambda在第一次调用时评估d1.update(d2),然后

返回更新的结果( )方法是无。所以在secend

调用None.update(d3)失败。这就是你可能想要的:

d1 = {'''':1}
d2 = {''b'':2}
d3 = {''c'':3}
l = [d1,d2,d3]
d4 = reduce(lambda x,y:x.update(y)或x,l)
d4
{''a'':1,''c'':3,''b'':2}


注意副作用在d1

d1



{''a'':1,''c'':3,'' b'':2}


如果你不提供初始字典。


彼得


This seems like it ought to work, according to the
description of reduce(), but it doesn''t. Is this
a bug, or am I missing something?

Python 2.3.2 (#1, Oct 20 2003, 01:04:35)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

d1 = {''a'':1}
d2 = {''b'':2}
d3 = {''c'':3}
l = [d1, d2, d3]
d4 = reduce(lambda x, y: x.update(y), l) Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
AttributeError: ''NoneType'' object has no attribute ''update'' d4 = reduce(lambda x, y: x.update(y), l, {})


Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
AttributeError: ''NoneType'' object has no attribute ''update''

- Steve.

解决方案

Stephen C. Waterbury wrote:

This seems like it ought to work, according to the
description of reduce(), but it doesn''t. Is this
a bug, or am I missing something?
the latter.
Python 2.3.2 (#1, Oct 20 2003, 01:04:35)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> d1 = {''a'':1}
>>> d2 = {''b'':2}
>>> d3 = {''c'':3}
>>> l = [d1, d2, d3]
>>> d4 = reduce(lambda x, y: x.update(y), l)
the update method returns None.

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
AttributeError: ''NoneType'' object has no attribute ''update''



right.
>>> d4 = reduce(lambda x, y: x.update(y), l, {})


Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
AttributeError: ''NoneType'' object has no attribute ''update''



same issue.

If you want to abuse reduce at all costs for this purpose,
reduce(lambda x, y: x.update(y) or x, l) might work.
Alex


Stephen C. Waterbury wrote:

This seems like it ought to work, according to the
description of reduce(), but it doesn''t. Is this
a bug, or am I missing something?

Python 2.3.2 (#1, Oct 20 2003, 01:04:35)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> d1 = {''a'':1}
>>> d2 = {''b'':2}
>>> d3 = {''c'':3}
>>> l = [d1, d2, d3]
>>> d4 = reduce(lambda x, y: x.update(y), l) Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
AttributeError: ''NoneType'' object has no attribute ''update'' >>> d4 = reduce(lambda x, y: x.update(y), l, {})


Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
AttributeError: ''NoneType'' object has no attribute ''update''

- Steve.



The update method updates the dictionary in place, but returns None.
Thus, after the first call to x.update(y), reduce is trying to call
x.update(y) with x equal to None. Hence the error.

Alternatives which work include

def rupdate(d, other):
d.update(other)
return d

reduce(rupdate, l)

and

d = {}
map(lambda x: d.update(x), l)

David


Stephen C. Waterbury wrote:

This seems like it ought to work, according to the
description of reduce(), but it doesn''t. Is this
a bug, or am I missing something?

Python 2.3.2 (#1, Oct 20 2003, 01:04:35)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> d1 = {''a'':1}
>>> d2 = {''b'':2}
>>> d3 = {''c'':3}
>>> l = [d1, d2, d3]
>>> d4 = reduce(lambda x, y: x.update(y), l) Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
AttributeError: ''NoneType'' object has no attribute ''update'' >>> d4 = reduce(lambda x, y: x.update(y), l, {}) Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
AttributeError: ''NoneType'' object has no attribute ''update''

- Steve.



No bug, your lambda evaluates d1.update(d2) on the first call and then
returns the result of the update() method which is None. So on the secend
call None.update(d3) fails. Here''s what you might have intended:

d1 = {''a'':1}
d2 = {''b'':2}
d3 = {''c'':3}
l = [d1, d2, d3]
d4 = reduce(lambda x, y: x.update(y) or x, l)
d4 {''a'': 1, ''c'': 3, ''b'': 2}

Note the side effect on d1
d1


{''a'': 1, ''c'': 3, ''b'': 2}

if you don''t provide an initial dictionary.

Peter


这篇关于reduce()异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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