Sublist values to two different variables? - python

I'm trying to assign two values of a list to two different variables. Here is the json list. Its raising key error. Please let me know where I am wrong.
[{'min': 1.158, 'max': 1.150, 'id': 269097, 'to': 1532003820, 'from': 1532003760, 'check': 1.15852, 'no_check': 1.15822, 'volume': 0},{'min': 1.1, 'max': 1.17, 'id': 269098, 'to': 1532003880, 'from': 1532003820, 'check': 1.158615, 'nocheck': 1.158515, 'volume': 0}]
Here is my code python3 code:
pt = [{'min': 1.158, 'max': 1.150, 'id': 269097, 'to': 1532003820, 'from': 1532003760, 'check': 1.15852, 'no_check': 1.15822, 'volume': 0},{'min': 1.1, 'max': 1.17, 'id': 269098, 'to': 1532003880, 'from': 1532003820, 'check': 1.158615, 'nocheck': 1.158515, 'volume': 0}]
y = [item[0] for item in pt]
z = [item[0] for item in pt]
print(y)
print(z)
Error:
File "test_YourM.py", line 19, in <module>
y = [item[0][0] for item in pt] File "test_YourM.py", line 19, in <listcomp>
y = [item[0][0] for item in pt] KeyError: 0
Expected output:
print(y) # {'min': 1.158, 'max': 1.150, 'id': 269097, 'to': 1532003820, 'from': 1532003760, 'check': 1.15852, 'no_check': 1.15822, 'volume': 0}
print(z) # {'min': 1.1, 'max': 1.17, 'id': 269098, 'to': 1532003880, 'from': 1532003820, 'check': 1.158615, 'nocheck': 1.158515, 'volume': 0}

[item for item in pt[0]]
[item for item in pt[1]]
The item is generated in in that scope, while pt isn't, even though you're enumerating a dict, you may want to do something like this:
{key: value for key, value in pt[0].items()}
{key: value for key, value in pt[1].items()}

Related

How to return key/value pairs in nested dict whose value is a list that contains said dict

As you can see the question itself is convoluted, but so is this problem. I'm trying to get the key/value pairs for name: name, symbol: symbol, and price: price in this API output. I've tried several things but can't seem to get it straight. Any help is appreciated.
Here is the dict:
data = {
'coins':
[
{
'id': 'bitcoin',
'icon': 'https://static.coinstats.app/coins/Bitcoin6l39t.png',
'name': 'Bitcoin',
'symbol': 'BTC',
'rank': 1,
'price': 48918.27974434776,
'priceBtc': 1,
'volume': 44805486777.77573,
'marketCap': 924655133704.012,
'availableSupply': 18902037,
'totalSupply': 21000000,
'priceChange1h': 0.05,
'priceChange1d': 1.22,
'priceChange1w': -3.02,
'websiteUrl': 'http://www.bitcoin.org',
'twitterUrl': 'https://twitter.com/bitcoin',
'exp': [
'https://blockchair.com/bitcoin/',
'https://btc.com/', 'https://btc.tokenview.com/'
]
},
{
'id': 'ethereum',
'icon': 'https://static.coinstats.app/coins/EthereumOCjgD.png',
'name': 'Ethereum',
'symbol': 'ETH',
'rank': 2,
'price': 4037.7735178501143,
'priceBtc': 0.08254618952631135,
'volume': 37993691263.19707,
'marketCap': 479507778421.57574,
'availableSupply': 118755491.4365,
'totalSupply': 0,
'priceChange1h': 0.3,
'priceChange1d': 4.53,
'priceChange1w': -8.85,
'websiteUrl': 'https://www.ethereum.org/',
'twitterUrl': 'https://twitter.com/ethereum',
'contractAddress': '0x2170ed0880ac9a755fd29b2688956bd959f933f8',
'decimals': 18,
'exp': [
'https://etherscan.io/',
'https://ethplorer.io/',
'https://blockchair.com/ethereum',
'https://eth.tokenview.com/'
]
}
]
}
Here's one of my failed attempts:
print(data['coins'][1])
new_dict = data['coins'][1]
for i in new_dict:
a = i.split(',')
print(a)
I'm not sure what the expected output is since you do not clarify, though you can access each key-value for each stock as such:
for stock in data["coins"]:
print(stock["name"] + " - " + stock["symbol"] + " - " + str(stock["price"]))
Output:
Bitcoin - BTC - 48918.27974434776
Ethereum - ETH - 4037.7735178501143

