在dict中附加一个新值 [英] Append a new value to dict

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

问题描述

我知道这不是公平的比较语言功能,但在我看来,在Python中为一个字典附加一个新键/值的
(一个Python新手)是非常麻烦的。

非常麻烦。 >

在Python中,这是我可以想出的最好的代码,用于添加一个新的

键,值为dict


mytable.setdefault(k,[])。append(v)


在Perl中,代码如下:


$ h {$ key} = $ value;


有没有比使用

obtuse / arcane setdefault代码更好/更简单的方法来编写代码?

I know it''s not "fair" to compare language features, but it seems to me
(a Python newbie) that appending a new key/value to a dict in Python is
awfully cumbersome.

In Python, this is the best code I could come up with for adding a new
key, value to a dict

mytable.setdefault( k, [] ).append( v )

In Perl, the code looks like this:

$h{ $key } = $value ;

Is there a better/easier way to code this in Python than the
obtuse/arcane setdefault code?

推荐答案

h {


key} =


value;


有没有比使用

obtuse / arcane setdefault代码更好/更简单的方法来编写代码?
value ;

Is there a better/easier way to code this in Python than the
obtuse/arcane setdefault code?

这篇关于在dict中附加一个新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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