搜索dict的值(每个值都是一个列表) [英] searching a value of a dict (each value is a list)

查看:82
本文介绍了搜索dict的值(每个值都是一个列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一把百万字的字典。

字典中的每个值都有一个包含最多一千个整数的列表。

跟随是一个带有5个键的简单示例。


dict = {1:[1,2,3,4,5],

2:[10,11,12],

900000:[100,101 ,102,103,104,105],

900001:[20,21,22],

999999:[15,16,17,18,19]}


我想在其值列表中找出具有特定

整数的键值。例如,如果我在列表中搜索
104,则必须返回900000.


如何使用Python执行此操作?想法?

解决方案

12?ù10à?,?ààl?? ?? 23oD,Seongsu Lee< se ... @ senux.comwrote:





我有一个包含百万个密钥的字典。

字典中的每个值都有一个包含最多一千个整数的列表。

跟随是一个带有5个键的简单示例。


dict = {1:[1,2,3,4,5],

2:[10,11,12],

900000:[100,101 ,102,103,104,105],

900001:[20,21,22],

999999:[15,16,17,18,19]}


我想在其值列表中找出具有特定

整数的键值。例如,如果我在列表中搜索
104,则必须返回900000.


如何使用Python执行此操作?想法?






我只是让dict以双向方式工作,以便

我可以通过键和值找出我想要的东西。需要一个标记

或前缀来区分来自密钥的密钥和来自值的密钥和来自密钥的密钥。 (值*( - 1))

来自pprint import pprint的


dict = {1:[1,2,3,4,5],

2:[10,11,12],

900000:[100,101,102,103,104,105],

900001 :[20,21,22],

999999:[15,16,17,18,19]}

代表k,v代表dict.items():

for x in v:

dict [x * -1] = k

pprint(dict)


{-105:900000,

-104:900000,

-103:900000,

-102:900000,

-101:900000,

-100:900000,

-22:900001,

-21:900001 ,

-20:900001,

-19:999999,

-18:999999,

-17 :999999,

-16:999999,

-15:999999,

-12:2,

-11:2,

-10:2,

-5:1,

-4:1,

-3:1,

-2:1,

-1:1,

1:[1,2,3, 4,5],

2:[10,11,12],

900000:[100,101,102,103,104,105],
900001:[20,21,22 ],

999999:[15,16,17,18,19]}


你怎么看待这个?空间复杂度较低的想法?


SeongsuLeeescribió:




我有一把百万字的字典。

字典中的每个值都有一个最多包含一千个整数的列表。

(...)


我想要找出在其值列表中具有特定

整数的键值。



对不起,如果这没用,但是你考虑过移动你的数据

建立一个合适的数据库吗?

我问,因为除非有人知道某个特定的模块,否则我认为我们在DB的真实领域是
。是最快的解决方案,可能不仅仅是你想要做的这个特殊操作。


问候,

Pablo


12ì??10ì??,ì?¤ì*?1ì?? 53 ???,Pablo Ziliani< pa ... @ decode.com.arwrote :


Seongsu Lee escribi?3:



< blockquote class =post_quotes>
我有一个包含百万个密钥的字典。

字典中的每个值都有一个最多包含一千个整数的列表。

(...)


我想在其值列表中找出具有特定

整数的键值。



对不起,如果这无用,但您是否考虑过移动数据

建立一个合适的数据库?

我问,因为除非有人知道某个特定的模块,否则我认为我们在DB的真实领域是
。是最快的解决方案,可能不仅仅是为了你想要做的特定操作。


问候,

Pablo



嗨Pablo,


感谢您的发布!我想在一个给定的环境,python中解决问题,并且我认为它是通过

解决的一个带有双向密钥的字典。我发布了它并且想要知道其他人是否知道更优雅的方式来实现它。


Hi,

I have a dictionary with million keys. Each value in the
dictionary has a list with up to thousand integers.
Follow is a simple example with 5 keys.

dict = {1: [1, 2, 3, 4, 5],
2: [10, 11, 12],
900000: [100, 101, 102, 103, 104, 105],
900001: [20, 21, 22],
999999: [15, 16, 17, 18, 19]}

I want to find out the key value which has a specific
integer in the list of its value. For example, if I search
104 in the list, 900000 must be returned.

How can I do this with Python? Ideas?

解决方案

On 12?ù10à?, ?ààü1??23oD, Seongsu Lee <se...@senux.comwrote:

Hi,

I have a dictionary with million keys. Each value in the
dictionary has a list with up to thousand integers.
Follow is a simple example with 5 keys.

dict = {1: [1, 2, 3, 4, 5],
2: [10, 11, 12],
900000: [100, 101, 102, 103, 104, 105],
900001: [20, 21, 22],
999999: [15, 16, 17, 18, 19]}

I want to find out the key value which has a specific
integer in the list of its value. For example, if I search
104 in the list, 900000 must be returned.

How can I do this with Python? Ideas?

Hi,

I just let the dict work in bidirectional fashion so that
I can find out what I want by both key and value. A mark
or prefix was needed to distinguish between keys originated
from keys and keys originated from values. (value * (-1))

from pprint import pprint
dict = {1: [1, 2, 3, 4, 5],
2: [10, 11, 12],
900000: [100, 101, 102, 103, 104, 105],
900001: [20, 21, 22],
999999: [15, 16, 17, 18, 19]}
for k, v in dict.items():
for x in v:
dict[x * -1] = k
pprint(dict)

{-105: 900000,
-104: 900000,
-103: 900000,
-102: 900000,
-101: 900000,
-100: 900000,
-22: 900001,
-21: 900001,
-20: 900001,
-19: 999999,
-18: 999999,
-17: 999999,
-16: 999999,
-15: 999999,
-12: 2,
-11: 2,
-10: 2,
-5: 1,
-4: 1,
-3: 1,
-2: 1,
-1: 1,
1: [1, 2, 3, 4, 5],
2: [10, 11, 12],
900000: [100, 101, 102, 103, 104, 105],
900001: [20, 21, 22],
999999: [15, 16, 17, 18, 19]}

What do you think of this? Ideas with less space complexity?


Seongsu Lee escribió:

Hi,

I have a dictionary with million keys. Each value in the
dictionary has a list with up to thousand integers.
(...)

I want to find out the key value which has a specific
integer in the list of its value.

Sorry if this is unhelpful, but have you considered moving your data
model a proper database?
I ask because unless someone knows of a specific module, I think we are
in DB''s authentic realm. Is the fastest solution, probably not just for
this particular operation you are trying to do.

Regards,
Pablo


On 12ì??10ì??, ì?¤ì*?1ì??53???, Pablo Ziliani <pa...@decode.com.arwrote:

Seongsu Lee escribi?3:

Hi,

I have a dictionary with million keys. Each value in the
dictionary has a list with up to thousand integers.
(...)

I want to find out the key value which has a specific
integer in the list of its value.


Sorry if this is unhelpful, but have you considered moving your data
model a proper database?
I ask because unless someone knows of a specific module, I think we are
in DB''s authentic realm. Is the fastest solution, probably not just for
this particular operation you are trying to do.

Regards,
Pablo

Hi Pablo,

Thank you for your posting! I wanted to solve the problem within
a given environment, python, and I think it is solved by
a dictionary with bidirectional key. I have posted it and want to
know if other knows more elegant way to do it.


这篇关于搜索dict的值(每个值都是一个列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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