How to rename keys in a dictionary and make a dataframe of it?

I have a complex situation which I hope to solve and which might profit us all. I collected data from my API, added a pagination and inserted the complete data package in a tuple named q1 and finally I have made a dictionary named dict_1of that tuple which looks like this:
dict_1 = {100: {'ID': 100, 'DKSTGFase': None, 'DK': False, 'KM': None,
'Country: {'Name': GE', 'City': {'Name': 'Berlin'}},
'Type': {'Name': '219'}, 'DKObject': {'Name': '8555', 'Object': {'Name': 'Car'}},
'Order': {'OrderId': 101, 'CreatedOn': '2018-07-06T16:54:36.783+02:00',
'ModifiedOn': '2018-07-06T16:54:36.783+02:00',
'Name': Audi, 'Client': {‘1’ }}, 'DKComponent': {'Name': ‘John’}},
{200: {'ID': 200, 'DKSTGFase': None, 'DK': False, ' KM ': None,
'Country: {'Name': ES', 'City': {'Name': 'Madrid'}}, 'Type': {'Name': '220'},
'DKObject': {'Name': '8556', 'Object': {'Name': 'Car'}},
'Order': {'OrderId': 102, 'CreatedOn': '2018-07-06T16:54:36.783+02:00',
'ModifiedOn': '2018-07-06T16:54:36.783+02:00',
'Name': Mercedes, 'Client': {‘2’ }}, 'DKComponent': {'Name': ‘Sergio’}},
Please note that in the above dictionary I have just stated 2 records. The actual dictionary has 1400 records till it reaches ID 1500.
Now I want to 2 things:
I want to change some keys for all the records. key DK has to become DK1. Key Name in Country has to become Name1 and Name in Object has to become 'Name2'
The second thing I want is to make a dataFrame of the whole bunch of data. My expected outcome is:
This is my code:
q1 = response_2.json()
next_link = q1['#odata.nextLink']
q1 = [tuple(q1.values())]
while next_link:
new_response = requests.get(next_link, headers=headers, proxies=proxies)
new_data = new_response.json()
q1.append(tuple(new_data.values()))
next_link = new_data.get('#odata.nextLink', None)
dict_1 = {
record['ID']: record
for tup in q1
for record in tup[2]
}
#print(dict_1)
for x in dict_1.values():
x['DK1'] = x['DK']
x['Country']['Name1'] = x['Country']['Name']
x['Object']['Name2'] = x['Object']['Name']
df = pd.DataFrame(dict_1)
When i run this I receive the following Error:
Traceback (most recent call last):
File "c:\data\FF\Desktop\Python\PythongMySQL\Talky.py", line 57, in <module>
x['Country']['Name1'] = x['Country']['Name']
TypeError: 'NoneType' object is not subscriptable
working code
lists=[]
alldict=[{100: {'ID': 100, 'DKSTGFase': None, 'DK': False, 'KM': None,
'Country': {'Name': 'GE', 'City': {'Name': 'Berlin'}},
'Type': {'Name': '219'}, 'DKObject': {'Name': '8555', 'Object': {'Name': 'Car'}},
'Order': {'OrderId': 101, 'CreatedOn': '2018-07-06T16:54:36.783+02:00',
'ModifiedOn': '2018-07-06T16:54:36.783+02:00',
'Name': 'Audi', 'Client': {'1' }}, 'DKComponent': {'Name': 'John'}}}]
for eachdict in alldict:
key=list(eachdict.keys())[0]
eachdict[key]['DK1']=eachdict[key]['DK']
del eachdict[key]['DK']
eachdict[key]['Country']['Name1']=eachdict[key]['Country']['Name']
del eachdict[key]['Country']['Name']
eachdict[key]['DKObject']['Object']['Name2']=eachdict[key]['DKObject']['Object']['Name']
del eachdict[key]['DKObject']['Object']['Name']
lists.append([key, eachdict[key]['DK1'], eachdict[key]['KM'], eachdict[key]['Country']['Name1'],
eachdict[key]['Country']['City']['Name'], eachdict[key]['DKObject']['Object']['Name2'], eachdict[key]['Order']['Client']])
pd.DataFrame(lists, columns=[<columnNamesHere>])
Output:
{100: {'ID': 100,
'DKSTGFase': None,
'KM': None,
'Country': {'City': {'Name': 'Berlin'}, 'Name1': 'GE'},
'Type': {'Name': '219'},
'DKObject': {'Name': '8555', 'Object': {'Name2': 'Car'}},
'Order': {'OrderId': 101,
'CreatedOn': '2018-07-06T16:54:36.783+02:00',
'ModifiedOn': '2018-07-06T16:54:36.783+02:00',
'Name': 'Audi',
'Client': {'1'}},
'DKComponent': {'Name': 'John'},
'DK1': False}}

