我有一個帶有配置的 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"],一個 json 物件,john每個jane將被創建 2 次。
對于一個模式[3,"john","jane"],一個 json 物件,john每個jane將被創建 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。有人可以幫忙嗎?
uj5u.com熱心網友回復:
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}")
uj5u.com熱心網友回復:
我能夠通過使用來修復它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}")
uj5u.com熱心網友回復:
為了避免必須復制的問題,將模板移動到 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/465164.html
上一篇:Scapy中的Sendp函式
