我有一個Python字典和一個爲該值定義的列表。我無法以我需要的格式打印輸出。Python打印字典鍵,值爲列表時的值。打印每個鍵,在單獨的行上的值
dict = {1 : [2,3], 2 : [1,4], 3 : [2,4], 4 : [2,3]}
所需打印格式:
1 - 2, 1 - 3
2 - 1, 2 - 4
3 - 2, 3 - 4
4 - 2, 4 - 3
代碼:
dict = {1 : [2,3], 2 : [1,4], 3 : [2,4], 4 : [2,3]}
for key, value in sorted(dict.items()):
for links in value:
print ("{} - {},".format(key, links)),
輸出:
1 - 2, 1 - 3, 2 - 1, 2 - 4, 3 - 2, 3 - 4, 4 - 2, 4 - 3,
或者:
for key, value in sorted(dict.items()):
for links in value:
print ("{} - {},".format(key, links))
1 - 2,
1 - 3,
2 - 1,
2 - 4,
3 - 2,
3 - 4,
4 - 2,
4 - 3,
的可能的複製[蟒3:上同一行上打印新的輸出(http://stackoverflow.com/questions/12032214/python-3-print-new-output-on - 同行) – smac89