python nested list and dicts, trouble accesing and setting - python
I am originally a c guy but recently I started doing some stuff in python.
The things that gives me trouble is the more advanced data structures in python.
I could do it all with multiple list like in c but that would just be boring right?
anyway here I have a data structure which is basically a list of dicts where the value field of the dict is another list of 2 key-value pairs:
clients = [
{'client1':[{'test':'testvalue','status':'statusvalue'}]},
{'client2':[{'test':'testvalue','status':'statusvalue'}]},
{'client3':[{'test':'testvalue','status':'statusvalue'}]}
]
now I want to be able to acces the testvalue and statusvalue fields and modify or read them. based on the position in the list.
in pseudocode it would be something like:
for i in range(0,clients):
getvalue(clients[i].'test')
setvalue(clients[i].'test')
getvalue(clients[i].'status')
setvalue(clients[i].'status')
in the end I want to use this data structure to render a html page with jinja2
For a start, in Python you should (almost) never iterate over range(len(something)). You iterate over something.
Secondly, your data structure is wrong. There's no point having a list of dicts, each dict containing a single key/value pair and each value consisting of a list with a single item. You should just have a dict of dicts: you can still iterate over it.
clients = {
'client1':{'test':'testvalue','status':'statusvalue'},
'client2':{'test':'testvalue','status':'statusvalue'},
'client3':{'test':'testvalue','status':'statusvalue'},
}
for key, value in clients.iteritems():
print value['test']
value['test'] = 'newvalue'
I have noticed that you put a dictionary inside a list as the value for each client.
I think you may wish to re-configure your data structure as such:
clients = [
{'client1':{'test':'testvalue','status':'statusvalue'}}
{'client2':{'test':'testvalue','status':'statusvalue'}}
{'client3':{'test':'testvalue','status':'statusvalue'}}
]
Therefore, you can begin iterating as such:
for client in clients:
for k, v in client.iteritems(): #this unpacks client into 'client' (k) and {'val'...} (v)
print v['test'] #this gets the value of test.
v['test'] = 'some_new_value' #this sets the value of test.
Related
Python- Insert new values into 'nested' list?
What I'm trying to do isn't a huge problem in php, but I can't find much assistance for Python. In simple terms, from a list which produces output as follows: {"marketId":"1.130856098","totalAvailable":null,"isMarketDataDelayed":null,"lastMatchTime":null,"betDelay":0,"version":2576584033,"complete":true,"runnersVoidable":false,"totalMatched":null,"status":"OPEN","bspReconciled":false,"crossMatching":false,"inplay":false,"numberOfWinners":1,"numberOfRunners":10,"numberOfActiveRunners":8,"runners":[{"status":"ACTIVE","ex":{"tradedVolume":[],"availableToBack":[{"price":2.8,"size":34.16},{"price":2.76,"size":200},{"price":2.5,"size":237.85}],"availableToLay":[{"price":2.94,"size":6.03},{"price":2.96,"size":10.82},{"price":3,"size":33.45}]},"sp":{"nearPrice":null,"farPrice":null,"backStakeTaken":[],"layLiabilityTaken":[],"actualSP":null},"adjustmentFactor":null,"removalDate":null,"lastPriceTraded":null,"handicap":0,"totalMatched":null,"selectionId":12832765}... All I want to do is add in an extra field, containing the 'runner name' in the data set below, into each of the 'runners' sub lists from the initial data set, based on selection_id=selectionId. So initially I iterate through the full dataset, and then create a separate list to get the runner name from the runner id (I should point out that runnerId===selectionId===selection_id, no idea why there are multiple names are used), this works fine and the code is shown below: for market_book in market_books: market_catalogues = trading.betting.list_market_catalogue( market_projection=["RUNNER_DESCRIPTION", "RUNNER_METADATA", "COMPETITION", "EVENT", "EVENT_TYPE", "MARKET_DESCRIPTION", "MARKET_START_TIME"], filter=betfairlightweight.filters.market_filter( market_ids=[market_book.market_id], ), max_results=100) data = [] for market_catalogue in market_catalogues: for runner in market_catalogue.runners: data.append( (runner.selection_id, runner.runner_name) ) So as you can see I have the data in data[], but what I need to do is add it to the initial data set, based on the selection_id. I'm more comfortable with Php or Javascript, so apologies if this seems a bit simplistic, but the code snippets I've found on-line only seem to assist with very simple Python lists and nothing 'nested' (to me the structure seems similar to a nested array). As per the request below, here is the full list: {"marketId":"1.130856098","totalAvailable":null,"isMarketDataDelayed":null,"lastMatchTime":null,"betDelay":0,"version":2576584033,"complete":true,"runnersVoidable":false,"totalMatched":null,"status":"OPEN","bspReconciled":false,"crossMatching":false,"inplay":false,"numberOfWinners":1,"numberOfRunners":10,"numberOfActiveRunners":8,"runners":[{"status":"ACTIVE","ex":{"tradedVolume":[],"availableToBack":[{"price":2.8,"size":34.16},{"price":2.76,"size":200},{"price":2.5,"size":237.85}],"availableToLay":[{"price":2.94,"size":6.03},{"price":2.96,"size":10.82},{"price":3,"size":33.45}]},"sp":{"nearPrice":null,"farPrice":null,"backStakeTaken":[],"layLiabilityTaken":[],"actualSP":null},"adjustmentFactor":null,"removalDate":null,"lastPriceTraded":null,"handicap":0,"totalMatched":null,"selectionId":12832765},{"status":"ACTIVE","ex":{"tradedVolume":[],"availableToBack":[{"price":20,"size":3},{"price":19.5,"size":26.36},{"price":19,"size":2}],"availableToLay":[{"price":21,"size":13},{"price":22,"size":2},{"price":23,"size":2}]},"sp":{"nearPrice":null,"farPrice":null,"backStakeTaken":[],"layLiabilityTaken":[],"actualSP":null},"adjustmentFactor":null,"removalDate":null,"lastPriceTraded":null,"handicap":0,"totalMatched":null,"selectionId":12832767},{"status":"ACTIVE","ex":{"tradedVolume":[],"availableToBack":[{"price":11,"size":9.75},{"price":10.5,"size":3},{"price":10,"size":28.18}],"availableToLay":[{"price":11.5,"size":12},{"price":13.5,"size":2},{"price":14,"size":7.75}]},"sp":{"nearPrice":null,"farPrice":null,"backStakeTaken":[],"layLiabilityTaken":[],"actualSP":null},"adjustmentFactor":null,"removalDate":null,"lastPriceTraded":null,"handicap":0,"totalMatched":null,"selectionId":12832766},{"status":"ACTIVE","ex":{"tradedVolume":[],"availableToBack":[{"price":48,"size":2},{"price":46,"size":5},{"price":42,"size":5}],"availableToLay":[{"price":60,"size":7},{"price":70,"size":5},{"price":75,"size":10}]},"sp":{"nearPrice":null,"farPrice":null,"backStakeTaken":[],"layLiabilityTaken":[],"actualSP":null},"adjustmentFactor":null,"removalDate":null,"lastPriceTraded":null,"handicap":0,"totalMatched":null,"selectionId":12832769},{"status":"ACTIVE","ex":{"tradedVolume":[],"availableToBack":[{"price":18.5,"size":28.94},{"price":18,"size":5},{"price":17.5,"size":3}],"availableToLay":[{"price":21,"size":20},{"price":23,"size":2},{"price":24,"size":2}]},"sp":{"nearPrice":null,"farPrice":null,"backStakeTaken":[],"layLiabilityTaken":[],"actualSP":null},"adjustmentFactor":null,"removalDate":null,"lastPriceTraded":null,"handicap":0,"totalMatched":null,"selectionId":12832768},{"status":"ACTIVE","ex":{"tradedVolume":[],"availableToBack":[{"price":4.3,"size":9},{"price":4.2,"size":257.98},{"price":4.1,"size":51.1}],"availableToLay":[{"price":4.4,"size":20.97},{"price":4.5,"size":30},{"price":4.6,"size":16}]},"sp":{"nearPrice":null,"farPrice":null,"backStakeTaken":[],"layLiabilityTaken":[],"actualSP":null},"adjustmentFactor":null,"removalDate":null,"lastPriceTraded":null,"handicap":0,"totalMatched":null,"selectionId":12832771},{"status":"ACTIVE","ex":{"tradedVolume":[],"availableToBack":[{"price":24,"size":6.75},{"price":23,"size":2},{"price":22,"size":2}],"availableToLay":[{"price":26,"size":2},{"price":27,"size":2},{"price":28,"size":2}]},"sp":{"nearPrice":null,"farPrice":null,"backStakeTaken":[],"layLiabilityTaken":[],"actualSP":null},"adjustmentFactor":null,"removalDate":null,"lastPriceTraded":null,"handicap":0,"totalMatched":null,"selectionId":12832770},{"status":"ACTIVE","ex":{"tradedVolume":[],"availableToBack":[{"price":5.7,"size":149.33},{"price":5.5,"size":29.41},{"price":5.4,"size":5}],"availableToLay":[{"price":6,"size":85},{"price":6.6,"size":5},{"price":6.8,"size":5}]},"sp":{"nearPrice":null,"farPrice":null,"backStakeTaken":[],"layLiabilityTaken":[],"actualSP":null},"adjustmentFactor":null,"removalDate":null,"lastPriceTraded":null,"handicap":0,"totalMatched":null,"selectionId":10064909}],"publishTime":1551612312125,"priceLadderDefinition":{"type":"CLASSIC"},"keyLineDescription":null,"marketDefinition":{"bspMarket":false,"turnInPlayEnabled":false,"persistenceEnabled":false,"marketBaseRate":5,"eventId":"28180290","eventTypeId":"2378961","numberOfWinners":1,"bettingType":"ODDS","marketType":"NONSPORT","marketTime":"2019-03-29T00:00:00.000Z","suspendTime":"2019-03-29T00:00:00.000Z","bspReconciled":false,"complete":true,"inPlay":false,"crossMatching":false,"runnersVoidable":false,"numberOfActiveRunners":8,"betDelay":0,"status":"OPEN","runners":[{"status":"ACTIVE","sortPriority":1,"id":10064909},{"status":"ACTIVE","sortPriority":2,"id":12832765},{"status":"ACTIVE","sortPriority":3,"id":12832766},{"status":"ACTIVE","sortPriority":4,"id":12832767},{"status":"ACTIVE","sortPriority":5,"id":12832768},{"status":"ACTIVE","sortPriority":6,"id":12832770},{"status":"ACTIVE","sortPriority":7,"id":12832769},{"status":"ACTIVE","sortPriority":8,"id":12832771},{"status":"LOSER","sortPriority":9,"id":10317013},{"status":"LOSER","sortPriority":10,"id":10317010}],"regulators":["MR_INT"],"countryCode":"GB","discountAllowed":true,"timezone":"Europe\/London","openDate":"2019-03-29T00:00:00.000Z","version":2576584033,"priceLadderDefinition":{"type":"CLASSIC"}}}
i think i understand what you are trying to do now first hold your data as a python object (you gave us a json object) import json my_data = json.loads(my_json_string) for item in my_data['runners']: item['selectionId'] = [item['selectionId'], my_name_here] the thing is that my_data['runners'][i]['selectionId'] is a string, unless you want to concat the name and the id together, you should turn it into a list or even a dictionary each item is a dicitonary so you can always also a new keys to it item['new_key'] = my_value
So, essentially this works...with one exception...I can see from the print(...) in the loop that the attribute is updated, however what I can't seem to do is then see this update outside the loop. mkt_runners = [] for market_catalogue in market_catalogues: for r in market_catalogue.runners: mkt_runners.append((r.selection_id, r.runner_name)) for market_book in market_books: for runner in market_book.runners: for x in mkt_runners: if runner.selection_id in x: setattr(runner, 'x', x[1]) print(market_book.market_id, runner.x, runner.selection_id) print(market_book.json()) So the print(market_book.market_id.... displays as expected, but when I print the whole list it shows the un-updated version. I can't seem to find an obvious solution, which is odd, as it seems like a really simple thing (I tried messing around with indents, in case that was the problem, but it doesn't seem to be, its like its not refreshing the market_book list post update of the runners sub list)!
returning results from multiple dictionaries in python
I'm learning Python (3.6) to work with API data returned in JSON (which is also new to me). The API call returns 26 files which comprise the entirety of our data set - Page 1 of 26, Page 2 of 26 etc. I'm able to iterate through a single dictionary to get the required parsed results but need to return results from all dictionaries. Tried merging them using suggestions from here, but unsuccessfully. The following code only returns results from the last dictionary: mkt = {**mkt1, **mkt2} The parsing script is: for prop in mkt["properties"]: for space in mkt["properties"][0 - len(mkt)]["spaces"]: try: if prop["name"][1:8] == "TESTING": del prop else: print(prop["name"][0:5], prop["name"][6:], space["floor"]["name"], space["suite"]) except: pass Any help would be appreciated!
Thanks johnashu! Your solution worked for me! To clarify for any people just starting out, we created a list of dictionaries like so: mkt = [mkt1, mkt2, mkt3...mkt26] Once we had the list of dictionaries, the code block was wrapped in code to iterate through all dictionaries, as shown by johnashu. The last statement was updated to reference the list of dictionaries: for dictionary in mkt: iter_dict(dictionary)
Assuming you have all dictionaries in a list of other data structure and that the parsing code works for a single dictionary. You can wrap the code in a function and then call the function using a for loop for each dictionary. I should help to make your code a little more readable. def iter_dict(mkt): """ Takes a dictionary as an argument for parsing information """ for prop in mkt["properties"]: for space in mkt["properties"][0 - len(mkt)]["spaces"]: try: if prop["name"][1:8] == "TESTING": del prop else: print(prop["name"][0:5], prop["name"][6:], space["floor"]["name"], space["suite"]) except: pass for dictionary in dict_list: iter_dict(dictionary)
manipulating the contents of dictionaries inside dictionaries
I am receiving data from a radar on different contacts. each contact has a lat, lon, direction, range and time stamp. and each time hit on a contact will be ID'd such as 1,2,3 etc. for one contact this suggests a dictionary over time. therefore, my dictionary for one contact will look something like this: {1:[data # t1], 2:[data # t2], 3:[data # t3]} And as time goes on the dictionary will fill up until ...But there will not be only one contact. there will be several, maybe many. this suggests a dictionary of dictionaries: {'SSHornblower': {1:[data], 2:[data], 3:[data]}, 'Lustania': {1:[], 2:[], 3:[]}, 'Queen Mary': {1:[], 2:[], 3:[], 4:[]}} It is not possible to know before hand how many contacts my radar will find, maybe 3 maybe 300. I cannot come up with names ahead of time for all the possible contacts and names for all the possible dictionaries. Therefore, I came up with the idea that once i nested a dictionary inside the larger dictionary, i could clear it and start over with the new contact. but when i do a clear after i nest one inside another, it clears everything inside the larger dictionary! Is there a way to get around this?
For filling up nested dictionaries a defaultdict can be very useful. Let's assume you have a function radar() that returns three values: contact_name contact_id contact_data Then the following would do the job: from collections import defaultdict store = defaultdict(dict) while True: contact_name, contact_id, contact_data = radar() store[contact_name][contact_id] = contact_data So even if there will be a new contact_name that is not yet present in the store, the magic of defaultdict will make sure that an empty nested dict will already be there when you access the store with the new key . Therefore store[new_contact_name][new_contact_id] = new_contact_data will work.
Parsing JSON in Python (Reverse dictionary search)
I'm using Python and "requests" to practice the use of API. I've had success with basic requests and parsing, but having difficulty with list comprehension for a more complex project. I requested from a server and got a dictionary. From there, I used: participant_search = (match1_request['participantIdentities']) To convert the values of the participantIdentities key to get the following data: [{'player': {'summonerName': 'Crescent Bladex', 'matchHistoryUri': '/v1/stats/player_history/NA1/226413119', 'summonerId': 63523774, 'profileIcon': 870}, 'participantId': 1}, My goal here is to combine the summonerId and participantId to one list. Which is easy normally, but the order of ParticipantIdentities is randomized. So the player I want information on will sometimes be 1st on the list, and other times third. So I can't use the var = list[0] like how I would normally do. I have access to summonerId, so I'm thinking I can search the list the summonerId, then somehow collect all the information around it. For instance, if I knew 63523774 then I could find the key for it. From here, is it possible to find the parent list of the key? Any guidance would be appreciated. Edit (Clarification): Here's the data I'm working with: http://pastebin.com/spHk8VP0 At line 1691 is where participant the nested dictionary 'participantIdentities' is. From here, there are 10 dictionaries. These 10 dictionaries include two nested dictionaries, "player" and "participantId". My goal is to search these 10 dictionaries for the one dictionary that has the summonerId. The summonerId is something I already know before I make this request to the server. So I'm looking for some sort of "search" method, that goes beyond "true/false". A search method that, if a value is found within an object, the entire dictionary (key:value) is given.
Not sure if I properly understood you, but would this work? for i in range(len(match1_request['participantIdentities'])): if(match1_request['participantIdentities'][i]['summonerid'] == '63523774': # do whatever you want with it. i becomes the index you're searching for.
ds = match1_request['participantIdentities'] result_ = [d for d in ds if d["player"]["summonerId"] == 12345] result = result_[0] if result_ else {} See if it works for you.
You can use a dict comprehension to build a dict wich uses summonerIds as keys: players_list = response['participantIdentities'] {p['player']['summonerId']: p['participantId'] for p in players_list}
I think what you are asking for is: "How do I get the stats for a given a summoner?" You'll need a mapping of participantId to summonerId. For example, would it be helpful to know this? summoner[1] = 63523774 summoner[2] = 44610089 ... If so, then: # This is probably what you are asking for: summoner = {ident['participantId']: ident['player']['summonerId'] for ident in match1_request['participantIdentities']} # Then you can do this: summoner_stats = {summoner[p['participantId']]: p['stats'] for p in match1_request['participants']} # And to lookup a particular summoner's stats: print summoner_stats[44610089] (ref: raw data you pasted)
How to get inner content of data in dictionary python
i'm new with python and can't get the inner content of the the dictionary. i have a "detail" as a dictionary and i would like to pull data from "properties" so i will have "time" and "distinct_id" as keys Right now the keys in the dictionary are "event" and "properties" and would like to go deeper in the dictionary example of dictionary "details": {"event":"User Profile - Page View","properties":{"time":1428969601,"distinct_id":"14cb3d99b195-0feaf7b1c-5c267370-c0000-14cb3d99b1a220"}} Thanks!
Each dictionary (instance of dict) comes with a method __getitem__(self, index), for which [] is a syntactic suggar. In order to access an unnested dictionary, you would normally use details['event']. Since nested dictionary is just another dictionary, the same applies further: details['properties']['time'] will return the 'time' of nested dictionary. If you want the code to be a bit nicer, you can assign the first dictionary to a variable properties = details['properties'] and access the attributes as you would in normal (not nested) dictionary print properties['time'] print properties['distinct_id']
dict = {"event":"User Profile - Page View","properties":{"time":1428969601,"distinct_id":"14cb3d99b195-0feaf7b1c-5c267370-c0000-14cb3d99b1a220"}} dict['properties']['time']