字典初始化 [英] dictionary initialization

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

问题描述




有了awk,我可以做类似

$ echo''你好''| awk''{a [$ 1] + +} END {for(i in a)print i,a [i]}''


也就是说,[''hello'']不在那里,而是分配和初始化

零参考。


使用Python,我得到

b = {}
b [1] = b [1] +1



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

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

KeyError:1


也就是说,我必须首先明确初始化b [1]。


就个人而言,我认为


a [i] ++

awk中的



更优雅如果我在a:a [i] + = 1

else: a [i] = 1

我想知道后者在Python中是如何合理的。


谢谢,

伟光

解决方案

echo''hello''| awk''{a


1] ++} END {for(i in a)print i,a [i]}''


也就是说,[''hello'']不在那里,但在参考时分配并初始化为

零。


使用Python,我得到

b = {}
b [ 1] = b [1] +1



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

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

KeyError:1


也就是说,我必须首先明确地初始化b [1]。


我个人认为


a [i] ++

awk中的
更多优雅比


如果我在a:a [i] + = 1

其他:a [i] = 1


我想知道后者在Python中是如何合理的。


谢谢,

Weiguang


< BLOCKQUOTE>嗯:)


" b [1]"看起来像一个List(但是你创建了一个Dict)

" b [''1'']看起来更像是一个Dict(但这不是你用过的)。


如果列表是你的东西:

a = []
a。追加(1)
a
[1] a [0] + = 1
a
[2]


如果是你的东西:

b = {}
b [''1''] = 1
b
{''1'':1} b ['' 1''] + = 1
b


{''1'':2}


列出了订单,不是Dicts。

使用''string''键访问的词条,使用

位置整数访问的列表条目。


具体做哪个功能你想要理由吗?


thx

Caleb


使用Python,我得到了>>> b = {}
>>> b [1] = b [1] +1


Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
KeyError:1

也就是说,我必须首先明确地初始化b [1]。

就个人而言,我认为


如果我在a:a [i] + = 1
其他:a [i] = 1

我想知道后者在Python中是如何合理的。

谢谢,
Weiguang




Hi,

With awk, I can do something like
$ echo ''hello'' |awk ''{a[$1]++}END{for(i in a)print i, a[i]}''

That is, a[''hello''] was not there but allocated and initialized to
zero upon reference.

With Python, I got

b={}
b[1] = b[1] +1


Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: 1

That is, I have to initialize b[1] explicitly in the first place.

Personally, I think

a[i]++

in awk is much more elegant than

if i in a: a[i] += 1
else: a[i] = 1

I wonder how the latter is justified in Python.

Thanks,
Weiguang

解决方案

echo ''hello'' |awk ''{a


1]++}END{for(i in a)print i, a[i]}''

That is, a[''hello''] was not there but allocated and initialized to
zero upon reference.

With Python, I got

b={}
b[1] = b[1] +1


Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: 1

That is, I have to initialize b[1] explicitly in the first place.

Personally, I think

a[i]++

in awk is much more elegant than

if i in a: a[i] += 1
else: a[i] = 1

I wonder how the latter is justified in Python.

Thanks,
Weiguang


Hmm :)

"b[1]" looks like a List (but you created a Dict)
"b[''1''] looks more like a Dict (but this is not what you used).

If lists are your thing:

a = []
a.append(1)
a [1] a[0] += 1
a [2]

If dicts are your thing:
b = {}
b[''1''] = 1
b {''1'': 1} b[''1''] += 1
b

{''1'': 2}

Lists are ordered, Dicts are not.
Dict entries accessed with ''string'' keys, List entries accessed with a
position integer.

Which feature specifically do you want justification for?

thx
Caleb


With Python, I got >>> b={}
>>> b[1] = b[1] +1


Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: 1

That is, I have to initialize b[1] explicitly in the first place.

Personally, I think

a[i]++

in awk is much more elegant than

if i in a: a[i] += 1
else: a[i] = 1

I wonder how the latter is justified in Python.

Thanks,
Weiguang




这篇关于字典初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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