List indices must be integers, not dict - python

I'm making a script for the game and i need help about grabbing dictionary information out of json.
So far I've dealt with list and I didn't had problem grabbing that information.
req['result']['trades'][key]['output']
This is my try of grabbing the info after which i get error list indices must be integers, not dict.
Any help greatly appreciated.
json print, i have removed some account sensitive data.
{
'fue_step': 30,
'type': 'm',
'rubies': xxx,
'might': xxx,
'race': 'xxx',
'result': {
'trades': [
{
'id': 13,
'requirement': ['VenomTrapPart', 1],
'token_cost': 1,
'output': ['VulcansBlessingRare', 1],
'name': 'VenomVulcan',
'enabled': True
},
{
'id': 14,
'requirement': ['EternalRune', 50],
'token_cost': 1,
'output': ['ColossalMite500TroopPrizeItem', 1],
'name': 'EternalColossal',
'enabled': True
},
{
'id': 86,
'requirement':['RenameProclamation', 25],
'token_cost': 1,
'output': ['TimeTrickstersBag', 1],
'name': 'RenameTrick',
'enabled': True
},
{
'id': 95,
'requirement': ['FireDragonBlaze', 1],
'token_cost': 1,
'output': ['FireTroop1000TroopPrizeItem', 10],
'name': 'BlazeFireTroop',
'enabled': True
},
{
'id': 100,
'requirement': ['StoneDragonQuake', 1],
'token_cost': 1,
'output': ['StoneTroop10kTroopPrizeItem', 1],
'name': 'StoneDQStoneTroop',
'enabled': True
},
{
'id': 113,
'requirement': ['VulcansBlessing15Elite', 3],
'token_cost': 1,
'output': ['VulcansBlessing16Elite', 1],
'name': '15 --> 16 Elite',
'enabled': True
},
{
'id': 114,
'requirement': ['LunaPowder', 25],
'token_cost': 1,
'output': ['IceTroop50kTroopPrizeItem', 1],
'name': 'Luna Powder --> Soul Reapers',
'enabled': True
},
{
'id': 115,
'requirement': ['GreaterCrystalDefense', 5],
'token_cost': 1,
'output': ['MasterCrystalDefense', 1],
'name': 'Greater Defense --> Master Defense',
'enabled': True
},
{
'id': 116,
'requirement': ['NanoCanisters', 3],
'token_cost': 1,
'output': ['TestroniusDeluxe', 1],
'name': 'Nano Canisters for Deluxes',
'enabled': True
},
{
'id': 117,
'requirement': ['VulcansBlessing16Common', 3],
'token_cost': 1,
'output': ['VulcansBlessing17Common', 1],
'name': "Common +16's for Common +17",
'enabled': True
}
],
'trades_today': 0,
'doubloons_left': 9567,
'free_trades_left': 1,
'success': True,
'auto_confirm': True,
'trade_reset_date':1450771200
},
'client_cachebreaker': '1449879464'

I edited your edit to include indentation in your JSON so that it is more readable. As soon as the edit is approved, you'll be able to see it.
Here is an example of how to access a value deep within the object:
print req['result']['trades'][4]['name']
Output:
StoneDQStoneTroop
If you want to access dicts within a list by their values, here is a post that explains a few ways to do so. This would allow you, for example, to find a dict in req['result']['trades'] by its 'id' value instead of by its index within the 'trades' list.

Related

How to filter nested json array on Python

I have a json array on python which I want to filter based on the value of : my_json_array['models']['variants']['condition']['type']
my json array looks like the following :
my_json_array = [
{'id': 1,
'models': [
{'color': {'code': '#626262', 'name': 'Gray'},
'variants': [{'id': 1,
'condition': [{'id': 1,
'type': 'type1',
'value': 14900},
{'id': 2,
'type': 'type2',
'value': 14000}]]
]
}]
I'm looking for a method to remove condition items with type = type2. The output should look like this :
my_json_array = [{
'id': 1,
'models': [
{'color': {'code': '#626262', 'name': 'Gray'},
'variants': [{'id': 1,
'condition': [{'id': 1,
'type': 'type1',
'value': 14900}]]
]
}]
Do you mean this?
my_json_array = [
{
'id': 1,
'models': [
{
'color': {'code': '#626262', 'name': 'Gray'},
'variants': [
{
'id': 1,
'condition': [
{
'id': 1,
'type': 'type1',
'value': 14900
},
{
'id': 2,
'type': 'type2',
'value': 14000
}
]
}
]
}
]
}
]
for mydict in my_json_array:
for model in mydict['models']:
for variant in model['variants']:
for condition in variant['condition']:
if condition['type']=="type2":
variant['condition'].remove(condition)
print(my_json_array) # [{'id': 1, 'models': [{'color': {'code': '#626262', 'name': 'Gray'}, 'variants': [{'id': 1, 'condition': [{'id': 1, 'type': 'type1', 'value': 14900}]}]}]}]

Convert list of dictionaries into a nested dictionary [duplicate]

This question already has answers here:
Python: create a nested dictionary from a list of parent child values
(3 answers)
Closed 3 years ago.
I have a list of dictionaries that I got from the database in parent-child relationship:
data = [
{"id":1, "parent_id": 0, "name": "Wood", "price": 0},
{"id":2, "parent_id": 1, "name": "Mango", "price": 18},
{"id":3, "parent_id": 2, "name": "Table", "price": 342},
{"id":4, "parent_id": 2, "name": "Box", "price": 340},
{"id":5, "parent_id": 4, "name": "Pencil", "price": 240},
{"id":6, "parent_id": 0, "name": "Electronic", "price": 20},
{"id":7, "parent_id": 6, "name": "TV", "price": 350},
{"id":8, "parent_id": 6, "name": "Mobile", "price": 300},
{"id":9, "parent_id": 8, "name": "Iphone", "price": 0},
{"id":10, "parent_id": 9, "name": "Iphone 10", "price": 400}
]
I want to convert it to a nested dictionary such as
[ { "id": 1, "parent_id": 0, "name": "Wood", "price": 0, "children": [ { "id": 2, "parent_id": 1, "name": "Mango", "price": 18, "children": [ { "id": 3, "parent_id": 2, "name": "Table", "price": 342 }, { "id": 4, "parent_id": 2, "name": "Box", "price": 340, "children": [ { "id": 5, "parent_id": 4, "name": "Pencil", "price": 240 } ] } ] } ] }, { "id": 6, "parent_id": 0, "name": "Electronic", "price": 20, "children": [ { "id": 7, "parent_id": 6, "name": "TV", "price": 350 }, { "id": 8, "parent_id": 6, "name": "Mobile", "price": 300, "children": [ { "id": 9, "parent_id": 8, "name": "Iphone", "price": 0, "children": [ { "id": 10, "parent_id": 9, "name": "Iphone 10", "price": 400 } ] } ] } ] } ]
You can do this recursively, starting from the root nodes (where parent_id = 0) going downwards. But before your recursive calls, you can group nodes by their parent_id so that accessing them in each recursive call can be done in constant time:
levels = {}
for n in data:
levels.setdefault(n['parent_id'], []).append(n)
def build_tree(parent_id=0):
nodes = [dict(n) for n in levels.get(parent_id, [])]
for n in nodes:
children = build_tree(n['id'])
if children: n['children'] = children
return nodes
tree = build_tree()
print(tree)
Output
[{'id': 1, 'parent_id': 0, 'name': 'Wood', 'price': 0, 'children': [{'id': 2, 'parent_id': 1, 'name': 'Mango', 'price': 18, 'children': [{'id': 3, 'parent_id': 2, 'name': 'Table', 'price': 342}, {'id': 4, 'parent_id': 2, 'name': 'Box', 'price': 340, 'children': [{'id': 5, 'parent_id': 4, 'name': 'Pencil', 'price': 240}]}]}]}, {'id': 6, 'parent_id': 0, 'name': 'Electronic', 'price': 20, 'children': [{'id': 7, 'parent_id': 6, 'name': 'TV', 'price': 350}, {'id': 8, 'parent_id': 6, 'name': 'Mobile', 'price': 300, 'children': [{'id': 9, 'parent_id': 8, 'name': 'Iphone', 'price': 0,'children': [{'id': 10, 'parent_id': 9, 'name': 'Iphone 10', 'price': 400}]}]}]}]
Code is documented inline. Ignoring the corner cases like circular relations etc.
# Actual Data
data = [
{"id":1, "parent_id": 0, "name": "Wood", "price": 0},
{"id":2, "parent_id": 1, "name": "Mango", "price": 18},
{"id":3, "parent_id": 2, "name": "Table", "price": 342},
{"id":4, "parent_id": 2, "name": "Box", "price": 340},
{"id":5, "parent_id": 4, "name": "Pencil", "price": 240},
{"id":6, "parent_id": 0, "name": "Electronic", "price": 20},
{"id":7, "parent_id": 6, "name": "TV", "price": 350},
{"id":8, "parent_id": 6, "name": "Mobile", "price": 300},
{"id":9, "parent_id": 8, "name": "Iphone", "price": 0},
{"id":10, "parent_id": 9, "name": "Iphone 10", "price": 400}
]
# Create Parent -> child links using dictonary
data_dict = { r['id'] : r for r in data}
for r in data:
if r['parent_id'] in data_dict:
parent = data_dict[r['parent_id']]
if 'children' not in parent:
parent['children'] = []
parent['children'].append(r)
# Helper function to get all the id's associated with a parent
def get_all_ids(r):
l = list()
l.append(r['id'])
if 'children' in r:
for c in r['children']:
l.extend(get_all_ids(c))
return l
# Trimp the results to have a id only once
ids = set(data_dict.keys())
result = []
for r in data_dict.values():
the_ids = set(get_all_ids(r))
if ids.intersection(the_ids):
ids = ids.difference(the_ids)
result.append(r)
print (result)
Output:
[{'id': 1, 'parent_id': 0, 'name': 'Wood', 'price': 0, 'children': [{'id': 2, 'parent_id': 1, 'name': 'Mango', 'price': 18, 'children': [{'id': 3, 'parent_id': 2, 'name': 'Table', 'price': 342}, {'id': 4, 'parent_id': 2, 'name': 'Box', 'price': 340, 'children': [{'id': 5, 'parent_id': 4, 'name': 'Pencil', 'price': 240}]}]}]}, {'id': 6, 'parent_id': 0, 'name': 'Electronic', 'price': 20, 'children': [{'id': 7, 'parent_id': 6, 'name': 'TV', 'price': 350}, {'id': 8, 'parent_id': 6, 'name': 'Mobile', 'price': 300, 'children': [{'id': 9, 'parent_id': 8, 'name': 'Iphone', 'price': 0, 'children': [{'id': 10, 'parent_id': 9, 'name': 'Iphone 10', 'price': 400}]}]}]}]
I worked out a VERY SHORT solution, I believe it isn't the most efficient algorithm, but it does the job, will need a hell of optimization to work on very large data sets.
for i in range(len(data)-1, -1, -1):
data[i]["children"] = [child for child in data if child["parent_id"] == data[i]["id"]]
for child in data[i]["children"]:
data.remove(child)
Here is the complete explanation:
data = [
{"id":1, "parent_id": 0, "name": "Wood", "price": 0},
{"id":2, "parent_id": 1, "name": "Mango", "price": 18},
{"id":3, "parent_id": 2, "name": "Table", "price": 342},
{"id":4, "parent_id": 2, "name": "Box", "price": 340},
{"id":5, "parent_id": 4, "name": "Pencil", "price": 240},
{"id":6, "parent_id": 0, "name": "Electronic", "price": 20},
{"id":7, "parent_id": 6, "name": "TV", "price": 350},
{"id":8, "parent_id": 6, "name": "Mobile", "price": 300},
{"id":9, "parent_id": 8, "name": "Iphone", "price": 0},
{"id":10, "parent_id": 9, "name": "Iphone 10", "price": 400}
]
# Looping backwards,placing the lowest child
# into the next parent in the heirarchy
for i in range(len(data)-1, -1, -1):
# Create a dict key for the current parent in the loop called "children"
# and assign to it a list comprehension that loops over all items in the data
# to get the elements which have a parent_id equivalent to our current element's id
data[i]["children"] = [child for child in data if child["parent_id"] == data[i]["id"]]
# since the child is placed inside our its parent already, we will
# remove it from its actual position in the data
for child in data[i]["children"]:
data.remove(child)
# print the new data structure
print(data)
And here is the output:
[{'id': 1, 'parent_id': 0, 'name': 'Wood', 'price': 0, 'children': [{'id': 2, 'parent_id': 1, 'name': 'Mango', 'price': 18, 'children': [{'id': 3, 'parent_id': 2, 'name': 'Table', 'price': 342, 'children': []}, {'id': 4, 'parent_id': 2, 'name': 'Box', 'price': 340, 'children': [{'id': 5, 'parent_id': 4, 'name': 'Pencil', 'price': 240, 'children': []}]}]}]}, {'id': 6, 'parent_id': 0, 'name': 'Electronic', 'price': 20, 'children': [{'id': 7, 'parent_id': 6, 'name': 'TV', 'price': 350, 'children': []}, {'id': 8, 'parent_id': 6, 'name': 'Mobile', 'price': 300, 'children': [{'id': 9, 'parent_id': 8, 'name': 'Iphone', 'price': 0, 'children': [{'id': 10, 'parent_id': 9, 'name': 'Iphone 10', 'price': 400, 'children': []}]}]}]}]

Elasticsearch [5.6] Sum Aggregations Losing Precision?

I'm trying to do a sum aggregation of amounts ($21.28 for example). But in the aggregation result, it only shows (21.0)
I've also tried changing the type of the mapping to float, and I got the same results.
The query itself looks like:
'aggs': {
'total': {
'sum': {
'field': 'amount'
}
}
}
And the mapping looks like:
'amount': {
'type': 'double',
'index': 'not_analyzed',
'store': False
},
And finally, here's the result, I've omitted some data but the important bits are the amounts:
{
'took': 3,
'aggregations': {'total': {'value': 21.0}},
'hits': {'total': 1, 'max_score': 0.51623213,
'hits': [
{
'_score': 0.51623213,
'_index': 'some_index',
'_type': 'donation',
'_source': {
'amount': 21.28,
'created_on': '2019-06-15T01:37:42.451249+00:00'
}
}
]},
'timed_out': False,
'_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}
}
I'd expect to see 21.28 in the results, and not 21.0.
My solution was to update to Elasticsearch 6.x

Interactive Plotly Barchart

I am attempting to adapt the Python Plotly example on 'Adding Sliders to Animations in Python' to a barchart but am getting a 'Figure field is invalid' message.
I am trying to adapt the input data to reflect that of a bar chart rather than a scatter chart (used in the example). I have created a grid:
Which I am using with the following code:
years = ['2007','2008','2009']
items = ['Name_1','Name_2']
col_name_template = '{column}'
for year in years:
frame = {'data': [], 'name': str(year)}
x_list = []
y_list = []
for item in items:
x_list.append(grid.get_column_reference(col_name_template.format(column='name')))
y_list.append(grid.get_column_reference(col_name_template.format(column=year)))
frame['data'].append(go.Bar(
x=x_list,
y=y_list
))
figure['frames'].append(frame)
slider_step = {'args': [
[year],
{'frame': {'duration': 300, 'redraw': False},
'mode': 'immediate',
'transition': {'duration': 300}}
],
'label': year,
'method': 'animate'}
sliders_dict['steps'].append(slider_step)
figure['layout']['sliders'] = [sliders_dict]
py.icreate_animations(figure, 'barchart example')
When trying to plot I get the following error:
Figure field is invalid. Reason: Raw data arrays are not allowed at this endpoint. Use grid references instead. Raw data found at the following paths in the figure...
How do I use only grid references but also ensure that a bar chart is plotted rather than a scatter chart?
I used the offline method of plotting plotly.offline.iplot which doesn't require grid references. The only downside of animations with bar charts is that transitions aren't currently supported.
Code below (including buttons and slider):
from plotly.offline import init_notebook_mode, iplot
from IPython.display import display, HTML
init_notebook_mode(connected = True)
years = ['2010', '2011', '2012']
items = ['A', 'B', 'C', 'D']
count = [
[1, 2, 3, 4],
[2, 3, 4, 1],
[3, 4, 1, 2]
]
figure = {
'data': [{
'type': 'bar',
'x': items,
'y': count[0]
}],
'layout': {
'xaxis': {
'title': 'X',
'gridcolor': '#FFFFFF',
'linecolor': '#000',
'linewidth': 1,
'zeroline': False,
'autorange': False
},
'yaxis': {
'title': 'Y',
'gridcolor': '#FFFFFF',
'linecolor': '#000',
'linewidth': 1,
'range': [0, 5],
'autorange': False
},
'title': 'Example Title',
'hovermode': 'closest',
'updatemenus': [{
'type': 'buttons',
'buttons': [{
'label': 'Play',
'method': 'animate',
'args': [None, {
'frame': {
'duration': 500,
'redraw': True
},
'fromcurrent': True,
'transition': {
'duration': 300,
'easing': 'quadratic-in-out'
}
}]
},
{
'label': 'End',
'method': 'animate',
'args': [None, {
'frame': {
'duration': 0,
'redraw': True
},
'fromcurrent': True,
'mode': 'immediate',
'transition': {
'duration': 0
}
}]
}
],
'direction': 'left',
'pad': {
'r': 10,
't': 87
},
'showactive': False,
'type': 'buttons',
'x': 0.1,
'xanchor': 'right',
'y': 0,
'yanchor': 'top'
}]
},
'frames': []
}
sliders_dict = {
'active': 0,
'yanchor': 'top',
'xanchor': 'left',
'currentvalue': {
'font': {
'size': 20
},
'prefix': 'Year:',
'visible': True,
'xanchor': 'right'
},
'transition': {
'duration': 300,
'easing': 'cubic-in-out'
},
'pad': {
'b': 10,
't': 50
},
'len': 0.9,
'x': 0.1,
'y': 0,
'steps': []
}
for index, year in enumerate(years):
frame = {
'data': [{
'type': 'bar',
'x': items,
'y': count[index]
}],
'name': str(year)
}
figure['frames'].append(frame)
slider_step = {
'args': [
[year],
{
'frame': {
'duration': 300,
'redraw': True
},
'mode': 'immediate',
'transition': {
'duration': 300
}
}
],
'label': year,
'method': 'animate'
}
sliders_dict['steps'].append(slider_step)
figure['layout']['sliders'] = [sliders_dict]
iplot(figure)

build Hierarchical comment tree?

I am trying to implement threaded comments in my django project which I want to look like this:
data
comment_list = [
{'id': 1, 'content': '...', 'pid': None, 'children_comments': []},
{'id': 2, 'content': '...', 'pid': None, 'children_comments': []},
{'id': 3, 'content': '...', 'pid': 1, 'children_comments': []},
{'id': 4, 'content': '...', 'pid': 3, 'children_comments': []},
{'id': 5, 'content': '...', 'pid': 4, 'children_comments': []},
{'id': 6, 'content': '...', 'pid': 2, 'children_comments': []},
{'id': 7, 'content': '...', 'pid': None, 'children_comments': []},
{'id': 8, 'content': '...', 'pid': 7, 'children_comments': []},
{'id': 9, 'content': '...', 'pid': None, 'children_comments': []},
{'id': 10, 'content': '...', 'pid': 9, 'children_comments': []},
]
for example
1
3
4
5
2
6
7
8
9
10
my code:
new = []
for comment in comment_list:
if comment['pid'] != None:
for i in comment_list:
if i['id'] == comment['pid']:
i['children_comments'].append(comment)
else:
new.append(comment)
i think that is not good
final in django jinjia how to show??
Sure.
The algorithm is as follows:
iterate over the flat list of comments, gathering up comments by their parentage into a comments_by_parent mapping
iterate over the list again, assigning children_comments from the comments_by_parent mapping
get the root-level comments
from collections import defaultdict
comment_list = [
{'id': 1, 'content': '...', 'pid': None},
{'id': 2, 'content': '...', 'pid': None},
{'id': 3, 'content': '...', 'pid': 1},
{'id': 4, 'content': '...', 'pid': 3},
{'id': 5, 'content': '...', 'pid': 4},
{'id': 6, 'content': '...', 'pid': 2},
{'id': 7, 'content': '...', 'pid': None},
{'id': 8, 'content': '...', 'pid': 7},
{'id': 9, 'content': '...', 'pid': None},
{'id': 10, 'content': '...', 'pid': 9},
]
comments_by_parent = defaultdict(list)
for comment in comment_list:
comments_by_parent[comment['pid']].append(comment)
for comment in comment_list:
comment['children_comments'] = comments_by_parent[comment['id']]
root_comments = comments_by_parent[None]
root_comments will end up looking like this (JSON output for clarity).
[
{
"id": 1,
"content": "...",
"pid": null,
"children_comments": [
{
"id": 3,
"content": "...",
"pid": 1,
"children_comments": [
{
"id": 4,
"content": "...",
"pid": 3,
"children_comments": [
{
"id": 5,
"content": "...",
"pid": 4,
"children_comments": []
}
]
}
]
}
]
},
{
"id": 2,
"content": "...",
"pid": null,
"children_comments": [
{
"id": 6,
"content": "...",
"pid": 2,
"children_comments": []
}
]
},
{
"id": 7,
"content": "...",
"pid": null,
"children_comments": [
{
"id": 8,
"content": "...",
"pid": 7,
"children_comments": []
}
]
},
{
"id": 9,
"content": "...",
"pid": null,
"children_comments": [
{
"id": 10,
"content": "...",
"pid": 9,
"children_comments": []
}
]
}
]
You can then output this in Jinja using a recursive for loop:
<ul>
{%- for comment in root_comments recursive %}
<li>
{{ comment.content }}
{%- if comment.children_comments -%}
<ul>{{ loop(comment.children_comments) }}</ul>
{%- endif %}
</li>
{%- endfor %}
</ul>

Categories

Resources