import datetime as DT
import calendar
def getMonthRanges(startDate, endDate):
while startDate <= endDate:
year, month = startDate.year, startDate.month
weekday, day = calendar.monthrange(year, month)
end = min(DT.date(year, month, day), endDate)
yield (startDate, end)
startDate = end+DT.timedelta(days=1)
ranges = [map(str, interval)
for interval in getMonthRanges(DT.date(2011,9,11), DT.date(2013,4,24))]
print(ranges)
产量
[['2011-09-11', '2011-09-30'], ['2011-10-01', '2011-10-31'], ['2011-11-01', '2011-11-30'], ['2011-12-01', '2011-12-31'], ['2012-01-01', '2012-01-31'], ['2012-02-01', '2012-02-29'], ['2012-03-01', '2012-03-31'], ['2012-04-01', '2012-04-30'], ['2012-05-01', '2012-05-31'], ['2012-06-01', '2012-06-30'], ['2012-07-01', '2012-07-31'], ['2012-08-01', '2012-08-31'], ['2012-09-01', '2012-09-30'], ['2012-10-01', '2012-10-31'], ['2012-11-01', '2012-11-30'], ['2012-12-01', '2012-12-31'], ['2013-01-01', '2013-01-31'], ['2013-02-01', '2013-02-28'], ['2013-03-01', '2013-03-31'], ['2013-04-01', '2013-04-24']]
分享全局变量不会更新,但是我将其导入到正在更新的解释器中。
请帮我如何更新字典 d
import texttable
class Lc:
d={50:8,100:6,200:4,500:3,1000:2,2000:1}
he=['container_size_in_ltrs','qty']
def inventory(self,g):
d1=self.d
if d1!=g:
d1=g
self.d=g
l1=d1.keys()
l1.sort()
li=[]
ls=[]
for i in l1:
li=[]
li.append(i)
li.append(d1[i])
ls.append(li)
table=texttable.Texttable()
table.header(self.he)
table.add_rows(ls,header=False)
print table.draw()
def add_inv(self,k,v):
d=self.d
for i,j in d.items():
if k==i:
d[k]=d[k]+v
print d
return d
z=Lc()
g=z.add_inv(500,2)
print z.d
弑天下
相关分类