Return Collections Counter Result as JSON with Python -


i not super familiar python i'm working on script needs return json object python php. problem i'm having use of collections counter , not not being valid json encode (actually won't compile, makes sense).

i getting 1 error: valueerror: keys must string

here snippet of code:

# make sure items in set unique items = collections.counter(tuple(item) item in all_items)  # print json response print json.dumps({ 'items': items, 'position': [ravg, gavg, bavg] }) 

this counter looks like:

counter({(11, 11, 15): 8452, (151, 131, 153): 7336, (26, 29, 35): 7324, (83, 81, 100): 5080, (113, 106, 126): 5012, (54, 56, 61): 4627, (193, 193, 194): 3783, (13, 124, 157): 822}) 

json allows keys strings, , tuples. try:

items = collections.counter(str(tuple(item)) item in all_items) 

or maybe

items = collections.counter(str(item) item in all_items) 

(depending on how you'd them formatted in json)


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -