2

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?

 [["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)
5
  • it is normal 2-dimensional list (not dictionary) so you don't have keys - only values. And what do you want to find ? Use for-loop to check every row. Commented Nov 21, 2016 at 15:11
  • As furas says, it is 2-dimensional list (python) or when talking about JSON it's 2-dimensional array. This is key-value JSON: { 'key': 'value' }. As furas asks, what is the example of "keyvalue" you are passing the the function? Commented Nov 21, 2016 at 15:12
  • Ture, but it is still a valid JSON. You can check it here jsonlint.com Commented Nov 21, 2016 at 15:13
  • Please specify (with input / output examples) what you mean by "searching" ? Commented Nov 21, 2016 at 15:14
  • input = a33104eb1987bec2519fe051d1e7bd0b4c9e4875 Output = 2778074170846781111111110 I already did it like this def OptionLookUp(key): with open('data.json', 'r') as table: x= json.load(table) for i in x: if (i[1] == key): print i Commented Nov 21, 2016 at 15:18

3 Answers 3

1

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".

EDIT: faster/more efficient

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!. That's what I wanted to know :)
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')]

1 Comment

The op's data is a list of lists, not a dict.
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:

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.