[英]Append tuples in dictionary
In the below part, I want to append in self.data
dictionary more key, value tuples
.在下面的部分中,我想在
self.data
字典中附加更多key, value tuples
。
Right now, when a new tuple
comes it goes on top of the current tuple
and I am always left with the most recent one, while i want to keep all of them.现在,当一个
tuple
出现时,它会位于当前tuple
之上,而我总是留下最新的tuple
,而我想保留所有这些元组。
Any suggestions?有什么建议?
from datetime import date
from collections import defaultdict
class Budget(object):
# todo : automatic date pickup
def __init__(self, amount):
self.amount = amount
self.data = defaultdict(list)
self.budget = None
self.rem_days = None
self.payment_dates = ['2019-01-23','2019-02-22','2019-03-22','2019-04-23','2019-05-23','2019-06-22',
'2019-07-23','2019-08-23','2019-09-23','2019-10-23','2019-11-22','2019-12-23',]
def calc_days(self):
cur_day = date.today().day
cur_month = date.today().month
ext_day = int(self.payment_dates[cur_month-1][8:10])
self.rem_days = ext_day - cur_day+1
print("Remaining days: ", self.rem_days)
def daily_budget(self):
self.budget = int(self.amount)/self.rem_days
print("Daily budget: ", self.budget)
def history(self):
# todo: fill in the dictionary with multiple tuples
imera = date.today()
cur_month = date.today().month
items = [self.rem_days, self.budget]
self.data[str(imera)].extend(items)
print (self.data)
inp = input("Insert account balance: ")
while inp != "s":
budget = Budget(inp)
budget.calc_days()
budget.daily_budget()
budget.history()
inp = input("Insert account balance or / to stop:")
output:输出:
Insert account balance: 54
Remaining days: 18
Daily budget: 3.0
defaultdict(<class 'list'>, {'2019-04-06': [18, 3.0]})
Insert account balance or / to stop: 54
Remaining days: 18
Daily budget: 3.0
defaultdict(<class 'list'>, {'2019-04-06': [18, 3.0]})
Insert account balance or / to stop:
You could use defaultdict
from collections
module,您可以使用
collections
模块中的defaultdict
,
Sample example:示例:
>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> d
defaultdict(<class 'list'>, {})
>>> d['foo'].append('bar')
>>> d['foo'].append('baz')
>>> d
defaultdict(<class 'list'>, {'foo': ['bar', 'baz']})
>>> d['foo']
['bar', 'baz']
>>> data = defaultdict(list)
>>> data["cur_day"].extend(['foo', 'bar'])
>>> data
defaultdict(<class 'list'>, {'cur_day': ['foo', 'bar']})
Code fix:代码修复:
from collections import defaultdict
class SomeClass(Object): # python2 ?
def __init__(self, amount):
self.amount = amount
self.data = defaultdict(list) # defaultdict for storing `data`
self.budget = None
self.rem_days = None
self.payment_dates = ['2019-01-23','2019-02-22','2019-03-22','2019-04-
23','2019-05-23','2019-06-22',
'2019-07-23','2019-08-23','2019-09-23','2019-10-
23','2019-11-22','2019-12-23',]
def history(self):
# todo: fill in the dictionary with multiple tuples
cur_day = date.today().day
items = (self.rem_days, self.budget) # made the items tuple
self.data['cur_day'].append(items) # appending data to the `list` in `cur_day` of the `dict` `data`
#self.data.update([("cur_day", items)])
print (self.data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.