输出每次都重复第一个字典
Output is repeating the first dictionary every time
我应该从三个单独的输入接收重新格式化字典的输出。
当前输出:
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}
预期输出:
print(buildCoverageDictionary(paidList1)) ->
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}
print(buildCoverageDictionary(paidList2)) ->
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [1010, 2000]}, 'MJ': {'Paid':
[5, 6, 7]}, 'ZF': {'Paid': [2660, 500]}}
print(buildCoverageDictionary(paidList3)) -> {'SK': {'Paid': [200, 400]}}
两个问题:
- 如何编辑代码以输出预期的输出?
- 我如何编辑代码以不需要我单独重新格式化每个字典,而是创建一个函数,无论输入是什么,都可以正确地重新格式化字典?
我的代码如下:
paidList1 = [["JS", 200, 400, 500, 600], ["SK", 400, 1000, 1600]]
paidList2 = [["JS", 200, 400, 500, 600], ["SK", 1010, 2000], ["MJ", 5, 6, 7], ["ZF", 2660, 500]]
paidList3 = [["SK", 200, 400]]
def buildCoverageDictionary(paidList):
JS_dict = {'Paid': paidList1[0][1:5]}
SK_dict = {'Paid': paidList1[1][1:4]}
new_dict1 = {paidList1[0][0]: JS_dict, paidList1[1][0]: SK_dict}
return new_dict1
JS_dict = {'Paid': paidList2[0][1:5]}
SK_dict = {'Paid': paidList2[1][1:3]}
MJ_dict = {'Paid': paidList2[2][1:4]}
ZF_dict = {'Paid': paidList2[3][1:3]}
new_dict2 = {paidList2[0][0]: JS_dict, paidList2[1][0]: SK_dict, paidList2[2][0]: MJ_dict, paidList2[3][0]: ZF_dict}
return new_dict2
SK_dict = {'Paid': paidList3[0][1:3]}
ew_dict3 = {paidList3[0][0]: SK_dict}
return new_dict3
print(buildCoverageDictionary(paidList1))
print(buildCoverageDictionary(paidList2))
print(buildCoverageDictionary(paidList3))
正如@Barmar 提到的,return
结束函数,执行后什么也没有。您可以尝试类似下面的方法,并使其更灵活地适应输入的变化,例如通过循环 paidList
而不是对索引进行硬编码,而是使用 [1:]
从索引 1
翻译到最后一个
paidList1 = [["JS", 200, 400, 500, 600], ["SK", 400, 1000, 1600]]
paidList2 = [["JS", 200, 400, 500, 600], ["SK", 1010, 2000], ["MJ", 5, 6, 7], ["ZF", 2660, 500]]
paidList3 = [["SK", 200, 400]]
def buildCoverageDictionary(paidList):
output = {}
for item in paidList:
output[item[0]] = {'Paid': item[1:]}
return output
print(buildCoverageDictionary(paidList1))
print(buildCoverageDictionary(paidList2))
print(buildCoverageDictionary(paidList3))
def buildCoverageDictionary(paidList):
return {item[0]: {'Paid': item[1:]} for item in paidList}
这应该可以回答您的第一个问题:
paidList1 = [["JS", 200, 400, 500, 600], ["SK", 400, 1000, 1600]]
paidList2 = [["JS", 200, 400, 500, 600], ["SK", 1010, 2000], ["MJ", 5, 6, 7], ["ZF", 2660, 500]]
paidList3 = [["SK", 200, 400]]
def buildCoverageDictionary():
JS_dict = {'Paid': paidList1[0][1:5]}
SK_dict = {'Paid': paidList1[1][1:4]}
new_dict1 = {paidList1[0][0]: JS_dict, paidList1[1][0]: SK_dict}
print(new_dict1)
JS_dict = {'Paid': paidList2[0][1:5]}
SK_dict = {'Paid': paidList2[1][1:3]}
MJ_dict = {'Paid': paidList2[2][1:4]}
ZF_dict = {'Paid': paidList2[3][1:3]}
new_dict2 = {paidList2[0][0]: JS_dict, paidList2[1][0]: SK_dict, paidList2[2][0]: MJ_dict, paidList2[3][0]: ZF_dict}
print(new_dict2)
SK_dict = {'Paid': paidList3[0][1:3]}
new_dict3 = {paidList3[0][0]: SK_dict}
print(new_dict3)
buildCoverageDictionary()
我所做的更改是:
- 你没有使用参数
paidList
,所以我去掉了它。
- 当您调用
return
函数时,它会 returns 您想要的东西并跳过该函数的其余部分。所以我用 print
. 替换了 return
s
- 你拼错了
new_dict3
(as ew_dict3
)。
我应该从三个单独的输入接收重新格式化字典的输出。
当前输出:
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}
预期输出:
print(buildCoverageDictionary(paidList1)) ->
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}
print(buildCoverageDictionary(paidList2)) ->
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [1010, 2000]}, 'MJ': {'Paid':
[5, 6, 7]}, 'ZF': {'Paid': [2660, 500]}}
print(buildCoverageDictionary(paidList3)) -> {'SK': {'Paid': [200, 400]}}
两个问题:
- 如何编辑代码以输出预期的输出?
- 我如何编辑代码以不需要我单独重新格式化每个字典,而是创建一个函数,无论输入是什么,都可以正确地重新格式化字典?
我的代码如下:
paidList1 = [["JS", 200, 400, 500, 600], ["SK", 400, 1000, 1600]]
paidList2 = [["JS", 200, 400, 500, 600], ["SK", 1010, 2000], ["MJ", 5, 6, 7], ["ZF", 2660, 500]]
paidList3 = [["SK", 200, 400]]
def buildCoverageDictionary(paidList):
JS_dict = {'Paid': paidList1[0][1:5]}
SK_dict = {'Paid': paidList1[1][1:4]}
new_dict1 = {paidList1[0][0]: JS_dict, paidList1[1][0]: SK_dict}
return new_dict1
JS_dict = {'Paid': paidList2[0][1:5]}
SK_dict = {'Paid': paidList2[1][1:3]}
MJ_dict = {'Paid': paidList2[2][1:4]}
ZF_dict = {'Paid': paidList2[3][1:3]}
new_dict2 = {paidList2[0][0]: JS_dict, paidList2[1][0]: SK_dict, paidList2[2][0]: MJ_dict, paidList2[3][0]: ZF_dict}
return new_dict2
SK_dict = {'Paid': paidList3[0][1:3]}
ew_dict3 = {paidList3[0][0]: SK_dict}
return new_dict3
print(buildCoverageDictionary(paidList1))
print(buildCoverageDictionary(paidList2))
print(buildCoverageDictionary(paidList3))
正如@Barmar 提到的,return
结束函数,执行后什么也没有。您可以尝试类似下面的方法,并使其更灵活地适应输入的变化,例如通过循环 paidList
而不是对索引进行硬编码,而是使用 [1:]
从索引 1
翻译到最后一个
paidList1 = [["JS", 200, 400, 500, 600], ["SK", 400, 1000, 1600]]
paidList2 = [["JS", 200, 400, 500, 600], ["SK", 1010, 2000], ["MJ", 5, 6, 7], ["ZF", 2660, 500]]
paidList3 = [["SK", 200, 400]]
def buildCoverageDictionary(paidList):
output = {}
for item in paidList:
output[item[0]] = {'Paid': item[1:]}
return output
print(buildCoverageDictionary(paidList1))
print(buildCoverageDictionary(paidList2))
print(buildCoverageDictionary(paidList3))
def buildCoverageDictionary(paidList):
return {item[0]: {'Paid': item[1:]} for item in paidList}
这应该可以回答您的第一个问题:
paidList1 = [["JS", 200, 400, 500, 600], ["SK", 400, 1000, 1600]]
paidList2 = [["JS", 200, 400, 500, 600], ["SK", 1010, 2000], ["MJ", 5, 6, 7], ["ZF", 2660, 500]]
paidList3 = [["SK", 200, 400]]
def buildCoverageDictionary():
JS_dict = {'Paid': paidList1[0][1:5]}
SK_dict = {'Paid': paidList1[1][1:4]}
new_dict1 = {paidList1[0][0]: JS_dict, paidList1[1][0]: SK_dict}
print(new_dict1)
JS_dict = {'Paid': paidList2[0][1:5]}
SK_dict = {'Paid': paidList2[1][1:3]}
MJ_dict = {'Paid': paidList2[2][1:4]}
ZF_dict = {'Paid': paidList2[3][1:3]}
new_dict2 = {paidList2[0][0]: JS_dict, paidList2[1][0]: SK_dict, paidList2[2][0]: MJ_dict, paidList2[3][0]: ZF_dict}
print(new_dict2)
SK_dict = {'Paid': paidList3[0][1:3]}
new_dict3 = {paidList3[0][0]: SK_dict}
print(new_dict3)
buildCoverageDictionary()
我所做的更改是:
- 你没有使用参数
paidList
,所以我去掉了它。 - 当您调用
return
函数时,它会 returns 您想要的东西并跳过该函数的其余部分。所以我用print
. 替换了 - 你拼错了
new_dict3
(asew_dict3
)。
return
s