when printing key and value it prints they key+value as many times as I have digits in my value
本问题已经有最佳答案,请猛点这里访问。
我正在学习Python3中的编程,我在为列表中的语句格式化2时遇到了问题。我是这样做的:
1 2 3 4 5 6 7 8 9 | items = { "Iphone X": {"price": 1000,"stock":10}, "Samsung S9": {"price": 800,"stock":10}, "Huawei P20": {"price": 600,"stock":10}, "HTC Vive": {"price": 400,"stock":10} } test = ["{} - {}$".format(nume, pret) for nume in items.keys() for pret in items[nume]["price"]] print(" ".join(test)) |
我不知道为什么当我把
如果我不改变
只需使用
1 2 3 4 5 | d = {'hammer':'5', 'scewdriver':'2'} test = ["{} - {}$".format(k, v) for k,v in d.items()] print(" ".join(test)) |