Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将集群术语存储在使用Python中的字典创建的变量中_Python_Loops_Dictionary_Variables - Fatal编程技术网

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

将集群术语存储在使用Python中的字典创建的变量中,python,loops,dictionary,variables,Python,Loops,Dictionary,Variables,我有一个python脚本,在这个脚本中,我浏览了一个示例列表,提取了特性并对文档特性进行了集群。我想将每个集群保存到一个在循环本身中创建的变量中 为了做到这一点,我声明了一个字典并创建了一组变量,这些变量将被命名为cluster0、cluster1、cluster2,等等,并使用以下方法将每个集群中的单词列表存储到新创建的变量中 d = {} feature_set =[] if(len(filtered_terms)!=0): for m in fil

我有一个python脚本,在这个脚本中,我浏览了一个示例列表,提取了特性并对文档特性进行了集群。我想将每个集群保存到一个在循环本身中创建的变量中

为了做到这一点,我声明了一个字典并创建了一组变量,这些变量将被命名为
cluster0
cluster1
cluster2
,等等,并使用以下方法将每个集群中的单词列表存储到新创建的变量中

    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中单词的重复,我尝试使用上面显示的以下方法打印使用dictionary
d
创建的变量中存储的单词列表

for k in d:
    print (k)
    print (d[k])
但是我得到了以下输出,其中变量名被正确地创建为
cluster0
cluster1
cluster2
,等等。但是只有集群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']

非常感谢您在这方面提供的任何帮助。

您的
功能集是一个简单的列表,就像您打印出来的一样

此代码:

for f in range(0, i+1):
    d["cluster{0}".format(f)] = feature_set
只需将此列表分配给每个集群,因此最终结果是非常令人期待的。为了让您的代码正常工作,一个想法是将您的
功能设置为二维列表,如下所示:

feature\u 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]

功能集[f]
就是集群的内容。

刚刚找到了解决方案。这仅仅是改变我创建词典的地点的问题。由于字典是在最顶层的循环中创建的,我已经在该循环中迭代了
I
,因此下面的
for
循环继续通过父循环,并通过使用上次
I
循环中记录的
feature\u set
初始化变量来结束

因此,我在
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])

谢谢你的推荐。虽然我确实得到了我所需要的,但我想在以后访问内容时,把它放在2D列表中可能是一种可行的方法。