简体   繁体   中英

I'm trying to figure out how to add to a list of dictionaries, rather than create a list of lists of dictionaries

I have a function, GetFunds, that returns a dictionary. It looks like this:

[ { "currency": "BTC", "available": 0.460594115839, "reserved": 0.0 } ]

to me this looks like a list with one dictionary in it. I would like to put more dictionaries in the list. So I try this code:

funds=GetFunds(1, "BTC")
funds=funds+(GetFunds(2, "BTC"))
funds=funds + GetFunds(1, "DIVI")
funds=funds + GetFunds(2, "DIVI")
print(funds)

and the result is a list of lists (I think)

[ { "currency": "BTC", "available": 0.460594115839, "reserved": 0.0 } ] [ { "currency": "BTC", "available": 0.460594115839, "reserved": 0.0 } ][ { "currency": "BTC", "available": 2.002708880342, "reserved": 0.449841884826 } ][ { "currency": "DIVI", "available": 6966346.17416024, "reserved": 0.0 } ][ { "currency": "DIVI", "available": 6285691.0243108, "reserved": 795457.15508981 } ]

But I think I want a single list with 4 elements, each of which is a dictionary....a single set of square braces

I have tried simply using '+', I have tried .append, btu I guess thats just for strings

Shown above

I want a single list of multiple dictionaries.

It's because there strings, so use json.loads :

import json
funds=json.loads(GetFunds(1, "BTC"))
funds=funds + json.loads(GetFunds(2, "BTC"))
funds=funds + json.loads(GetFunds(1, "DIVI"))
funds=funds + json.loads(GetFunds(2, "DIVI"))
print(funds)

Or little less recommended, using ast.literal_eval :

import ast
funds=ast.literal_eval(GetFunds(1, "BTC"))
funds=funds + ast.literal_eval(GetFunds(2, "BTC"))
funds=funds + ast.literal_eval(GetFunds(1, "DIVI"))
funds=funds + ast.literal_eval(GetFunds(2, "DIVI"))
print(funds)

If the result is a list it should work:

a = {'a' : 1, 'b' : 2}
b = {'c' : 3, 'd' : 4}
aa = [a]
aa.extend([b])
aa

result:

 [{'b': 2, 'a': 1}, {'c': 3, 'd': 4}]

So:

funds=GetFunds(1, "BTC")
funds.extend((GetFunds(2, "BTC")))

should work.

Otherwise check if it is effectively a list (as it seems to be).

type(aa)
<class 'list'>

I don't have enough reputation to post a comment, so I'm posting an answer instead.

It's obvious that GetFunds returns a string by looking at the second snippet you posted. You're right, when you're looking at something that looks like [{key: value}] my first impulse would also be "this is a list with one dictionary". However, in your second snippet you have something like [{key: value}][{key: value}]. This is obviously not a list of dictionaries or a list of lists of dictionaries. A list of dictionaries would look like [{k: v}, {k: v}], and a list of lists of dictionaries would look like [[{k: v}], [{k: v}]]. That tells me that when you thought you were appending elements to a list, you were actually concatenating two strings (which happened to have characters in them that made it look like it was a list, most likely to be used with JSON). Notice how the open and closing square brackets are back to back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM