3

I'm fairly new to Python and JSON, and I'm having a bit of trouble parsing through this data:

{
  "tests":
  [
    {"array": [-21, 301, 12, 4, 65, 56, 210, 356, 9, -47], "target": 163},
    {"array": [-21, 301, 12, 4, 65, 56, 210, 356, 9, -47], "target": 164},
    {"array": [1, 2, 3, 4, 5, 6, 7, 8, 9, 15], "target": 18},
    {"array": [-7, -5, -3, -1, 0, 1, 3, 5, 7], "target": -5},
    {"array": [3, 5, -4, 8, 11, 1, -1, 6], "target": 10},
    {"array": [1, 2, 3, 4, 5, 6, 7, 8, 9], "target": 17},
    {"array": [3, 5, -4, 8, 11, 1, -1, 6], "target": 15},
    {"array": [4, 6, 1, -3], "target": 3},
    {"array": [4, 6, 1], "target": 5},
    {"array": [4, 6], "target": 10},
    {"array": [14], "target": 15},
    {"array": [15], "target": 15}
  ]
}

This is what's inside of the file, and if I understand correctly, this is a dictionary ("tests":) with dictionaries inside of it that are comma-separated, where each dictionary has a kvp of array:list, target:int. Please correct me if I am wrong on this part.

Now, what I'm trying to do is loop through each of the dictionaries and print the list then integer of each. So far, this is what I have in Python:

for array, target_sum in test_data['tests']:
    print(array, target_sum)

but all I'm printing out is this:

array target array target array target array target array target array target array target array target array target array target array target array target

I guess what I'm trying to ask is how to print the values of this instead of the keys. Any help is appreciated, sorry for the noob question.

1
  • 'tests' is a keyword for dictionary it is in, and to this keyword is asigned a list of dictionaries that each contain two keywords and values Commented Oct 28, 2020 at 20:24

6 Answers 6

1

Indeed, you are printing the key name and not the values!

To print the values, do:

for test in test_data['tests']:
    print(test['array'], test['target'])

It gives:

[-21, 301, 12, 4, 65, 56, 210, 356, 9, -47] 163
[-21, 301, 12, 4, 65, 56, 210, 356, 9, -47] 164
[1, 2, 3, 4, 5, 6, 7, 8, 9, 15] 18
[-7, -5, -3, -1, 0, 1, 3, 5, 7] -5
[3, 5, -4, 8, 11, 1, -1, 6] 10
[1, 2, 3, 4, 5, 6, 7, 8, 9] 17
[3, 5, -4, 8, 11, 1, -1, 6] 15
[4, 6, 1, -3] 3
[4, 6, 1] 5
[4, 6] 10
[14] 15
[15] 15
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for! Thank you.
0

Perhaps something like this?

import json

data = '''\
{
  "tests":
  [
    {"array": [-21, 301, 12, 4, 65, 56, 210, 356, 9, -47], "target": 163},
    {"array": [-21, 301, 12, 4, 65, 56, 210, 356, 9, -47], "target": 164},
    {"array": [1, 2, 3, 4, 5, 6, 7, 8, 9, 15], "target": 18},
    {"array": [-7, -5, -3, -1, 0, 1, 3, 5, 7], "target": -5},
    {"array": [3, 5, -4, 8, 11, 1, -1, 6], "target": 10},
    {"array": [1, 2, 3, 4, 5, 6, 7, 8, 9], "target": 17},
    {"array": [3, 5, -4, 8, 11, 1, -1, 6], "target": 15},
    {"array": [4, 6, 1, -3], "target": 3},
    {"array": [4, 6, 1], "target": 5},
    {"array": [4, 6], "target": 10},
    {"array": [14], "target": 15},
    {"array": [15], "target": 15}
  ]
}
'''

d = json.loads(data)
for test in d['tests']:
    print(sum(test['array']), test['target'])

Comments

0

This is what you have to do:

for item in a['tests']:
    print(item['array'], '----', item['target'])

which gives you the following output:

[-21, 301, 12, 4, 65, 56, 210, 356, 9, -47] ---- 163
[-21, 301, 12, 4, 65, 56, 210, 356, 9, -47] ---- 164
[1, 2, 3, 4, 5, 6, 7, 8, 9, 15] ---- 18
[-7, -5, -3, -1, 0, 1, 3, 5, 7] ---- -5
[3, 5, -4, 8, 11, 1, -1, 6] ---- 10
[1, 2, 3, 4, 5, 6, 7, 8, 9] ---- 17
[3, 5, -4, 8, 11, 1, -1, 6] ---- 15
[4, 6, 1, -3] ---- 3
[4, 6, 1] ---- 5
[4, 6] ---- 10
[14] ---- 15
[15] ---- 15

