python 3.x - Assigning int values to each dictionary entry as well as having reference numbers? -
i'm trying create dictionary in python 3 holds 12 different names, first 7 must have value of 8.50, last 5 13.50 possible assign them int values? have tried find answers through google etc either i'm not wording questions right or it's not possible. @ moment i'm using:
pizza_dict = {"0: basic cheese $8.50", "1: pepperoni $8.50"}
but can not find way give them int values if possible?
use {}.fromkeys():
>>> {}.fromkeys(['1','2','3'],8.5) {'1': 8.5, '3': 8.5, '2': 8.5}
for example:
>>> mydict={} >>> mydict.update({}.fromkeys(['1','2','3'],8.5)) >>> mydict {'1': 8.5, '3': 8.5, '2': 8.5} >>> mydict.update({}.fromkeys(['4', '5', '6'],13.5)) >>> mydict {'1': 8.5, '3': 8.5, '2': 8.5, '5': 13.5, '4': 13.5, '6': 13.5}
just don't use mutable argument (like list) because same 1 each key.
surprising things can happen mutable:
>>> di={}.fromkeys([1,2,3],[]) >>> di[1].append('what?') >>> di {1: ['what?'], 2: ['what?'], 3: ['what?']}
Comments
Post a Comment