Python中字典中的字典

Python中字典中的字典,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,假设我有一个名为website.csv的csv文件: facebook.com a social network website twitter.com another social network website facebook.com a social website facebook.com a website twitter.com another network website youtube.com

假设我有一个名为website.csv的csv文件:

facebook.com        a social network website
twitter.com         another social network website
facebook.com        a social website
facebook.com        a website
twitter.com         another network website 
youtube.com         a website like facebook but to share videos
youtube.com         a video sharing website
我想创建一个包含网站名称(作为关键字)的字典,其值是描述中的单词字典,它将每个单词作为关键字,单词出现的频率作为值,并且应该包括变量“TOTAL”作为每个网站描述中单词数的总和

这是我创建的代码:

def webdescription(data):
    import csv
    data = website.csv
    csvreader = csv.reader(data)
    d = defaultdict(int)
    dfinal = {}
    for line in data:
        description_list = line[1].split()
        dfinal[line[0]] = d
        for each in description_list:
            d[each] += 1
            d['_TOTAL_'] = sum(d.itervalues())
    return dfinal
预期产出应为:

{'facebook.com': {'a':3, 'social': 2, 'network':1, 'website':3, '_TOTAL_': 9}
{'twitter.com': {'another':2, 'social':1, 'network':2, 'website':2, '_TOTAL_': 7}
{'youtube.com': {'a':2, 'website':2, 'like': 1, 'sharing':1, 'share':1, 'video':1,     
'videos': 1, 'facebook': 1, '_TOTAL_':10}
我似乎没有得到预期的产出。
非常感谢您的帮助

website.csv

facebook.com,a social network website
twitter.com,another social network website
facebook.com,a social website
facebook.com,a website
twitter.com,another network website 
youtube.com,a website like facebook but to share videos
youtube.com,a video sharing website

>>从集合导入defaultdict,计数器
>>>d=默认DICT(计数器)
>>>打开('website.csv')作为f:
对于名称,csv.reader中的描述(f):
单词=描述拆分()
d[名称]。更新(字)
d[姓名]['TOTAL']+=len(字)
>>>d
defaultdict(,{'facebook.com':Counter({'TOTAL':9,'a':3,'website':3,'social':2,'network':1}),'twitter.com':Counter({'TOTAL':7,'website':2,'network':2}),'youtube.com':Counter({'TOTAL':12,'a':2,'website':2,'sharing:1,'sharing 1,'but 1,'to':1,'facebook''video':1})})

website.csv

facebook.com,a social network website
twitter.com,another social network website
facebook.com,a social website
facebook.com,a website
twitter.com,another network website 
youtube.com,a website like facebook but to share videos
youtube.com,a video sharing website

>>从集合导入defaultdict,计数器
>>>d=默认DICT(计数器)
>>>打开('website.csv')作为f:
对于名称,csv.reader中的描述(f):
单词=描述拆分()
d[名称]。更新(字)
d[姓名]['TOTAL']+=len(字)
>>>d
defaultdict(,{'facebook.com':Counter({'TOTAL':9,'a':3,'website':3,'social':2,'network':1}),'twitter.com':Counter({'TOTAL':7,'website':2,'network':2}),'youtube.com':Counter({'TOTAL':12,'a':2,'website':2,'sharing:1,'sharing 1,'but 1,'to':1,'facebook''video':1})})

您总是使用相同的d。应该为每条新线创建一个新对象,如

for line in data:
    description_list = line[1].split()
    d = dfinal[line[0]] = defaultdict(int)

你总是使用相同的d。应该为每条新线创建一个新对象,如

for line in data:
    description_list = line[1].split()
    d = dfinal[line[0]] = defaultdict(int)

你的产出是多少?它看起来怎么样?我的输出没有包含同一网站的所有描述,而是打印{'facebook.com':{'a':1,'social':1,'network':1,'website':1,'TOTAL':4}等等:\你的输出是什么?它看起来怎么样?我的输出没有包含同一网站的所有描述,而是打印{'facebook.com':{'a':1,'social':1,'network':1,'website':1,'TOTAL':4}等等:\n但有一件事,如果csv文件实际上包含两列以上,并且网站和描述分别位于第一列和最后一列,该怎么办?@FrederickAdler小改动,
用于csv中的行。reader(f)
name,desc=row[0],row[-1]
但是有一件事,如果csv文件实际上包含两列以上,并且网站和描述分别位于第一列和最后一列,该怎么办?@FrederickAdler小改动,csv.reader(f)中的行为
name,desc=row[0],row[-1]