I'm a newbee in Python so excuse if my question looks, dummy.
I have a json file which look like this
[{'_id': '1', 'date': '2019-09-07', 'name': 'abi', 'value': 0, 'unit': '°C'},
{'_id': '2', 'date': '2019-09-08', 'name': 'allo', 'value': 3, 'unit': '°F'},
{'_id': '3', 'date': '2019-09-09', 'name': 'ali', 'value': 0, 'unit': '°C'}]
and I want to read this json file in order to convert it into a dictionary of array which looks like
[{'_id': [ '1', '2','3']},
{'date': [ '2019-09-07', '2019-09-08','2019-09-09']},
{'name': [ 'abi', 'allo','ali']},
{'value': [ '0', '3','0']},
{'unit': [ '°C', '°F','°C']},]
Thank you in advance
Use collections.defaultdict
Ex:
from collections import defaultdict
data = [{'_id': '1', 'date': '2019-09-07', 'name': 'abi', 'value': 0, 'unit': '°C'},
{'_id': '2', 'date': '2019-09-08', 'name': 'allo', 'value': 3, 'unit': '°F'},
{'_id': '3', 'date': '2019-09-09', 'name': 'ali', 'value': 0, 'unit': '°C'}]
result = defaultdict(list)
for i in data:
for k, v in i.items():
result[k].append(v)
print(result)
or .setdefault
Ex:
result = {}
for i in data:
for k, v in i.items():
result.setdefault(k, []).append(v)
print(result)
Output:
{'_id': ['1', '2', '3'],
'date': ['2019-09-07', '2019-09-08', '2019-09-09'],
'name': ['abi', 'allo', 'ali'],
'unit': ['°C', '°F', '°C'],
'value': [0, 3, 0]}
Related
Assume I have this:
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373}
and I want to add this to the dictionary:
{'id': 325910, 'order_id': 109179557, 'matched_order_id': 109179004,
'direction': 'BUY', 'trading_pair_id': 50, 'symbol': 'BTC/ECS', 'amount': '0.001',
'price': '193499.99', 'date': 1654429749384}
So it will look like this: (If it possible.)
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373},
{'id': 325910, 'order_id': 109179557, 'matched_order_id': 109179004,
'direction': 'BUY', 'trading_pair_id': 50, 'symbol': 'BTC/ECS', 'amount': '0.001',
'price': '193499.99', 'date': 1654429749384}
I'm not good to python, so I don't know if this is possible. If it is possible how do I search for element: 'trading_pair_id': 50 and if the 'id' for that index has changed I want to update all elements with new data for that index.
I dont think the output you mentioned in your question is a valid dictionary.. It's more like a tuple.. So I came up with my code to join these two dictionaries.
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373}
another_tradedict = {'id': 325910, 'order_id': 109179557, 'matched_order_id': 109179004,
'direction': 'BUY', 'trading_pair_id': 50, 'symbol': 'BTC/ECS', 'amount': '0.001',
'price': '193499.99', 'date': 1654429749384}
for i in tradedict:
temp = []
temp.append(tradedict[i])
temp.append(another_tradedict[i])
tradedict[i] = temp
print(tradedict)
And the output would be something like this,
{'id': [325920, 325910], 'order_id': [109185014, 109179557], 'matched_order_id': [109181538, 109179004], 'direction': ['BUY', 'BUY'], 'trading_pair_id': [48, 50], 'symbol': ['ECS/EUR', 'BTC/ECS'], 'amount': ['1', '0.001'], 'price': ['0.1507', '193499.99'], 'date': [1654433352373, 1654429749384]}
You cannot repeat keys in a dictionary
What I meant to say is,
Your primary dictionary is having a key name "id", "order_id" etc
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373}
But the dictionary you wish to add also has the same keys, hence you will not be able to add these two dictionaries in particular.
But what you can do is:
You can add new key-value pairs in your dictionary
tradeict["Company"]="amazon"
You will get this as a result
{'id': 325920,
'order_id': 109185014,
'matched_order_id': 109181538,
'direction': 'BUY',
'trading_pair_id': 48,
'symbol': 'ECS/EUR',
'amount': '1',
'price': '0.1507',
'date': 1654433352373,
'company': 'amazon'}
Notice how new key value pair has been added to the dictionary
I have code that is returning a list of dicts, which looks like this:
[{'id': 3605917, 'qty': 66640, 'side': 'Buy', 'time': 1609395938896, 'symbol': 'BTCUSD', 'price': 28901.5}, {'id': 3605914, 'qty': 500, 'side': 'Buy', 'time': 1609395936891, 'symbol': 'BTCUSD', 'price': 28907.5}, {'id': 3605911, 'qty': 1, 'side': 'Buy', 'time': 1609395874764, 'symbol': 'BTCUSD', 'price': 28942}, {'id': 3605449, 'qty': 70000, 'side': 'Sell', 'time': 1609384815688, 'symbol': 'BTCUSD', 'price': 28956}, {'id': 3605440, 'qty': 40, 'side': 'Sell', 'time': 1609384671382, 'symbol': 'BTCUSD', 'price': 28940}]
Reading through some previous questions, I was able to get some code to allow me to sum the total quantity for each side, where stream is the list above.
from collections import defaultdict
b = defaultdict(int)
for q in stream:
b[q['side']] += q['qty']
print(b)
This returns something that looks like this. I truncated the list above, so the numbers below won't match the example list:
defaultdict(<class 'int'>, {'Buy': 8106603, 'Sell': 1482687})
I'd like to modify the code above to show both the sum of the quantity, but also the average price. Weighted average by qty would be ideal, but would also be ok with a simple average.
For printing sum of quantities, one-liners:
a = [{'id': 3605917, 'qty': 66640, 'side': 'Buy', 'time': 1609395938896, 'symbol': 'BTCUSD', 'price': 28901.5}, {'id': 3605914, 'qty': 500, 'side': 'Buy', 'time': 1609395936891, 'symbol': 'BTCUSD', 'price': 28907.5}, {'id': 3605911, 'qty': 1, 'side': 'Buy', 'time': 1609395874764, 'symbol': 'BTCUSD', 'price': 28942}, {'id': 3605449, 'qty': 70000, 'side': 'Sell', 'time': 1609384815688, 'symbol': 'BTCUSD', 'price': 28956}, {'id': 3605440, 'qty': 40, 'side': 'Sell', 'time': 1609384671382, 'symbol': 'BTCUSD', 'price': 28940}]
print('Buy', sum(i['qty'] for i in a if i['side'] == 'Buy'))
print('Sell', sum(i['qty'] for i in a if i['side'] == 'Sell'))
For average of prices:
print(sum(i['qty']*i['price'] for i in a if i['side'] == 'Buy')/len(list(i for i in a if i['side'] == 'Buy')))
print(sum(i['qty']*i['price'] for i in a if i['side'] == 'Sell')/len(list(i for i in a if i['side'] == 'Sell')))
Guys i have this table in dash the following columns are drop down columns Reason Code, Staging and Overwrite. Based on their values i want to create values under Final Staging column dynamically in dash
https://i.stack.imgur.com/MDCMb.png
Here is my code
app.layout = html.Div([
dash_table.DataTable(
id='table-dropdown',
data=staging.to_dict('records'),
columns=[
{'id': 'customer_id', 'name': 'Customer ID'},
{'id': 'booking_date', 'name': 'Booking Date'},
{'id': 'oustanding_balance_(currency)',
'name': 'Outstanding Balance(currency)'},
{'id': 'booking_date', 'name': 'Booking Date'},
{'id': 'past_due_days', 'name': 'Past Due Days'},
{'id': 'segment', 'name': 'Segment'},
{'id': 'contract_rate_%', 'name': 'Contract Rate(%)'},
{'id': 'fee/commission_rate_%', 'name': 'Fee/Commission Rate(%)'},
{'id': 'collateral_(force_sale_value_(currency)',
'name': 'Collateral(currency)'},
{'id': 'collateral_(type)', 'name': 'Collateral(Type)'},
{'id': 'maturity_date', 'name': 'Maturity Date'},
{'id': 'repayment_years', 'name': 'Repayment(Years)'},
{'id': 'quantitative_assessment', 'name': 'Quantitative Assessment'},
{'id': 'reason_code',
'name': 'Reason Code', 'presentation': 'dropdown'},
{'id': 'staging',
'name': 'Staging', 'presentation': 'dropdown'},
{'id': 'overwrite',
'name': 'Overwrite', 'presentation': 'dropdown'},
{'id': 'final_staging', 'name': 'Final Staging'},
],
editable=True,
dropdown={
'reason_code': {
'options': [
{'label': i, 'value': i}
for i in staging['reason_code'].unique()
]
},
'staging': {
'options': [
{'label': i, 'value': i}
for i in staging['staging'].unique()
]
},
'overwrite': {
'options': [
{'label': i, 'value': i}
for i in staging['overwrite'].unique()
]
}
}
),
html.Div(id='table-dropdown-container')
])
I have this two dictionaries:
{'data': {'id': '001_101_001', 'name': 'chview', 'type': 'multiple', 'mapping': {}},
{'id': '001_102_001', 'name': 'view', 'type': 'binary', 'mapping': {'abc':'exp'}}
And:
{'queries':{'view': 'text', 'chview': 'text1'}}
The desired output should be:
{'new_data' : {'001_101_001': { 'query': 'text1', 'type': 'multiple', 'mapping': {}},
'001_102_001': { 'query1': 'text', 'type': 'binary', 'mapping': {'abc':'exp'}}
Because there are a lot of this dictionaries I need to match them by 'name', to have the coresponding id matched. Any ideas?
Your first dictionary has a problem, it is not hashable. It should be a list of dictionaries.
{"data" :[
{'id': '001_101_001', 'name': 'chview', 'type': 'multiple', 'mapping': {}},
{'id': '001_102_001', 'name': 'view', 'type': 'binary', 'mapping': {'abc':'exp'}}
]}
Complete code:
data = {"data" :[
{'id': '001_101_001', 'name': 'chview', 'type': 'multiple', 'mapping': {}},
{'id': '001_102_001', 'name': 'view', 'type': 'binary', 'mapping': {'abc':'exp'}}
]}
queries = {"queries" : {'view': 'text', 'chview': 'text1'}}
new_data = {}
for d in data["data"]:
item = {d["id"] : {
"query": queries["queries"][d["name"]],
"type": d["type"],
"mapping": d["mapping"]
}}
new_data.update(item)
print({"new_data": new_data})
OUTPUT:
{'new_data': {'001_101_001': {'query': 'text1', 'type': 'multiple', 'mapping': {}}, '001_102_001': {'query': 'text', 'type': 'binary', 'mapping': {'abc': 'exp'}}}}
I want get value (abc.com/p/B3N) from this json :
{'id': 123456, 'parent_id': 0, 'number': '23856', 'order_key': 'abc', 'created_via': 'checkout', 'version': '3.6.4', 'status': 'processing', 'currency': 'USD', 'date_created': '2019-10-05T13:18:49', 'date_created_gmt': '2019-10-05T13:18:49', 'date_modified': '2019-10-05T13:19:20', 'date_modified_gmt': '2019-10-05T13:19:20', 'discount_total': '0.00', 'discount_tax': '0.00', 'shipping_total': '0.00', 'shipping_tax': '0.00', 'cart_tax': '0.00', 'total': '0.40', 'total_tax': '0.00', 'prices_include_tax': False, 'customer_id': 0, 'customer_ip_address': '111.101.111.111', 'customer_user_agent': 'Mozilla/5.0 (Linux; Android 8.0.0; SAMSUNG SM-J337P) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/10.1 Chrome/71.0.3578.99 Mobile Safari/537.36', 'customer_note': '', 'billing': {'first_name': '', 'last_name': '', 'company': '', 'address_1': '', 'address_2': '', 'city': '', 'state': '', 'postcode': '', 'country': '', 'email': 'abc#gmail.com', 'phone': ''}, 'shipping': {'first_name': '', 'last_name': '', 'company': '', 'address_1': '', 'address_2': '', 'city': '', 'state': '', 'postcode': '', 'country': ''}, 'payment_method': 'paypal', 'payment_method_title': 'PayPal', 'transaction_id': '851R', 'date_paid': '2019-10-05T13:19:20', 'date_paid_gmt': '2019-10-05T13:19:20', 'date_completed': None, 'date_completed_gmt': None, 'cart_hash': '0675772a1e', 'meta_data': [{'id': 123456, 'key': 'is_vat_exempt', 'value': 'no'}, {'id': 123456, 'key': 'Payment type', 'value': 'instant'}, {'id': 274929, 'key': '_paypal_status', 'value': 'completed'}, {'id': 123456, 'key': 'PayPal Transaction Fee', 'value': '0.32'}], 'line_items': [{'id': 10927, 'name': 'Jeans', 'product_id': 1234, 'variation_id': 0, 'quantity': 1, 'tax_class': '', 'subtotal': '0.10', 'subtotal_tax': '0.00', 'total': '0.10', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 100000, 'key': '', 'value': 'Views $0.00 × 500'}, {'id': 100001, 'key': '', 'value': 'Worldwide'}, {'id': 100002, 'key': '', 'value': 'abc.com/p/B3N'}, {'id': 100003, 'key': '', 'value': '17'}], 'sku': '', 'price': 0.1}], 'tax_lines': [], 'shipping_lines': [], 'fee_lines': [{'id': 10928, 'name': 'PayPal Fee (Free Fee for order over $5)', 'tax_class': '0', 'tax_status': 'taxable', 'amount': '0.3', 'total': '0.30', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 122543, 'key': '_legacy_fee_key', 'value': 'paypal-fee'}]}], 'coupon_lines': [], 'refunds': [], '_links': {'self': [{'href': 'abc.com'}], 'collection': [{'href': 'abc.com'}]}}
this is my code
m = (wcapi.get(order + ordernumber).json())
n = json.dumps(m)
o = json.loads(n)
for i in o:
if i['id'] == '100002':
print(i['value'])
break
and i got this error :
if i['id'] == '100002':
TypeError: string indices must be integers
i have searched others topics but ... can't. thanks for help me!
When you do for i in o and o is a dictionary, the for loop iterates over the keys in o - which are strings in your case. Hence the error. i is a string.
To get the key you need to know the exact structure of o.
I'm gonna give you some examples:
o['id'] # 123456
o['billing']['email'] # "abc#gmai.com"
Now to get the value you want:
first_line_items_meta = o['line_items'][0]['metadata']
for item in first_line_items_meta:
if item['id'] == 100002:
print(item['value']) # "abc.com/p/B3N"