Python 通过多维字典循环并计算
这肯定有问题,但我找不到 我有一本这样的字典:Python 通过多维字典循环并计算,python,for-loop,Python,For Loop,这肯定有问题,但我找不到 我有一本这样的字典: data = { "Jan": { "2017-01-01 00:00:00": { "001": 10, "002": 20, "003": 30 }, "2017-01-01 01:00:00": { "001": 20, "002": 40, "
data = {
"Jan": {
"2017-01-01 00:00:00": {
"001": 10,
"002": 20,
"003": 30
},
"2017-01-01 01:00:00": {
"001": 20,
"002": 40,
"003": 50
},
"2017-01-01 02:00:00": {
"001": 90,
"002": 50,
"003": 60
}
}
}
我想循环浏览字典并计算累积点,如果可能的话修改字典。例如,对于001,它将是
data["Jan"]["2017-01-01 00:00:00"]["001"] == 10
data["Jan"]["2017-01-01 01:00:00"]["001"] == 30
data["Jan"]["2017-01-01 02:00:00"]["001"] == 120
我不想得到最终的累积和,我想要相对的
现在我有这个代码:
import copy
from datetime import datetime, timedelta
copydata = copy.deepcopy(data)
# I made a copy because I have an if statement and Python was
complaining that the dictionary changed size during iteration
for i, month in enumerate(copydata):
for dt in copydata[month]:
for user in copydata[month][dt]:
current_datetime = datetime.strptime(dt, '%Y-%m-%d %H:00:00')
cumulativepoints=data[month][dt][user] # getting the current hour's points. Since the loop is in random order, it can start at any time
if current_datetime.hour > 0: # if the hour is 0 then it's the first hour and I don't need to calculate anything
for x in range(1, current_datetime.hour+1): # starting at 01:00:00 till the current_datetime.hour plus one to count itself
past_time = current_datetime - timedelta(hours=x)
past_time = past_time.strftime('%Y-%m-%d %H:00:00')
if data[month][past_time]:
cumulativepoints += data[month][past_time][user]
data[month][past_time][user] = cumulativepoints # <--- the error happens here
但在data[month][pass_time][user]=cumulativepoints这一行,Python抛出了一个错误:TypeError:list索引必须是整数,而不是str
我很确定这段代码比它应该的复杂得多。但这是由于许多错误消息导致的许多调整的结果
问题:通过多维字典循环并计算
您可以这样做,例如:
def pp_dict():
for month in data:
print('month:{}'.format(month))
for dt in sorted(data[month]):
print('\tdt:{}'.format(dt))
for user in sorted(data[month][dt]):
print('\t\tuser:{}:{}'.format(user, data[month][dt][user]))
def cum_sum(month, user):
cum_sum = 0
for dt in sorted(data[month]):
cum_sum += data[month][dt][user]
data[month][dt][user] = cum_sum
for user in ['001']:
cum_sum('Jan', user)
pp_dict()
输出:
通过Python:3.4.2测试,dict不是一个好的数据结构,它不是有序的,您将以随机顺序迭代键。但是顺序对于您试图完成的任务至关重要…@rlcabral,我在Python2.7.3中没有收到任何类型的错误。假设存在问题,尝试将变量cumulativepoints封装在int函数中。同样,对上面的数据行[月份][过去时间][用户]执行相同的操作。我感觉你不小心传递了一个字符串而不是一个整数。@SirJames,同样error@juanpa.arrivillaga,什么是更好的数据结构?我有一个CSV文件中的完整数据,所以我可以很容易地再次读取它,创建一个不同的数据结构。好吧,因为顺序很重要,某种嵌套列表,可能是一个目录列表或列表列表。如果你想玩得开心,你可以用namedtuple来记录。你的方法的问题是它只为一个用户计算。我需要在每个用户上循环调用函数的所有数据,它肯定会多次使用相同的参数调用函数。但就目前而言,我认为您的答案必须适用于我。@rlcabral:为['001']中的用户添加尽可能多的用户到数组中,例如为['001',002']中的用户添加尽可能多的用户:等等。
month:Jan
dt:2017-01-01 00:00:00
user:001:10
user:002:20
user:003:30
dt:2017-01-01 01:00:00
user:001:30
user:002:40
user:003:50
dt:2017-01-01 02:00:00
user:001:120
user:002:50
user:003:60