把矩阵转为字符串写入文件

import numpy as  np
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    fa = open('ntweet.txt', 'r')
    line = fa.readline()
    testlist = []
    al=[]
    bl=[]
    timelist = []
    time_dist = dict()
    train_pos=[]
    while line:
        line=line.split('\n')
        a=int(line[0].split(' ')[0])
        # print(a)
        b =int(line[0].split(' ')[1])
        # print(b)
        al.append(a)
        bl.append(b)
        line = fa.readline()
    fa.close()
    node=al+bl
    ns=list(set(node))
    ns.sort()
    print(len(al), len(bl), len(node),len(ns))
    ones = np.ones((len(ns), 1000))
    number= np.array(ns)
    number = number[:, np.newaxis]
    fea=np.append(number, ones, axis=1)
    fea= fea.tolist()

    # print(fea.shape)

    f = open("k.txt", "w")

    t=''
    for i in fea:
        for e in range(len(fea[0])):
            t = t + str(i[e]) + ' '
        f.write(t.strip(' '))
        f.write('\n')
        t = ''
    f.close()

你可能感兴趣的:(矩阵,numpy,python)