How to access the internal value of a nested dictionary

I have following nested dictionary,
Data = [{'seriesId': 'Food and Beverage',
'forecastPoint': '2020-08-26T00:00:00Z',
'rowId': 35,
'timestamp': '2020-08-27T00:00:00.000000Z',
'predictionValues': [{'value': 32.5133330947, 'label': 'Volume (actual)'}],
'forecastDistance': 1,
'prediction': 32.5133330947},
{'seriesId': 'Food and Beverage',
'forecastPoint': '2020-08-26T00:00:00Z',
'rowId': 36,
'timestamp': '2020-08-28T00:00:00.000000Z',
'predictionValues': [{'value': 30.2438893873, 'label': 'Volume (actual)'}],
'forecastDistance': 2,
'prediction': 30.2438893873}]
However I want to bring that dictionary into following form dictionary where I only want to access the value from predictionValues and get other keys as it is.
Expected output should be a dictionary
Data = [{'seriesId': 'Food and Beverage',
'forecastPoint': '2020-08-26T00:00:00Z',
'rowId': 35,
'timestamp': '2020-08-27T00:00:00.000000Z',
'value': 32.5133330947,
'forecastDistance': 1,
'prediction': 32.5133330947},
{'seriesId': 'Food and Beverage',
'forecastPoint': '2020-08-26T00:00:00Z',
'rowId': 36,
'timestamp': '2020-08-28T00:00:00.000000Z',
'value': 30.2438893873,
'forecastDistance': 2,
'prediction': 30.2438893873}]
Try this
for d in Data:
d.update({'value': d.pop('predictionValues')[0].pop('value')})
Use the following
for i in range(len(Data)):
Data[i]['predictionValues'] = Data[i]['predictionValues'][0]['value']
The original list of dictionaries seems to contain the prediction value already, so just deleting the predictionValue entry should do the trick
for dictionary in Data:
del dictionary['predictionValues']
A bit of a play on list comprehension with dict :
[
dict( (key, value)
if not isinstance(value, list)
else ("value", entry[key][0]["value"])
for key, value in entry.items()
)
for entry in Data
]
[{'seriesId': 'Food and Beverage',
'forecastPoint': '2020-08-26T00:00:00Z',
'rowId': 35,
'timestamp': '2020-08-27T00:00:00.000000Z',
'value': 32.5133330947,
'forecastDistance': 1,
'prediction': 32.5133330947},
{'seriesId': 'Food and Beverage',
'forecastPoint': '2020-08-26T00:00:00Z',
'rowId': 36,
'timestamp': '2020-08-28T00:00:00.000000Z',
'value': 30.2438893873,
'forecastDistance': 2,
'prediction': 30.2438893873}]

Building a list inside a list - Python

