我在.txt文件中有幾個空格分隔的數組我想從一個文件夾中將它們全部導入,按正確的順序連接它們,然後取一列並將值其中一列繼續從第一列開始。導入並連接數組,然後按順序在一列中插入值
基本上,如果我有數組1
[1 2 3]
[2 2 3]
[3 2 3]
和陣列2
[1 2 3]
[2 3 3]
[3 2 3]
我想最終的陣列
[1 2 3]
[2 2 3]
[3 2 3]
[4 2 3]
[5 3 3]
[6 2 3]
任何想法?將一列的最終值添加到下一個數組中相同列的所有值非常重要。在第三個數組中,我想添加SECOND數組的最終值,依此類推。
噢,我很新的,到目前爲止,我只有這一點,這使得文件名列表:
import os
import numpy as np
from os import listdir
mypath = raw_input('Enter directory path: ')
relevant_path = mypath
included_extenstions = ['txt']
file_names = [fn for fn in os.listdir(relevant_path)
if any(fn.endswith(ext) for ext in included_extenstions)]
sorted_file_names = sorted(file_names)
好了,現在我有這樣的:
included_extenstions = ['txt']
file_names = [fn for fn in os.listdir(relevant_path)
if any(fn.endswith(ext) for ext in included_extenstions)]
sorted_file_names = sorted(file_names)
loc_one = np.loadtxt(sorted_file_names[0])
loc_two = np.loadtxt(sorted_file_names[1])
loc_three = np.loadtxt(sorted_file_names[2])
loc_four = np.loadtxt(sorted_file_names[3])
loc_five = np.concatenate((loc_one, loc_two), axis = 0)
loc_five[-len(loc_two):,1] += loc_one[-1,1]
loc_six = np.concatenate((loc_five, loc_three), axis = 0)
loc_six[-len(loc_three):,1] += loc_five[-1,1]
loc_seven = np.concatenate((loc_six, loc_four), axis = 0)
loc_seven[-len(loc_four):,1] += loc_six[-1,1]
(數組都被稱爲loc_number)它看起來很蠻力而且不靈活。另外,如果我多次運行它,它以某種方式存儲它在第一次運行時所做的事情,因此所有事情都保持加倍 - loc_seven第一次提供了正確的結果,但第二次運行時的loc_seven提供了兩個loc_sevens(然後是4等)的串聯)。
要開始,你可以使用[numpy.loadtxt](http://docs.scipy.org/doc/numpy-1.11.0/reference/generated/numpy.loadtxt.html#numpy-loadtxt)和[numpy.column_stack](http:// docs .scipy.org/doc/numpy-1.11.0/reference/generated/numpy.column_stack.html#numpy.column_stack) – Michele