Python for循环的意外输出



我有一个JSON文件配置schedules.json,其中包含有关模式和一对用户的信息:

{
"pattern": [
[2, "john", "jane"],
[2, "sam", "bob"],
[3, "john", "jane"],
[2, "sam", "bob"],
[2, "john", "jane"],
[3, "sam", "bob"]
]
}

这个想法是,对于在第一个索引中找到的数字,将为该列表中的每个用户创建一个json对象,用于在该数字中找到的次数。例如:
模式[2,"john","jane"],johnjane的json对象将分别创建2次。
对于模式[3,"john","jane"],johnjane的json对象将分别创建3次等…

python代码如下:

shift_rota = []
schedule_layers_template = {
"morning_user": "",
"night_user": ""
}
# Load the schedules config file
with open('config/schedules.json') as f:

schedules = json.load(f)

for pattern in schedules['pattern']:

# Get the number of occurrences
occurence = int(pattern[0])

# Remove occurence from list
pattern.pop(0)

morning_shift_user = pattern[0]
night_shift_user = pattern[1]

for _ in range(0, occurence):

my_template = schedule_layers_template

my_template['morning_user'] = morning_shift_user

# Append schedule layer to list
shift_rota.append(my_template)

my_template['night_user'] = night_shift_user

# Append schedule layer to list
shift_rota.append(my_template)

print(f"Final shift rota {shift_rota}")

这里的问题是最终输出返回的是:

Final shift rota [{'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}, {'morning_user': 'sam', 'night_user': 'bob'}]

由于某些原因,最终输出重复用户sam和bob。有人能帮帮忙吗?

修改后的PFB代码运行正常

在这里,我们应该创建schedule_layers_template的副本,然后将其分配给my_template。

import json

shift_rota = []
schedule_layers_template = {
"morning_user": "",
"night_user": ""
}
# Load the schedules config file
with open('schedules.json') as f:
schedules = json.load(f)
for pattern in schedules['pattern']:
# Get the number of occurrences
occurence = int(pattern[0])
# Remove occurence from list
pattern.pop(0)
morning_shift_user = pattern[0]
night_shift_user = pattern[1]
for _ in range(0, occurence):
my_template = schedule_layers_template.copy()
my_template['morning_user'] = morning_shift_user
my_template['night_user'] = night_shift_user
# Append schedule layer to list
shift_rota.append(my_template)
print(f"Final shift rota {shift_rota}")

我能够通过使用deepcopy来修复它。示例代码如下:

import copy
...
with open('schedules.json') as f:
schedules = json.load(f)
for pattern in schedules['pattern']:
# Get the number of occurrences
occurence = int(pattern[0])
# Remove occurence from list
pattern.pop(0)
morning_shift_user = pattern[0]
night_shift_user = pattern[1]
for _ in range(0, occurence):
my_template = copy.deepcopy(schedule_layers_template)
my_template['morning_user'] = morning_shift_user
my_template['night_user'] = night_shift_user
# Append schedule layer to list
shift_rota.append(my_template)
print(f"Final shift rota {shift_rota}")

为了避免必须复制的问题,将模板移到for循环中并运行

会更容易一些。
shift_rota = []
with open('schedules.json') as f:

schedules = json.load(f)

for pattern in schedules['pattern']:

# Get the number of occurrences
occurence = int(pattern[0])

for _ in range(0,occurence):

schedule_layers_template = {
"morning_user": pattern[1],
"night_user": pattern[2]
}
shift_rota.append(schedule_layers_template)



pprint(shift_rota)

最新更新