I have been trying to solve this problem but I cannot get it right :( I am querying mySQL database in Django, and my results comes back as a tuple inside of an array. Please see dummy data below;
query_results = [
(500, datetime.date(2017, 1, 1)),
(302, datetime.date(2017, 1, 2)),
(550, datetime.date(2017, 1, 3)),
(180, datetime.date(2017, 1, 4)),
(645, datetime.date(2017, 1, 5)),
(465, datetime.date(2017, 1,6))
]
500, 302, 550 etc... are my records. The next element are the date of those records. I would like to append them into a dictionary using a loop, but I always get an error of:
TypeError: list indices must be integers, not tuple.
Can anyone please give me some advise on how to append these into something like this:
myDict = {
"record": "",
"date": ""
}
All help is greatly appreciated!
You could unpack the list of tuples and zip them into a dict with the keys you want,
dict(zip(['record', 'date'], zip(*query_results)))
which outputs
{'date': (datetime.date(2017, 1, 1),
datetime.date(2017, 1, 2),
datetime.date(2017, 1, 3),
datetime.date(2017, 1, 4),
datetime.date(2017, 1, 5),
datetime.date(2017, 1, 6)),
'record': (500, 302, 550, 180, 645, 465)}
Update
If you're hoping for a list of dicts, you could use a list comprehension directly.
[dict(record=record, date=date) for record, date in query_results]
which outputs
[{'date': datetime.date(2017, 1, 1), 'record': 500},
{'date': datetime.date(2017, 1, 2), 'record': 302},
{'date': datetime.date(2017, 1, 3), 'record': 550},
{'date': datetime.date(2017, 1, 4), 'record': 180},
{'date': datetime.date(2017, 1, 5), 'record': 645},
{'date': datetime.date(2017, 1, 6), 'record': 465}]
Alternative format to Mitch's answer, as the post was a bit ambiguous on wanted result:
>list_of_dicts = [ {'result': tup[0], 'date': tup[1]} for tup in query_results]
>list_of_dicts[0]
{'result':500, 'date':datetime.date(2017, 1, 1) }
Building on other disucssion if you would rather have each record key to a date time you can instead build a dictionary of each record as an entry. This assumes that the records will never be duplicates. In which case the comprehension would look like this:
my_dict = {str(record) : date for record, date in query_results}
where the dict looks like:
{"500": date1, "501", date2} #but not necessarily in order
This loop should do:
my_dict = {}
for item in query_results:
my_dict[item[0]] = item[1]
This will stick your records (500, 550, etc.) as dictionary keys (item[0] part) so that you can easily call them out. And the value associated with those keys will be the datetime object (item[1] part).
If you want a more complicated solution please let us know.
Tom
If you want them as a tuple inside the dictionary, Mitch's answer would work.
If you want a separate dictionary in each loop, you can do:
for result in query_results:
myDict = {}
myDict["record"] = result[0]
myDict["date"] = result[1]
your_operation()
Or if you want the record as the key, and the date as the value (unless you have repeating records, this could be very useful) you can do:
myDict = {}
for result in query_results:
myDict[result[0]] = result[1]
Then you would have:
myDict = {"500": datetime.date(2017, 1, 1),
"302": datetime.date(2017, 1, 2),
etc..}
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.