Python用键值搜索Json

时间:2021-09-03 19:16:47

I'm having a JSON file and I'm trying to do a search using the values ( not the keys). Is there a built in function in Python that does so?

我有一个JSON文件,我正在尝试使用值(而不是键)进行搜索。 Python中是否有内置函数可以执行此操作?

 [["2778074170846781111111110", "a33104eb1987bec2519fe051d1e7bd0b4c9e4875"],
  ["2778074170846781111111111", "f307fb3db3380bfd27901bc591eb025398b0db66"]]

I thought of this approach. Loading the file into a list and start searching. Is there a more efficient way?

我想到了这种方法。将文件加载到列表中并开始搜索。有更有效的方法吗?

def OptionLookUp(keyvalue):
    with open('data.json', 'r') as table:
        x= json.loads(table)

3 个解决方案

#1


0  

After your edit I can say that there is no faster/more efficient way than turning your JSON into python 2-dimensional list and loop through each node and compare second field with your "keyvalue".

在编辑之后,我可以说没有比将JSON转换为python二维列表并循环遍历每个节点并将第二个字段与“keyvalue”进行比较的更快/更有效的方法。

EDIT: faster/more efficient

编辑:更快/更高效

#2


1  

your_dict = {'a': 1, 'b': 2, 'c': 'asd'}  # the dictionary
your_value = 'asd'  # the value to look for
[elem for elem in your_dict.iteritems() if elem[1] == 'asd']  # look for entry with value == your_value
Output: [('c', 'asd')]

EDIT:

for a list:

列表:

your_list = [['a', 1], ['b', 2], ['c', 'asd']]  # the list
your_value = 'asd'  # the value to look for
[elem for elem in your_list if elem[1] == 'asd']  # look for element with value == your_value
Output: [('c', 'asd')]

#3


0  

I assume you're looking for the key (or keys) associated with a given value.

我假设您正在寻找与给定值相关联的键(或键)。

If your data is garanteed to be a list of (key, value) pairs, depending on 1/ the data's volume and 2/ how many lookups you'll have to perform on a same dataset, you can either do a plain sequential search:

如果您的数据被保证为(键,值)对的列表,则取决于1 /数据的卷和2 /您必须在同一数据集上执行的查找次数,您可以执行简单的顺序搜索:

def slookup(lst, value):
    return [k for k, v in lst if v == value]

or build a reverse index then lookup the index:

或者构建一个反向索引然后查找索引:

import defaultdict
def index(lst):
    d = defaultdict(list)
    for k, v in lst:
        d[v].append(k)
    return d

rindex = index(lst)
print rindex.get(someval)
print rindex.get(someotherval)

This second solution only makes sense if you have a lot of lookups to do on the same dataset, obviously...

第二种解决方案只有在同一数据集上有很多查找时才有意义,显然......

#1


0  

After your edit I can say that there is no faster/more efficient way than turning your JSON into python 2-dimensional list and loop through each node and compare second field with your "keyvalue".

在编辑之后,我可以说没有比将JSON转换为python二维列表并循环遍历每个节点并将第二个字段与“keyvalue”进行比较的更快/更有效的方法。

EDIT: faster/more efficient

编辑:更快/更高效

#2


1  

your_dict = {'a': 1, 'b': 2, 'c': 'asd'}  # the dictionary
your_value = 'asd'  # the value to look for
[elem for elem in your_dict.iteritems() if elem[1] == 'asd']  # look for entry with value == your_value
Output: [('c', 'asd')]

EDIT:

for a list:

列表:

your_list = [['a', 1], ['b', 2], ['c', 'asd']]  # the list
your_value = 'asd'  # the value to look for
[elem for elem in your_list if elem[1] == 'asd']  # look for element with value == your_value
Output: [('c', 'asd')]

#3


0  

I assume you're looking for the key (or keys) associated with a given value.

我假设您正在寻找与给定值相关联的键(或键)。

If your data is garanteed to be a list of (key, value) pairs, depending on 1/ the data's volume and 2/ how many lookups you'll have to perform on a same dataset, you can either do a plain sequential search:

如果您的数据被保证为(键,值)对的列表,则取决于1 /数据的卷和2 /您必须在同一数据集上执行的查找次数,您可以执行简单的顺序搜索:

def slookup(lst, value):
    return [k for k, v in lst if v == value]

or build a reverse index then lookup the index:

或者构建一个反向索引然后查找索引:

import defaultdict
def index(lst):
    d = defaultdict(list)
    for k, v in lst:
        d[v].append(k)
    return d

rindex = index(lst)
print rindex.get(someval)
print rindex.get(someotherval)

This second solution only makes sense if you have a lot of lookups to do on the same dataset, obviously...

第二种解决方案只有在同一数据集上有很多查找时才有意义,显然......