在字典中附加元组

时间:2019-04-06 15:18:53

标签: python dictionary tuples

在下面的部分中,我想在self.data词典中附加更多键,值tuples

现在,当一个新的tuple出现在当前tuple的顶部时,我总是留下最新的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: ,而我想保留所有这些。

有什么建议吗?

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:

“)

输出:

for a, x in enumerate(allcity):
        x.to_csv('msd{}.csv'.format(a))

1 个答案:

答案 0 :(得分:1)

您可以使用defaultdict模块中的collections

示例:

>>> 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']})

代码修复:

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)