从词典列表中制作几个词云
Making several wordclouds out of list of dictionaries
我在 union_dicts
中有一个词典列表。为了给你一个想法,它的结构如下
union_dicts = [{'bla' : 6, 'blub': 9}, {'lub': 20, 'pul':12}]
(实际的dicts列表要长很多倍,但这是为了给出思路)
对于这个特定的词典列表,我想制作一个词云。制作词云的函数如下(这个没毛病):
def make_words(words):
return ' '.join([('<font size="%d">%s</font>'%(min(1+words[x]*5/max(words.values()), 5), x)) for x in words])
现在我写了下面的代码,应该返回每一个字典。 Return 在下面的函数中只返回第一个字典:
def bupol():
for element in union_dicts:
return HTML(make_words(element))
bupol()
我已经尝试简单地将其打印出来,但我只是得到“'Ipython display object'”,而不是实际显示。我确实想要显示器。产量也不适用于此功能,并且出于某种原因,以当前方式使用 list = []
和 list.apped()
return 列表而不是 returning 也不起作用。我对如何正确迭代它一无所知,所以我在 union_dicts
中显示了每个字典,这是一个字典列表。
这样的怎么样?
def bupol():
result = []
for element in union_dicts:
result.append(HTML(make_words(element)))
return result
我在 union_dicts
中有一个词典列表。为了给你一个想法,它的结构如下
union_dicts = [{'bla' : 6, 'blub': 9}, {'lub': 20, 'pul':12}]
(实际的dicts列表要长很多倍,但这是为了给出思路)
对于这个特定的词典列表,我想制作一个词云。制作词云的函数如下(这个没毛病):
def make_words(words):
return ' '.join([('<font size="%d">%s</font>'%(min(1+words[x]*5/max(words.values()), 5), x)) for x in words])
现在我写了下面的代码,应该返回每一个字典。 Return 在下面的函数中只返回第一个字典:
def bupol():
for element in union_dicts:
return HTML(make_words(element))
bupol()
我已经尝试简单地将其打印出来,但我只是得到“'Ipython display object'”,而不是实际显示。我确实想要显示器。产量也不适用于此功能,并且出于某种原因,以当前方式使用 list = []
和 list.apped()
return 列表而不是 returning 也不起作用。我对如何正确迭代它一无所知,所以我在 union_dicts
中显示了每个字典,这是一个字典列表。
这样的怎么样?
def bupol():
result = []
for element in union_dicts:
result.append(HTML(make_words(element)))
return result