我正在将文件中的数据读入一系列列表,如下所示:
sourceData = [[source, topic, score],[source, topic, score],[source, topic, score]...]
其中,每个列表中的源和主题可以相同也可以不同。你知道吗
我试图实现的是一个字典,它将与每个源相关的主题和它们的相关分数(分数将被平均,但是为了这个问题的目的,让我们把它们列为主题(键)的值)。你知道吗
理想情况下,结果将类似于嵌套dict的列表,如下所示:
[{SOURCE1:{TOPIC_A:SCORE1,SCORE2,SCORE3},
{TOPIC_B:SCORE1,SCORE2,SCORE3},
{TOPIC_C:SCORE1,SCORE2,SCORE3}},
{SOURCE2:{TOPIC_A:SCORE1,SCORE2,SCORE3},
{TOPIC_B:SCORE1,SCORE2,SCORE3},
{TOPIC_C:SCORE1,SCORE2,SCORE3}}...]
我认为最好的方法是创建一个源代码计数器,然后为每个源代码的每个主题创建一个dict,并将每个dict保存为每个对应源代码的值。但是,我在正确迭代以获得所需结果时遇到了困难。你知道吗
以下是我目前掌握的情况:
sourceDict = {}
sourceDictList = []
for row in sourceData:
source = row[0]
score = row[1]
topic = row[2]
sourceDict = [source,{topic:score}]
sourceDictList.append(sourceDict)
sourceList.append(source)
其中sourceDictList
的结果如下:[[source, {topic: score}]...],
(实质上是重新格式化原始列表中的数据),而sourceList
只是所有源的列表(一些重复)。你知道吗
然后初始化一个计数器,并将计数器中的源代码与sourceDictList
中的源代码进行匹配,如果匹配,则将topic:score
dict保存为键:
sourceCounter = Counter(sourceList)
for key,val in sourceCounter.items():
for dictitem in sourceDictList:
if dictitem[0] == key:
sourceCounter[key] = dictitem[1]
但是输出只是将最后的topic:score
dict保存到每个源。因此,不是期望的:
[{SOURCE1:{TOPIC_A:SCORE1,SCORE2,SCORE3},
{TOPIC_B:SCORE1,SCORE2,SCORE3},
{TOPIC_C:SCORE1,SCORE2,SCORE3}},
{SOURCE2:{TOPIC_A:SCORE1,SCORE2,SCORE3},
{TOPIC_B:SCORE1,SCORE2,SCORE3},
{TOPIC_C:SCORE1,SCORE2,SCORE3}}...]
我只得到:
Counter({SOURCE1: {TOPIC_n: 'SCORE_n'}, SOURCE2: {TOPIC_n: 'SCORE_n'}, SOURCE3: {TOPIC_n: 'SCORE_n'}})
我的印象是,如果dict中保存了一个唯一的密钥,它将附加key:value
对,而不会覆盖以前的密钥。我错过什么了吗?你知道吗
谢谢你的帮助。你知道吗
您可以简单地使用集合的defaultdict
我们可以做到:
相关问题 更多 >
编程相关推荐