关于python语言一致性的问题 [英] Question about consistency in python language

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

问题描述

让我们说我按如下方式定义对的列表:

Let''s say I define a list of pairs as follows:

l = [(''d'',3),(''a '',2),(''b'',1)]


有谁可以解释为什么这不起作用?h = {} .update(l)


而我必须去:h = {}
h.update(l)
用给定的对列表初始化字典?


当字符串上的类似操作正常工作时:s ="" .join([" d"," o"," g"])
l = [(''d'', 3), (''a'', 2), (''b'', 1)]
Can anyone explain why this does not work?h = {}.update(l)
and instead I have to go:h = {}
h.update(l) to initialize a dictionary with the given list of pairs?

when an analagous operation on strings works fine:s = "".join(["d","o","g"])




似乎不一致。


谢谢,

Scott



Seems inconsistent.

thanks,
Scott

推荐答案

更新更新字典 - 它实际上返回None,而不是更新的字典。
更新的字典。但是,您可以使用dict(list)从列表

(键,值)对构造字典。示例:
update updates the dictionary in place - it actually returns None, not
the updated dict. However, you can construct a dictionary from a list
of (key, value) pairs using dict(list). Example:
l = [(''foo'',''bar''),('' baz'',''qig'')]
d = dict(l)
print d
l = [(''foo'', ''bar''), (''baz'', ''qig'')]
d = dict(l)
print d



{'' foo'':''bar'',''baz'':''qig''}


{''foo'': ''bar'', ''baz'': ''qig''}


这是可变类型和不可变类型之间的区别。在这个意义上它

是一致的。


如果你想一次性完成前者:


h = dict(l)


另外,你不应该使用1,我的意思是l,作为变量名。它变得令人困惑

因为l,我的意思是1,看起来很像1,我的意思是l。


James


2005年9月8日星期四16:03, le *** *****@gmail.com 写道:
This is the difference between mutable and immutable types. In this sense it
is consistent.

If you want to do the former in one shot:

h = dict(l)

Also, you shouldn''t use "1", I mean "l", as a variable name. It gets confusing
because "l", I mean "1", looks a lot like "1", I mean "l".

James

On Thursday 08 September 2005 16:03, le********@gmail.com wrote:
让我们说我按如下方式定义对的列表:
Let''s say I define a list of pairs as follows:
l = [(''d'',3),('''',2),(''b'',1)]
任何人都可以解释为什么这不起作用?
h = {} .update(l)
而我必须去:h = {}
h.update(l)
用给定的列表初始化一个字典当对字符串的类似操作正常工作时,对?

:s ="" .join([" d"," o"," g"])
l = [(''d'', 3), (''a'', 2), (''b'', 1)]
Can anyone explain why this does not work?
h = {}.update(l)
and instead I have to go:h = {}
h.update(l)
to initialize a dictionary with the given list of pairs?

when an analagous operation on strings works fine:s = "".join(["d","o","g"])



似乎不一致。

谢谢,
Scott



Seems inconsistent.

thanks,
Scott




-

James Stroud

加州大学洛杉矶分校基因组学研究所a d蛋白质组学

方框951570

洛杉矶,加利福尼亚州90095

http://www.jamesstroud.com/


2005年9月8日16:03:12 -0700 , le********@gmail.com 写道:
On 8 Sep 2005 16:03:12 -0700, le********@gmail.com wrote:
让我们说我按如下方式定义对的列表:
Let''s say I define a list of pairs as follows:
l = [(''d'',3),('' a'',2),(''b'',1)]
任何人都可以解释为什么这不起作用?h = {} .update(l)
而我必须去: h = {}
h.update(l)用给定的对列表初始化字典?

当对字符串的类似操作正常工作时:s ="" .join ([d,o,g])
似乎不一致。
l = [(''d'', 3), (''a'', 2), (''b'', 1)]
Can anyone explain why this does not work?h = {}.update(l)
and instead I have to go:h = {}
h.update(l)to initialize a dictionary with the given list of pairs?

when an analagous operation on strings works fine:s = "".join(["d","o","g"])
Seems inconsistent.



加入并不是一个好的比喻,因为输入和输出是不可变的。

list.sort更多c dict.update是值得的。现在有一个内置排序函数

,它将获取一个列表并返回一个已排序的函数。也许会有一个类似于词典的更新

功能,但我不认为使用是常见的,

你可以制作自己的。几种方式。如果你想从一个空的字典开始

,dict构造函数将接受一对对(只要每对的第一个

是可以清洗的,这是也需要更新)。例如,


Join isn''t that good an analogy, because the inputs and outputs are immutable.
list.sort is more comparable to dict.update. There is now a builtin sorted function
that will take a list and return a sorted one. Perhaps there will come an "updated"
function analogously for dictionaries, but I don''t think the use is that common,
and you can make your own. Several ways. If you want to start with an empty dict
anyway, the dict constructor will accept a sequence of pairs (so long as the first
of every pair is hashable, which is also required for update). E.g.,

dict([(''d'',3),(''a'',2),(''b'',1)])
{''a'':2,''b'':1,'''':3}


或作为元组表达式dict的对序列( ((''d'',3),('''',2),(''b'',1)))
{''''':2,''b'' :1,'''':3}


或者有时将键和值作为单独的序列开始是很方便的

和zip他们在一起为dict构造函数

dict(zip(''dab'',[3,2,1]))
{'''':2,''b' ':1,'''':3}


或生成器表达式

dict((k,3-i)for i,k in枚举(''dab''))
{'''':2,''b'':1,''d'':3}

虽然注意到dict希望序列作为单个参数:dict((''d'',3),('''',2),(''b'',1))
Traceback(最近一次调用最后一次) ):

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

TypeError:dict最多需要1个参数,得到3

def更新(d,seq):d = d.copy(); d.update(SEQ);返回d
...更新({},[(''d'',3),('''',2),(''b'',1)])
dict([(''d'', 3), (''a'', 2), (''b'', 1)]) {''a'': 2, ''b'': 1, ''d'': 3}

or the sequence of pairs as a tuple expression dict(((''d'', 3), (''a'', 2), (''b'', 1))) {''a'': 2, ''b'': 1, ''d'': 3}

or sometimes it''s handy to start with the keys and values as separate sequences
and zip them together for the dict constructor
dict(zip(''dab'', [3,2,1])) {''a'': 2, ''b'': 1, ''d'': 3}

or a generator expression
dict((k,3-i) for i,k in enumerate(''dab'')) {''a'': 2, ''b'': 1, ''d'': 3}
though note that dict wants the sequence as a single argument: dict( (''d'', 3), (''a'', 2), (''b'', 1) ) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: dict expected at most 1 arguments, got 3
def updated(d, seq): d=d.copy(); d.update(seq); return d ... updated({}, [(''d'', 3), (''a'', 2), (''b'', 1)])



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


从变异方法中不返回对变异值

的引用的一个理由是,它很容易被认为是纯粹的表达式,并且b $ b是纯粹的表达式,并且忘记

变异对象的持续副作用。我认为这太容易出错了。


问候,

Bengt Richter


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

One rationale for not returning a reference to the mutated value
from a mutating method is that it is too easy to think of it
as a pure expression, and forget the persistent side effect of
mutating the object. I think that was thought too bug-prone.

Regards,
Bengt Richter


这篇关于关于python语言一致性的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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