使用Python 3.6中的setdefault使用来自两个不同文件的信息显示(名称,ID和频率计数)

时间:2017-04-11 16:00:50

标签: python dictionary frequency

我正在尝试读取两个.dat文件并创建一个程序,该程序使用aid2name的值作为字典中的键,该字典具有aid2numplays的键和值,并设置为其值。这一切都是希望文件产生的结果包括(艺术家姓名,艺术家ID,播放频率)。值得注意的是,第一个文件提供艺术家姓名和艺术家ID,而第二个文件提供用户ID,艺术家ID和每个用户的频率。任何想法如何按用户聚合这些频率,然后以(艺术家姓名,艺术家ID,播放频率)格式显示它们?以下是我目前管理的内容:

import codecs
aid2name = {}
d2 = {}
fp = codecs.open("artists.dat", encoding = "utf-8")
fp.readline()  #skip first line of headers
for line in fp:
    line = line.strip()
    fields = line.split('\t')
    aid = int(fields[0])
    name = fields[1]
    aid2name = {int(aid), name}
    d2.setdefault(fields[1], {})
    #print (aid2name)
# do other processing
    #print(dictionary)

aid2numplays = {}
fp = codecs.open("user_artists.dat", encoding = "utf-8")
fp.readline()  #skip first line of headers
for line in fp:
    line = line.strip()
    fields = line.split('\t')
    uid = int(fields[0])
    aid = int(fields[1])
    weight = int(fields[2])
    aid2numplays = [int(aid), int(weight)]
    #print(aid2numplays)
    #print(uid, aid, weight)

for (d2.fields[1], value) in d2:
    group = d2.setdefault(d2.fields[1], {}) # key might exist already
    group.append(aid2numplays)

print(group)

1 个答案:

答案 0 :(得分:1)

修改:关于setdefault的使用,如果您想按艺术家ID对用户数据进行分组,那么您可以:

grouped_data = {}
for u in users:
    k, v = u[1], {'userID': u[0], 'weight': u[2]}
    grouped_data.setdefault(k, []).append(v)

这与写作基本相同:

grouped_data = {}
for u in users:
    k, v = u[1], {'userID': u[0], 'weight': u[2]}
    if k in grouped_data:
        grouped_data[k].append(v)
    else:
        grouped_data[k] = [v]

作为如何计算艺术家在不同用户数据中出现的次数的示例,您可以将数据读入列表列表:

with codecs.open("artists.dat", encoding = "utf-8") as f:
    artists = f.readlines()

with codecs.open("user_artists.dat", encoding = "utf-8") as f:
    users = f.readlines()

artists = [x.strip().split('\t') for x in artists][1:]  # [['1', 'MALICE MIZER', ..
users = [x.strip().split('\t') for x in users][1:]  # [['2', '51', '13883'], ..]

使用 artistID 作为关键字迭代艺术家创建字典。为播放统计添加占位符。

data = {}
for a in artists:
    artistID, name = a[0], a[1]
    data[artistID] = {'name': name, 'plays': 0}

迭代用户每行更新字典:

for u in users:
    artistID = u[1]
    data[artistID]['plays'] += 1

数据的输出

{'1': {'name': 'MALICE MIZER', 'plays': 3},
 '2': {'name': 'Diary of Dreams', 'plays': 12},
 '3': {'name': 'Carpathian Forest', 'plays': 3},  ..}

修改:要迭代用户数据并创建与用户关联的所有艺术家的字典,我们可以:

artist_list = [x.strip().split('\t') for x in artists][1:]
user_stats_list = [x.strip().split('\t') for x in users][1:]

artists = {}
for a in artist_list:
    artistID, name = a[0], a[1]
    artists[artistID] = name

grouped_user_stats = {}
for u in user_stats_list:
    userID, artistID, weight = u
    if userID not in grouped_user_stats:
        grouped_user_stats[userID] = { artistID: {'name': artists[artistID], 'plays': 1} }
    else:
        if artistID not in grouped_user_stats[userID]:
            grouped_user_stats[userID][artistID] = {'name': artists[artistID], 'plays': 1}
        else:
            grouped_user_stats[userID][artistID]['plays'] += 1
            print('this never happens') 
            # it looks the same artist is never listed twice for the same user

输出:

{'2': {'100': {'name': 'ABC', 'plays': 1},
       '51': {'name': 'Duran Duran', 'plays': 1},
       '52': {'name': 'Morcheeba', 'plays': 1},
       '53': {'name': 'Air', 'plays': 1}, .. }, 
 ..
}