Comments

0

the code could look like this:

tests = [
    {"array": [-21, 301, 12, 4, 65, 56, 210, 356, 9, -47], "target": 163},
    {"array": [-21, 301, 12, 4, 65, 56, 210, 356, 9, -47], "target": 164},
    {"array": [1, 2, 3, 4, 5, 6, 7, 8, 9, 15], "target": 18},
    {"array": [-7, -5, -3, -1, 0, 1, 3, 5, 7], "target": -5},
    {"array": [3, 5, -4, 8, 11, 1, -1, 6], "target": 10},
    {"array": [1, 2, 3, 4, 5, 6, 7, 8, 9], "target": 17},
    {"array": [3, 5, -4, 8, 11, 1, -1, 6], "target": 15},
    {"array": [4, 6, 1, -3], "target": 3},
    {"array": [4, 6, 1], "target": 5},
    {"array": [4, 6], "target": 10},
    {"array": [14], "target": 15},
    {"array": [15], "target": 15}
  ]


for dictionary in tests:
    for _, value in dictionary.items():
        print(value)

so in your case I made tests a list because it was not neccesary to store it in a dict(). and then I looped over each dictionary in that list and for each dictionary I got the key which is _ (underscore) which is a throwaway variable(we will not use it furthermore) and the value and printed it out. (dictionary.items() returns you all keywords and values of those)

Comments

0

The main big dictionary has a key "tests". This key's value is a list. The list contains dictionaries with the keys array and target. array has a value that is a list of integers. target is a single integer. Say you had this json in a string.

json_str = """{
  "tests":
  [
    {"array": [-21, 301, 12, 4, 65, 56, 210, 356, 9, -47], "target": 163},
    {"array": [-21, 301, 12, 4, 65, 56, 210, 356, 9, -47], "target": 164},
    {"array": [1, 2, 3, 4, 5, 6, 7, 8, 9, 15], "target": 18},
    {"array": [-7, -5, -3, -1, 0, 1, 3, 5, 7], "target": -5},
    {"array": [3, 5, -4, 8, 11, 1, -1, 6], "target": 10},
    {"array": [1, 2, 3, 4, 5, 6, 7, 8, 9], "target": 17},
    {"array": [3, 5, -4, 8, 11, 1, -1, 6], "target": 15},
    {"array": [4, 6, 1, -3], "target": 3},
    {"array": [4, 6, 1], "target": 5},
    {"array": [4, 6], "target": 10},
    {"array": [14], "target": 15},
    {"array": [15], "target": 15}
  ]
}"""

To parse this dict, do

j_dict = json.loads(json_str)

Now, j_dict['tests'] gives you the value in the key tests, which is a list. Try it with

print(j_dict['tests'])

You want to iterate over this list, and print the values of the array and target keys of each element of the list.

for elem_in_list in j_dict['tests']:
    # Now each elem_in_list is a dict like so:
    # {"array": [1, 2, 3, 4, 5, 6, 7, 8, 9, 15], "target": 18}
    print(elem_in_list['array'], elem_in_list['target'])

This gives the output

[-21, 301, 12, 4, 65, 56, 210, 356, 9, -47] 163
[-21, 301, 12, 4, 65, 56, 210, 356, 9, -47] 164
[1, 2, 3, 4, 5, 6, 7, 8, 9, 15] 18
[-7, -5, -3, -1, 0, 1, 3, 5, 7] -5
[3, 5, -4, 8, 11, 1, -1, 6] 10
[1, 2, 3, 4, 5, 6, 7, 8, 9] 17
[3, 5, -4, 8, 11, 1, -1, 6] 15
[4, 6, 1, -3] 3
[4, 6, 1] 5
[4, 6] 10
[14] 15
[15] 15

Now if you wanted to iterate over each element in the array key, you'd simply do that. Say you wanted to take the sum of the squares of those elements, and check if that was equal to target,

for elem_in_list in j_dict['tests']:
    # Now each elem_in_list is a dict like so:
    # {"array": [1, 2, 3, 4, 5, 6, 7, 8, 9, 15], "target": 18}
    elem_array = elem_in_list['array']
    elem_target = elem_in_list['target']
    total = 0
    for x in elem_array:
        total += x**2
    if elem_target == total:
        print("Equal!")
    else:
        print("Not equal!")

Comments

0

I don't know if this is the most pythonic way to do it, or even if it will work, but you can try this (Just for printing data as asked in the end):

for array, target_sum in test_data['tests']:
    print(test_data['tests'].get(array), test_data['tests'].get(target_sum))

Comments

Your Answer

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