Python打印来自两个词典
我把对话分为两本字典,每本字典都包含那个人说的单词(我有两个人)。我必须打印4列(关键字、第一个目录中的数字(第一人称使用该词的次数)、第二个目录中的数字以及它们的数量),并按关键字排序。有人能帮我吗?输出必须如下所示:Python打印来自两个词典,python,Python,我把对话分为两本字典,每本字典都包含那个人说的单词(我有两个人)。我必须打印4列(关键字、第一个目录中的数字(第一人称使用该词的次数)、第二个目录中的数字以及它们的数量),并按关键字排序。有人能帮我吗?输出必须如下所示: african 1 0 1 air-speed 1 0 0 an 1 1 2 arthur 1 0 1 ... 如你所见,我有som文本 text = """Bridgekeeper: Hee hee he
african 1 0 1
air-speed 1 0 0
an 1 1 2
arthur 1 0 1
...
如你所见,我有som文本
text = """Bridgekeeper: Hee hee heh. Stop. What... is your name?
King Arthur: It is 'Arthur', King of the Britons.
Bridgekeeper: What... is your quest?
King Arthur: To seek the Holy Grail.
Bridgekeeper: What... is the air-speed velocity of an unladen swallow?
King Arthur: What do you mean? An African or European swallow?"""
bridgekeeper_w和arthur_w的输出:
print (bridgekeeper_w)
{'hee': 2, 'heh': 1, 'stop': 1, 'what': 3, 'is': 3, 'your': 2, 'name': 1, 'quest': 1, 'the': 1, 'air-speed': 1, 'velocity': 1, 'of': 1, 'an': 1, 'unladen': 1, 'swallow': 1}
print (arthur_w)
{'king': 4, 'it': 1, 'is': 1, 'arthur': 1, 'of': 1, 'the': 2, 'britons': 1, 'to': 1, 'seek': 1, 'holy': 1, 'grail': 1, 'what': 1, 'do': 1, 'you': 1, 'mean': 1, 'an': 1, 'african': 1, 'or': 1, 'european': 1, 'swallow': 1}
现在我需要这个(关键字、第一个单词的数字、第二个单词的数字和计数):
实现以下数据帧的步骤很少-
{'Bridgekeeper': Counter({'Hee': 1,
'hee': 1,
'heh': 1,
'Stop': 1,
'What': 3,
'is': 3,
'your': 2,
'name': 1,
'quest': 1,
'the': 1,
'airspeed': 1,
'velocity': 1,
'of': 1,
'an': 1,
'unladen': 1,
'swallow': 1}),
如果我们将JSON加载到数据帧中,它可以很容易地转换为所需的输出
from collections import defaultdict
import string
from collections import Counter
import pandas as pd
result = defaultdict(list)
for row in text.split('\n'):
result[row.split(':')[0].strip()].append(row.split(':')[1].strip())
result = {key:(' '.join(value)).translate(str.maketrans('', '', string.punctuation)) for key,value in result.items()}
result = {key:Counter(value.split(' ')) for key,value in result.items()}
df = pd.DataFrame(result).fillna(0).astype(int)
df['sum'] = df['Bridgekeeper'] + df['King Arthur']
df.to_csv('out.csv', sep='\t')
输出数据帧-
Bridgekeeper King Arthur sum
Hee 1 0 1
hee 1 0 1
heh 1 0 1
Stop 1 0 1
What 3 1 4
is 3 1 4
your 2 0 2
name 1 0 1
quest 1 0 1
the 1 2 3
airspeed 1 0 1
velocity 1 0 1
of 1 1 2
an 1 0 1
unladen 1 0 1
swallow 1 1 2
It 0 1 1
Arthur 0 1 1
King 0 1 1
Britons 0 1 1
To 0 1 1
seek 0 1 1
Holy 0 1 1
Grail 0 1 1
do 0 1 1
you 0 1 1
mean 0 1 1
An 0 1 1
如果您已经有两个字典,那么主要的问题是如何循环任意一个字典中的键。但这并不难
对于已排序的密钥(集合(列表(bridgekeeper_w.keys())+列表(arthur_w.keys())):
如果密钥不在bridgekeeper中,则b_count=0,否则bridgekeeper_w[密钥]
如果密钥不在arthur\u w中,则a\u count=0,否则arthur\u w[密钥]
打印(“%-20s%3i%3i%3i%”(键、b\u计数、a\u计数、b\u计数+a\u计数))
如果字典的完整性不重要,一个更优雅的解决方案可能是将缺少的键添加到其中一个字典中,然后简单地循环它的所有键
arthur_w.keys()中的键的:
如果钥匙不在bridgekeeper_w中:
桥头堡守望者_w[钥匙]=0
对于键,已排序的b_计数(bridgekeeper_w.items()):
如果密钥不在arthur\u w中,则a\u count=0,否则arthur\u w[密钥]
打印(“%-20s%3i%3i%3i%”(键、b\u计数、a\u计数、b\u计数+a\u计数))
这消除了第一个解决方案中相当繁琐且稍微复杂的
集合(list(keys())
,代价是遍历其中一个字典两次。或者没有第三方库的解决方案:
bridgekeeper_d={'hee':2,'heh':1,'stop':1,'what':3,'is':3,'your':2,'name':1,'quest':1,'the':1,'air speed':1,'an':1,'unladen':1,'swallow':1}
arthur_d={'king':4,'it':1,'is':1,'arthur':1,'of':1,'the':2,'Britans':1,'to':1,'seek':1,'holy':1,'grail':1,'what':1,'do':1,'you':1,'mean':1,'an':1,'african':1,'or':1,'european':1,'Sw
joined=dict.fromkeys(list(bridgekeeper_d.keys())+list(arthur_d.keys()),{}
对于键,bridgekeeper_d.items()中的值:
加入[key][“bridgekeeper”]=值
对于键,arthur_d.items()中的值:
加入[键][“亚瑟”]=值
#此时,连接的外观如下所示:
# {
#hee:{'bridgekeeper':1,'arthur':1},
#嘿:{'bridgekeeper':1,'arthur':1},
#'stop':{'bridgekeeper':1,'arthur':1},
#'什么':{'bridgekeeper':1,'arthur':1}
# ...
# }
对于键,dic已加入。项()
打印(“%-15s%d%d%d%”(键,dic[“桥头堡”]、dic[“亚瑟”]、dic[“桥头堡”]+dic[“亚瑟”]))
输出:
hee12
嘿嘿,一一二
停止1 1 2
什么11 2
这是112
你的1112
名称11 2
任务1 1 2
11号和12号
空气速度1 1 2
速度1 1 2
第12页
A 11 2
空车1 1 2
吞下1 1 2
国王1 1 2
它是1112
亚瑟11 2
英国人1 1 2
1至12
寻求1 1 2
神圣的11 2
圣杯12
做1 1 2
你11 2
平均11.2
非洲1 1 2
还是11 2
欧洲1 1 2
嗯……我看不出任何问题?我不知道如何这样打印:“african 1 0 1”请在单独的部分将示例输入和预期输出添加到您的问题中,您可以在代码末尾添加一个打印(bridgekeeper_w)
,重新运行它并发布输出,这样我就知道dict是什么样子了?我在postCan的底部添加了它。请逐行解释代码的作用?即使是作为一名高级Python程序员,这一点也不明显,我们也不知道OP的经验有多丰富#@程序员doneIf如果您没有注意到,OP只想知道如何加入并打印2条指令……谢谢您,但我认为作为初学者,这对我来说确实是“硬编码”。我不想编辑代码,我只是想帮助将其打印成表单关键字、数字、数字,但这是我在大学上的第二堂关于python的课,我们必须使用PandaScope。请解释一下您的格式(%-20s%3i%3i
)是如何工作的?我不明白……Python中有几种打印格式解决方案,它们都有将列对齐到特定宽度的机制。简而言之,%-20s
表示为20个字符长的字符串腾出空间,如果内容较短,则向右填充。类似地,%3i
表示打印一个最多三位数字的数字,并向左填充。这方面有很多问题,;看,例如,正如我所说,你的答案显然更好,因此值得接受……谢谢,你太谦虚了(:为什么你使用太旧的%
格式?可以作为f字符串:f'{key:20}{b_count:3}{a_count:3}{b_count+a_count}
Bridgekeeper King Arthur sum
Hee 1 0 1
hee 1 0 1
heh 1 0 1
Stop 1 0 1
What 3 1 4
is 3 1 4
your 2 0 2
name 1 0 1
quest 1 0 1
the 1 2 3
airspeed 1 0 1
velocity 1 0 1
of 1 1 2
an 1 0 1
unladen 1 0 1
swallow 1 1 2
It 0 1 1
Arthur 0 1 1
King 0 1 1
Britons 0 1 1
To 0 1 1
seek 0 1 1
Holy 0 1 1
Grail 0 1 1
do 0 1 1
you 0 1 1
mean 0 1 1
An 0 1 1