首页 > 解决方案 > 在计算给定大型数据集的项目和频率后创建两个单独的文本文件

问题描述

我有一个大的值数据集,我按降序排序并使用如此大列表的 Python 代码计算项目和频率。输出在逗号分隔的列表中,但我希望输出在列中并保存在两个不同的文本文件中,一个文本文件用于一个垂直列中的项目,另一个文本文件用于垂直列中此类项目的频率柱子。这是我用来计算项目和频率的代码,我只需要建议为垂直列中的每个参数添加两个单独的文本文件的创建。

K = [0.11729534, 0.16569225, 0.2672644, 0.19168988, 0.095590018, 0.082994543, 0.087023214, 0.10699161, 0.063435465, 0.028770683, 0.029708872, 0.041429114, 0.046457175, 0.057534702, 0.045394801, 0.051440958, 0.05362796, 0.072624497, 0.099292949, 0.22106786, 0.30126628]

# K values in descending order
K_sorted = sorted(K, reverse=True)


# Calculate frequency for the K values in descending order
items, freqs = np.unique(K_sorted, return_counts=True)
items, freqs = items[::-1], freqs[::-1]
print('New K list without repetitions= ',items)
print('Frequency= ',freqs)

任何建议谢谢

标签: pythontext-filesfrequencyitems

解决方案


我想这就是你想要的

with open('k-values.txt', 'w') as filehandle:
    for listitem in items:
        filehandle.write('%s\n' % listitem)

with open('freq.txt', 'w') as filehandle:
    for listitem in freqs:
        filehandle.write('%s\n' % listitem)

推荐阅读