I am new to Python. How can I store the following data as a list inside of another list?
inputList = [[{'timestamp': '2017-10-28T00:00:00.000Z', 'open': '0.051430', 'close': '0.051210', 'min': '0.050583', 'max': '0.051955', 'volume': '30953.184', 'volumeQuote': '1584.562468339'}, {'timestamp': '2017-10-29T00:00:00.000Z', 'open': '0.051191', 'close': '0.049403', 'min': '0.048843', 'max': '0.053978', 'volume': '42699.215', 'volumeQuote': '2190.567660769'}],[{'timestamp': '2017-10-28T00:00:00.000Z', 'open': '0.063390', 'close': '0.072991', 'min': '0.062544', 'max': '0.073524', 'volume': '199636.573', 'volumeQuote': '13427.870355674'}, {'timestamp': '2017-10-29T00:00:00.000Z', 'open': '0.072840', 'close': '0.073781', 'min': '0.069449', 'max': '0.090833', 'volume': '284448.623', 'volumeQuote': '21687.962221794'}]]
Output should be:
outputList = [[0.051210, 0.049403],[0.072991, 0.073781]]
and what I have so far is:
[0.051210, 0.049403, 0.072991, 0.073781]
I use the following code:
insideLoop = []
outputList = []
for list in inputList:
for i, v in enumerate(list):
closing = float(v['close'])
insideLoop.append(closing)
outputList.append(insideLoop)
To be noted that the inputList can be several lists long.
Any solution to this?
Thanks a lot!
You can use a simple list comprehension
result = [[x['close'], y['close']] for x, y in inputList]
print(result)
# - > [['0.051210', '0.049403'], ['0.072991', '0.073781']]
Update
For undetermined number of elements in sublist, use nested list comprehension
result = [[x['close'] for x in y] for y in inputList]
print(result)
# - > [['0.051210', '0.049403'], ['0.072991', '0.073781']]
You can use a nested list comprehension:
s = [[{'timestamp': '2017-10-28T00:00:00.000Z', 'open': '0.051430', 'close': '0.051210', 'min': '0.050583', 'max': '0.051955', 'volume': '30953.184', 'volumeQuote': '1584.562468339'}, {'timestamp': '2017-10-29T00:00:00.000Z', 'open': '0.051191', 'close': '0.049403', 'min': '0.048843', 'max': '0.053978', 'volume': '42699.215', 'volumeQuote': '2190.567660769'}],[{'timestamp': '2017-10-28T00:00:00.000Z', 'open': '0.063390', 'close': '0.072991', 'min': '0.062544', 'max': '0.073524', 'volume': '199636.573', 'volumeQuote': '13427.870355674'}, {'timestamp': '2017-10-29T00:00:00.000Z', 'open': '0.072840', 'close': '0.073781', 'min': '0.069449', 'max': '0.090833', 'volume': '284448.623', 'volumeQuote': '21687.962221794'}]]
new_s = [[float(i['close']) for i in b] for b in s]
Output:
[[0.051210, 0.049403], [0.072991, 0.073781]]
Try this:
result = []
for a in inputList:
res = []
for i in a:
res.append(i['close'])
result.append(res)
print(result)

Appending to List within a Dictionary

