如何使用列表作为字典的关键? [英] How to use list as key of dictionary?

查看:76
本文介绍了如何使用列表作为字典的关键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我们知道列表不能用作字典的键。那么,如何工作

呢?


例如,有随机列表,如l = [1,323,54,67]。


欢迎提出任何建议!


祝你好运,

Davy

Hi all,

We know that list cannot be used as key of dictionary. So, how to work
around it?

For example, there is random list like l=[1,323,54,67].

Any suggestions are welcome!

Best regards,
Davy

推荐答案

Davy写道:
Davy wrote:

大家好,


我们知道那份清单不能用作字典的关键字。
Hi all,

We know that list cannot be used as key of dictionary.



是的,但我们知道为什么吗?

Yeah, but do we know why ?


那么,如何工作

围绕它?
So, how to work
around it?



这是一个附属问题。

That''s a subsidiary question.


>

For例如,有随机列表,如l = [1,323,54,67]。
>
For example, there is random list like l=[1,323,54,67].



不要使用1owercase L作为变量名称,p1ease!

Don''t use 1owercase L as a variab1e name, p1ease !


>

欢迎任何建议!
>
Any suggestions are welcome!


>> {tuple([1,323,54,67] ):666}
>>{tuple([1,323,54,67]):666}



{(1,923,54,67):666}

{(1, 323, 54, 67): 666}


11月5日晚上10点53分,Davy< zhushe ... @ gmail.comwrote:
On Nov 5, 10:53 pm, Davy <zhushe...@gmail.comwrote:

大家好,


我们知道列表不能用作字典的键。那么,如何工作

呢?


例如,有随机列表,如l = [1,323,54,67]。


欢迎提出任何建议!


祝你好运,

Davy
Hi all,

We know that list cannot be used as key of dictionary. So, how to work
around it?

For example, there is random list like l=[1,323,54,67].

Any suggestions are welcome!

Best regards,
Davy



改为使用元组。

Use a tuple instead.


>> d = {}
d [元组([1,2,3,4])] =''你好世界''
d
>>d = {}
d[tuple([1,2,3,4])] = ''hello world''
d



{(1,2,3,4):''hello world''}

{(1, 2, 3, 4): ''hello world''}


>> d [1,2,3,4]
>>d[1,2,3,4]



''hello world''


马特

''hello world''

Matt


嗨Matimus和鲍里斯,


谢谢:)


关于第1级以上的向量的另一个问题,我怎样才能将它作为字典的关键?

? />

例如,如果我有L = [[1,2,3],[4,5,6,7]]的列表,

那么我do L_tuple = tuple(L)
Hi Matimus and Boris,

Thank you :)

And a further question about vector above rank 1, how can I use it as
the key of dictionary?

For example, if I have list like L=[[1,2,3],[4,5,6,7]],
Then I do L_tuple = tuple(L)

>> L_tuple =([1,2 ,3],[4,5,6,7])
>>L_tuple = ([1,2,3],[4,5,6,7])



但{L_tuple:''hello''}导致错误?


祝你好运,

Davy


11月6日下午3:09,Matimus< mccre ... @ gmail.comwrote:

But {L_tuple:''hello''} cause an error?

Best regards,
Davy

On Nov 6, 3:09 pm, Matimus <mccre...@gmail.comwrote:


11月5日晚上10点53分,Davy< zhushe ... @ gmail.comwrote:
On Nov 5, 10:53 pm, Davy <zhushe...@gmail.comwrote:

大家好,
Hi all,


我们知道列表不能用作字典的键。那么,如何工作

呢?
We know that list cannot be used as key of dictionary. So, how to work
around it?


例如,有一个随机列表,如l = [1,323,54,67]。
For example, there is random list like l=[1,323,54,67].


欢迎任何建议!
Any suggestions are welcome!


祝你好运,

Davy
Best regards,
Davy



使用而不是元组。


Use a tuple instead.


> d = {}
d [元组([1,2,3,4] )]'''你好世界''
d
>d = {}
d[tuple([1,2,3,4])] = ''hello world''
d



{(1,2,3,4):''hello world' '}>> d [1,2,3,4]


''你好世界'


马特


{(1, 2, 3, 4): ''hello world''}>>d[1,2,3,4]

''hello world''

Matt



这篇关于如何使用列表作为字典的关键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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