I am trying to get all the values individually for each asset (BCHUSD and TRXUSD).
What I want to do is something like this:
BCHUSD a = 301.340000 b = 301.160000 c = 301.280000
TRXUSD a = 0.0609450 b = 0.0609440 c = 0.0609540
Could someone tell me how I can do it please?
Regards!
import requests
import json
while True:
req = requests.get('https://api.kraken.com/0/public/Ticker?pair=BCHUSD,TRXUSD,XRPUSD')
print(req)
<Response [200]>
print(type(req))
<class 'requests.models.Response'>
obj = req.json()
print(type(obj))
<class 'dict'>
for k, v in obj.items():
if type(v) is dict and k:
for nk, nv in v.items():
print(nk, nv)
BCHUSD {'a': ['298.240000', '11', '11.000'], 'b': ['298.040000', '3', '3.000'], 'c':
['299.000000', '0.89507885'], 'v': ['38.42175237', '5614.56089299'], 'p':
['300.890848', '277.650439'], 't': [24, 2314], 'l': ['299.000000', '260.000000'], 'h':
['302.390000', '309.900000'], 'o': '299.000000'}
TRXUSD {'a': ['0.0608250', '4881', '4881.000'], 'b': ['0.0607820', '40500',
'40500.000'], 'c': ['0.0608630', '81.94337742'], 'v': ['21067.61432979',
'9622286.56922629'], 'p': ['0.0610566', '0.0589675'], 't': [25, 1729], 'l':
['0.0608630', '0.0562060'], 'h': ['0.0612840', '0.0618410'], 'o': '0.0611130'}
XXRPZUSD {'a': ['0.69018000', '666', '666.000'], 'b': ['0.69000000', '42829',
'42829.000'], 'c': ['0.69022000', '358.00000000'], 'v': ['287549.02071579',
'27810492.67564827'], 'p': ['0.69737332', '0.65981291'], 't': [429, 10340], 'l':
['0.69000000', '0.62229000'], 'h': ['0.70386000', '0.72105000'], 'o': '0.69935000'}
I think the following could help you as a starting point:
response_json = {
"title": "name",
"abc": {'a': [1,2,3], "b": [2,3,4]},
"ohter_stuff": "xxx",
"xyz": {'a': [10, 20 ,30], "b": [20, 30, 40]}
}
# select relevant key value pairs
data = {
key: value for key, value in response_json.items()
if isinstance(value, dict)
}
# get the inner subdict index length
length = len(data['abc']['a'])
# get the inner subdict keys
items = list(data['abc'].keys())
# loop and print
for index in range(length):
for name, subdict in data.items():
# join the items at index pairs into a string
index_items = " ".join(
f"{item} = {subdict[item][index]}"
for item in items
)
print(name, index_items)
This is a pure standard library python solution. If you can install other libraries, I would recommend to have a look into pandas.
Related
We have data like this
input = {
'a': 3,
'b': {'g': {'l': 12}},
'c': {
'q': 3,
'w': {'v': 3},
'r': 8,
'g': 4
},
'd': 4
}
It is not known in advance how many nesting levels there will be
We need to get the full address to the final value, all points of which are separated by a dot, or another special character
Like this:
a:3
b.g.l: 12
c.q: 3
c.w.v: 3
etc
I tried to solve this problem with a recursive function.
def recursive_parse(data: dict, cache: Optional[list]=None):
if cache is None:
cache = []
for k in data:
cache.append(k)
if not isinstance(data[k], dict):
print(f"{'.'.join(cache) } :{data[k]}")
cache.clear()
else:
recursive_parse(data[k], cache)
But I have problems with "remembering" the previous key of the nested dictionary.
a :3
b.g.l :12
c.q :3
w.v :3
r :8
g :4
d :4
What is the correct algorithm to solve this?
It's probably better to use an explicit stack for this, rather than the Python call stack. Recursion is slow in Python, due to high function call overhead, and the recursion limit is fairly conservative.
def dotted(data):
result = {}
stack = list(data.items())
while stack:
k0, v0 = stack.pop()
if isinstance(v0, dict):
for k1, v1 in v0.items():
item = ".".join([k0, k1]), v1
stack.append(item)
else:
result[k0] = v0
return result
Demo:
>>> data
{'a': 3,
'b': {'g': {'l': 12}},
'c': {'q': 3, 'w': {'v': 3}, 'r': 8, 'g': 4},
'd': 4}
>>> for k, v in reversed(dotted(data).items()):
... print(k, v)
...
a 3
b.g.l 12
c.q 3
c.w.v 3
c.r 8
c.g 4
d 4
Try:
dct = {
"a": 3,
"b": {"g": {"l": 12}},
"c": {"q": 3, "w": {"v": 3}, "r": 8, "g": 4},
"d": 4,
}
def parse(d, path=None):
if path is None:
path = []
if isinstance(d, dict):
for k, v in d.items():
yield from parse(v, path + [k])
else:
yield "{}: {}".format(".".join(path), d)
for p in parse(dct):
print(p)
Prints:
a: 3
b.g.l: 12
c.q: 3
c.w.v: 3
c.r: 8
c.g: 4
d: 4
I have a df as shown below
Params Value
teachers 49
students 289
R 3.7
holidays 165
OS 18
Em_from 2020-02-29T20:00:00.000Z
Em_to 2020-03-20T20:00:00.000Z
Em_F 3
Em_C 2
sC_from 2020-03-31T20:00:00.000Z
sC_to 2020-05-29T20:00:00.000Z
sC_F 25
sC_C 31
From the above df I would like to convert that as a dictionary of dictionary as shown below.
dict:
{'teachers': 49,
'students': 289,
'R': 3.7,
'holidays': 165,
'OS':18,
'Em': {'from': '2020-02-29T20:00:00.000Z', 'to': '2020-03-20T20:00:00.000Z',
'F': 3, 'C': 2},
'sC': {'from': '2020-03-31T20:00:00.000Z', 'to': '2020-05-29T20:00:00.000Z',
'F': 25, 'C': 31}}
Use:
s = df['Params'].str.split('_')
m = s.str.len().eq(1)
d1 = df[m].set_index('Params')['Value'].to_dict()
d2 = df[~m].assign(Params=s.str[-1]).agg(tuple, axis=1)\
.groupby(s.str[0]).agg(lambda s: dict(s.tolist())).to_dict()
dct = {**d1, **d2}
Result:
{'Em': {'C': '2',
'F': '3',
'from': '2020-02-29T20:00:00.000Z',
'to': '2020-03-20T20:00:00.000Z'},
'OS': '18',
'R': '3.7',
'holidays': '165',
'sC': {'C': '31',
'F': '25',
'from': '2020-03-31T20:00:00.000Z',
'to': '2020-05-29T20:00:00.000Z'},
'students': '289',
'teachers': '49'}
Please always try to provide the data in a reproducible way, more people will be able to attempt the question
Dataset
Params = ['teachers','students','R','holidays','OS','Em_from','Em_to','Em_F','Em_C','sC_from','sC_to','sC_F','sC_C']
Value = ['49','289','3.7','165','18','2020-02-29T20:00:00.000Z','2020-03-20T20:00:00.000Z','3','2','2020-03-31T20:00:00.000Z','2020-05-29T20:00:00.000Z','25','31']
df = pd.DataFrame(zip(Params,Value),columns=["col1","col2"])
you can do something like
d = {}
for lst in df.values:
for k,v in zip(lst[0:],lst[1:]):
if any(name in k for name in ('Em_from', 'sC_from')):d[k.split('_')[0]] = {k.split('_')[1]:v}
elif any(name in k for name in ('Em_to', 'Em_F','Em_C','sC_to','sC_F','sC_C')):d[k.split('_')[0]][k.split('_')[1]] = v
else:d[k] = v
Output
{'teachers': '49',
'students': '289',
'R': '3.7',
'holidays': '165',
'OS': '18',
'Em': {'from': '2020-02-29T20:00:00.000Z',
'to': '2020-03-20T20:00:00.000Z',
'F': '3',
'C': '2'},
'sC': {'from': '2020-03-31T20:00:00.000Z',
'to': '2020-05-29T20:00:00.000Z',
'F': '25',
'C': '31'}}
panda's dataframes have a to_json method (see docs)
There are multiple examples there, but the general flow goes like this, let's say you have a dataframe called df:
import json
import pandas as pd
parsed = df.to_json()
df_json = json.loads(json_df)
Read the docs to see more examples and different parameters you may have to fiddle with.
create a new dictionary(new_dict) with inverted key-value pairs from the original dictionary (my_dict)
def invert(dic_t):
new_dict={}
for key,value in dic_t.items():
new_dict[key]=value
new_dict[value]=key
return new_dict
my_dict = {
"A": "B",
"C": "D",
"E": "F"
}
print(invert(my_dict))
Output:
{'A': 'B', 'B': 'A', 'C': 'D', 'D': 'C', 'E': 'F', 'F': 'E'}
I am new to python so any help would be useful
What you are doing is correct, you just have to remove the line new_dict[key]=value as this represents the same old dictionary
def invert(dic_t):
new_dict={}
for key,value in dic_t.items():
# new_dict[key]=value
new_dict[value]=key
return new_dict
Also, you can use dictionary comprehension if you are looking for an one-line answer!
def invert(dict_t):
return {v:k for k, v in dict_t.items()}
Probelm statement:
response= [{'HotelId': 8, 'IsFast': False, 'Payload': {'HotelInstaceId': 8, 'IsResetNeeded': False, 'HotelType': 'test', 'Product': {'Family': 'Times', 'Model': 'Roman', 'Type': 'Visible', 'Vendor': 'Royal'}, 'Hotel': {'DisplayBrightness': 80, 'LedColor': None, 'Location': '', 'Name': 'testing'}}}]
I want to verify the value of name "testing".
My code:
result = response["Data"][0]["Payload"]["Name"]
if result == "testing":
print("pass")
else:
print("fail")
New code:
tests = [{'a': 1},
{'b': {'a': 2}},
{'a': 3, 'b': {'a': 4}},
{'a': {'b': 5}},
{'a': {'a': 6}}]
def recursively_find_dict_values(self, d, tests, key="HotelType"):
output = []
for i, v in d.items():
if i == key:
output.append(v)
if isinstance(v, dict):
output.extend(self.recursively_find_dict_values(v, key, tests))
return output
for t in tests:
recursively_find_dict_values(t, key="HotelType")
i will get the response from another function and that response i will send to "recursively_find_dict_values" func as a argument..
Example inputs:
Input keys Output
"Name" "testing"
"DisplayBrightness" 80
"HotelType" "test"
but i want to do in generic way like if i pass "Name" or "DisplayBrightness" or any key to the function it should return the value and also if keys in nested dictionary then also it should return the value.
How can i achieve this?
I think this may be able to help you:
def recursively_find_dict_values(d, key):
output = []
for i,v in d.items():
if i == key:
output.append(v)
if isinstance(v, dict):
output.extend(recursively_find_dict_values(v, key))
return output
tests = [{'a': 1},
{'b': {'a': 2}},
{'a': 3, 'b': {'a': 4}},
{'a': {'b': 5}},
{'a': {'a': 6}}]
for t in tests:
print(recursively_find_dict_values(t, 'a'))
Try it online!
I would check all the examples to see if the functionality is to your liking. Note that if the value of 'a' is a dict, it will add a dict to the list, and if the dict value of 'a' also has a key 'a', both levels will be added to the dict (see the fifth example). Also, even if there is only one match, it will still return a list.
In this example, d is the name of your input dict:
# Getting all the values of key in d:
values = recursively_find_dict_values(d, key)
# Checking if key has value v in d:
if v in recursively_find_dict_values(d):
print('Success!')
I have the code with recursion function:
def myPermutation (newString, newDict):
if sum(newDict.values()) == 0:
print(newDict)
return
else:
curDict = newDict
nextDict=newDict
for char in newString:
# print('from line 09 -> ', curDict)
# print('from line 10 -> ', char, curDict[char])
if curDict[char] == 0:
continue
else:
# print(char)
print(curDict)
nextDict[char] -= 1
print(nextDict)
myPermutation(newString, nextDict)
nextDict=curDict
return
newString = 'AB'
# newDict = curDict(newString)
newDict = {'A': 1, 'B': 1}
# print(newString, newDict)
test = myPermutation(newString, newDict)
# print(test)
My out put is this:
{'A': 1, 'B': 1}
{'A': 0, 'B': 1}
{'A': 0, 'B': 1}
{'A': 0, 'B': 0}
{'A': 0, 'B': 0}
It looks like my recursion function is not working correctly, I did some debug and found when the function tried to do second loop from top level, (move from 'A' to 'B' from 1st level of Tree), the dictionary changed from {'A':1, 'B':1} to {'A':0, 'B':0}. The expect output put should be something like:
{'A': 1, 'B': 1}
{'A': 0, 'B': 1}
{'A': 0, 'B': 1}
{'A': 0, 'B': 0}
{'A': 0, 'B': 0}
{'A': 1, 'B': 1}
{'A': 1, 'B': 0}
{'A': 1, 'B': 0}
{'A': 0, 'B': 0}
{'A': 0, 'B': 0}
The following code:
curDict = newDict
nextDict=newDict
creates two names that both point to the same object. If you change one, the other will change. Maybe you want a deep copy?
import copy
curDict = copy.deepcopy(newDict)
nextDict = copy.deepcopy(newDict)
This means you can now change the two curDict independently of nextDict.