Python for 循环中的意外输出

Python unexpected output in for loop

我有一个带有配置 schedules.json 的 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)