将聚类术语存储在使用Python中的字典创建的变量中

时间:2018-05-15 04:40:32

标签: python loops dictionary variables

我有一个python脚本,我已经浏览了一个样本列表并提取了这些功能并将文档功能集中在一起。我想将每个集群保存到循环本身内创建的变量中。

为了做到这一点,我宣布了一个字典,并创建了一组名为cluster0cluster1cluster2等的变量,并存储了单词列表在每个集群中使用以下方法创建新变量。

    d = {}
    feature_set =[]

    if(len(filtered_terms)!=0):
        for m in filtered_terms:
            print(' %s' % m, end='')
            feature_set.append(m)
        for w in cluster_terms:
            for b in filtered_terms:
                if (w != b):
                    print(' %s' % w, end='')
                    feature_set.append(w)
    else:
        for h in cluster_terms:
            print(' %s' % h, end='')
            feature_set.append(h)

    for f in range(0, i+1):
        #globals()['string%s' % f] = feature_set
        d["cluster{0}".format(f)] = feature_set

    print()
print("Clusters stored in a dictionary of Variables")

print ()
for k in d:
    print (k)
    print (d[k])

原始群集数据如下所示。

Top terms per cluster:
Cluster 0: wilson adam presid cleveland roosevelt lincoln grant monro fillmor parti
Cluster 1: instrument flute drum drum flute instrument bar bar sound sound instrument trumpet trumpet music music concert concert flute
Cluster 2: string cello violin instrument violin violin violin cello cello cello string string string string string string bow bow bow bow instrument instrument instrument cello cello cello violin violin violin music music music music instrument instrument instrument
Cluster 3: languag chines german italian arab spanish spoken swahili ghana vietnames
Cluster 4: newton string kangaroo guitar ford singapor penguin uruguay romania piano
Cluster 5: eleph beetl polar leopard speci wolv bear wolf fur

忽略群集2中单词的重复,我尝试使用上面显示的以下方法打印存储在使用字典d创建的变量中的单词列表。

for k in d:
    print (k)
    print (d[k])

但是我得到以下输出,其中变量名称被正确创建为cluster0cluster1cluster2等,但只有集群5的内容(集群5中的最后一个集群)原始集群)被重复存储到所有变量中。

输出

cluster2
[u'eleph', u'beetl', u'polar', u'leopard', u'speci', u'wolv', u'bear', u'wolf', u'fur']
cluster3
[u'eleph', u'beetl', u'polar', u'leopard', u'speci', u'wolv', u'bear', u'wolf', u'fur']
cluster0
[u'eleph', u'beetl', u'polar', u'leopard', u'speci', u'wolv', u'bear', u'wolf', u'fur']
cluster1
[u'eleph', u'beetl', u'polar', u'leopard', u'speci', u'wolv', u'bear', u'wolf', u'fur']
cluster4
[u'eleph', u'beetl', u'polar', u'leopard', u'speci', u'wolv', u'bear', u'wolf', u'fur']
cluster5
[u'eleph', u'beetl', u'polar', u'leopard', u'speci', u'wolv', u'bear', u'wolf', u'fur']

在这方面的任何帮助将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

您的feature_set是一个单独的列表,就像您打印出来一样。

此代码:

for f in range(0, i+1):
    d["cluster{0}".format(f)] = feature_set

只需将此列表分配给每个群集,因此非常期待最终结果。为了使您的代码能够工作,一个想法就是将feature_set作为二维列表,如下所示:

feature_set = [[1, 2, 3, 4], [5, 6], [7, 8, 9], [10], [11,12]]

for f in range(0, i+1):
    d["cluster{0}".format(f)] = feature_set[f]

feature_set[f]就是您群集的内容。

答案 1 :(得分:0)

刚刚找到解决方案。这只是改变我创建字典的位置的问题。由于字典是在我已经迭代i的最顶层循环中创建的,因此以下for循环继续通过父循环并通过使用feature_set初始化变量而结束在上一个i循环期间录制。

因此,我修改了i循环上的字典初始化,如下所示,现在是单词。

d = {}
for i in range(true_k):
    print("Cluster %d:" % i, end='')
    cluster_terms = []
    for ind in order_centroids[i, :10]:
       ...

    feature_set =[]

    if(len(filtered_terms)!=0):
        for m in filtered_terms:
            print(' %s' % m, end='')
            feature_set.append(m)
        for w in cluster_terms:
            for b in filtered_terms:
                if (w != b):
                    print(' %s' % w, end='')
                    feature_set.append(w)
        d["cluster{0}".format(i)] = feature_set
    else:
        for h in cluster_terms:
            print(' %s' % h, end='')
            feature_set.append(h)
        d["cluster{0}".format(i)] = feature_set

        #globals()['string%s' % f] = feature_set
    #d["cluster{0}".format(i)] = feature_set

    print()
print("Clusters stored in a dictionary of Variables")

print ()
for k in d:
    print (k)
    print (d[k])