我有一个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