我有一个问题,我不知道如何在同一行上打印两本词典。你知道吗
我有:
Fantasy = {"Gotham":[city, 2000], "Smallville":[town, 40], "Nuketown":[town,60]}
Real = {"London":[city, 1500], "Whitby":[town, 40], "Liverpool":[city, 1000]}
我试过这个(通过):
listFantasy = dictList(Fantasy)
listReal = dictList(Real)
def dictList (dict):
list = []
for key, value in dict_.iteritems(): # Create list form dict
temp = [key,value]
lista_finale.append(temp)
return list
for list1, list2 in zip(listFantasy, listReal):
print list1[0]
for listValue1, listValue2 in zip(list1[1:],list2[1:]):
listValue1.sort(key = lambda x: x[1], reverse = True)
listValue2.sort(key = lambda x: x[1], reverse = True)
for value1, value2 in zip(listValue1, listValue2):
print " \n %-10s \t %-10s" % (valore1[0], valore1[1]),"\t\t\t\t %-10s \t %-10s" % (valore2[0], valore2[1])
输出:
Gotham London
city 2000 city 1500
NukeTow Liverpool
town 60 city 1000
Smallville Whitby
town 40 town 40
但是这个解决方案非常慢,大约需要30秒,我需要知道如何以相同的方式直接在线打印两个词典,而不会在代码执行上严重浪费时间。你知道吗
可能是这样?你知道吗
如果您想将任意数量的字典(可能,不仅是幻想和真实的)与多个对象类型(可能,不仅是城市和城镇)连接并对齐,您可能会发现
pandas
很有用:这段代码打印的内容比您想要的要多一些,但我认为这些额外的信息很有用,必须显式显示:
试试这是:-
相关问题 更多 >
编程相关推荐