Objective: Append items from value['itemArray'] to Products['Items'] - see function fba_orders
Problem: The Current code only appends the last item of value['itemArray'] to Products['Items']
Current Output:
{'Items': [{'SellerFulfillmentOrderItemId': 266804219, 'SellerSKU': 'IX-GZ31-31K6', 'Quantity': 1}, {'SellerFulfillmentOrderItemId': 266804219, 'SellerSKU': 'IX-GZ31-31K6', 'Quantity': 1}]}
Correct Output would be:
{'Items': [{'SellerFulfillmentOrderItemId': 266804218, 'SellerSKU': 'KM-090914-840-BEARLAPTOP', 'Quantity': 1}, {'SellerFulfillmentOrderItemId': 266804219, 'SellerSKU': 'IX-GZ31-31K6', 'Quantity': 1}]}
Code:
import sys
VALUE = {'amountPaid': '43.38',
'amountSaved': 0.0,
'buyerCheckoutMessage': '',
'buyerUserID': 13182254,
'buyerUserName': 'W5Tiny',
'checkoutStatus': {'status': 'Complete'},
'createdTime': '2015-06-30T22:41:01Z',
'creatingUserRole': 'Buyer',
'itemArray': [{'item': {'itemID': 266804218,
'price': '21.1',
'quantity': 1,
'sellerInventoryID': 'KM-090914-840-BEARLAPTOP',
'sk': 'KM-090914-840-BEARLAPTOP',
'title': u"VTech Bear's Baby Laptop, Blue [Toy]"}},
{'item': {'itemID': 266804219,
'price': '22.28',
'quantity': 1,
'sellerInventoryID': 'IX-GZ31-31K6',
'sk': 'IX-GZ31-31K6',
'title': 'Toy State Caterpillar Push Powered Rev It Up Dump Truck [Toy]'}}],
'orderID': 34013525,
'orderStatus': 'Completed',
'paidTime': '2015-06-30T22:50:38Z',
'shippingAddress': {'addressID': 15798541,
'cityName': 'Nashville',
'country': 'US',
'countryName': None,
'name': 'UNKNOWN',
'postalCode': '37221',
'stateOrProvince': 'TN',
'street1': '123 BOOGIE DRIVE',
'street2': None},
'shippingDetails': {'amount': '0.0',
'insuranceFee': 0,
'servicesArray': [],
'shippingService': 'Standard shipping'},
'subtotal': 43.38,
'taxAmount': 0.0,
'total': '43.38',
'transactionArray': {'transaction': {'buyer': {'email': 'fakewilson259612#hotmail.com'},
'finalValueFee': '0.0',
'providerID': '11V84334FD304010L',
'providerName': 'Paypal'}}}
def fba_order():
address = {}
products = {'Items': []}
item = {}
Items = []
address['City'] = VALUE['shippingAddress']['cityName']
address['CountryCode'] = VALUE['shippingAddress']['country']
address['Line1'] = VALUE['shippingAddress']['street1']
address['Line2'] = VALUE['shippingAddress']['street2']
address['Name'] = VALUE['shippingAddress']['name']
address['PostalCode'] = VALUE['shippingAddress']['postalCode']
address['StateOrProvinceCode'] = VALUE['shippingAddress']['stateOrProvince']
for items in VALUE['itemArray']:
item['Quantity'] = items['item']['quantity']
item['SellerFulfillmentOrderItemId'] = items['item']['itemID']
item['SellerSKU'] = items['item']['sk']
products['Items'].append(item)
continue
print address, '\n', products
if __name__ == '__main__':
sys.exit(fba_order())
You are reusing the dictionary referenced by item over and over again; appending this one dictionary won't create a new copy. Rather, you are adding multiple references to that one dictionary. As you continue to alter the dictionary all those references will show those changes.
Better produce an entirely new dictionary for each loop iteration:
for items in VALUE['itemArray']:
item = {
'Quantity': items['item']['quantity'],
'SellerFulfillmentOrderItemId': items['item']['itemID']
'SellerSKU': items['item']['sk'],
}
products['Items'].append(item)
The issue is that you are creating item outside the for loop and then just changing the values inside the for loop and appending it to the list.
dictionaries are reference, and hence even after appending to products['Items'] list if you change the item dictionary it will make changes to the item that was appended to the list.
You want to initialize item to a new dictionary inside the for loop.
Example -
def fba_order():
address = {}
products = {'Items': []}
Items = []
address['City'] = VALUE['shippingAddress']['cityName']
address['CountryCode'] = VALUE['shippingAddress']['country']
address['Line1'] = VALUE['shippingAddress']['street1']
address['Line2'] = VALUE['shippingAddress']['street2']
address['Name'] = VALUE['shippingAddress']['name']
address['PostalCode'] = VALUE['shippingAddress']['postalCode']
address['StateOrProvinceCode'] = VALUE['shippingAddress']['stateOrProvince']
for items in VALUE['itemArray']:
item = {}
item['Quantity'] = items['item']['quantity']
item['SellerFulfillmentOrderItemId'] = items['item']['itemID']
item['SellerSKU'] = items['item']['sk']
products['Items'].append(item)
continue
print address, '\n', products

Categories

Resources