将列数据转置并复制到每一行
Transpose and copy column data to each row
我在以下结构中设置了数据
A ,B ,C ,D ,E
1 a1,b1,c1,d1,e1
2 a2,b2,c2,d2,e2
3 a3,b3,c3,d3,e3
4 a4,b4,c4,d4,e4
我想从 E 列下的每一行中提取 x 个字段,以便可以发生以下影响
A ,B ,C ,D ,E
1 a1,b1,c1,d1,e1,e2,e3
2 a2,b2,c2,d2,e2,e3,e4
3 a3,b3,c3,d3,e3,e4,etc
4 a4,b4,c4,d4,e4,etc,etc
我只是使用了 etc,因为我想像我正在尝试做的事情的想法会被接受。
本质上转置数据只是让我把一列的一个区域变成一行,但是转置后我不想删除数据,我只是想复制那些区域,但是我有一个很大的传播 sheet(目前有 1500 行并且还在增加)
虽然我使用的是扩展 sheet,但这只是一个 CSV,如果需要,我很乐意使用各种脚本语言或编程语言,我考虑过 C++,python ,javascript 等等,但我不确定实现此目标的最简单方法是什么。
这基本上可以满足您的要求。首先是一些虚拟数据:
def char_range(c1, c2):
"""Generates the characters from `c1` to `c2`, inclusive."""
for c in xrange(ord(c1), ord(c2)+1):
yield chr(c)
matrix = [[c + str(i) for c in char_range('a', 'e')] for i in range(1, 5)]
这给了我们:
[['a1', 'b1', 'c1', 'd1', 'e1'],
['a2', 'b2', 'c2', 'd2', 'e2'],
['a3', 'b3', 'c3', 'd3', 'e3'],
['a4', 'b4', 'c4', 'd4', 'e4']]
现在一些变量使表达式更易于阅读:
# How many values you want to grab for each row
x = 3
# Length of the last column in the matrix
last = len(matrix)
现在是魔术。我假设,如果我们已经到达最后一行的末尾,我们只会输入更少的值。这应该只适用于你的最后 x
行。
[mr + [row[-1] for row in matrix[min(i+1, last):min(i+1+grab, last)]]
for (i, mr) in enumerate(matrix)]
导致:
[['a1', 'a2', 'a3', 'a4', 'b4', 'c4', 'd4'],
['b1', 'b2', 'b3', 'b4', 'c4', 'd4', 'e4'],
['c1', 'c2', 'c3', 'c4', 'd4', 'e4'],
['d1', 'd2', 'd3', 'd4', 'e4'],
['e1', 'e2', 'e3', 'e4']]
注意这里使用了 min
因为我们不想超出矩阵的边界,我们不知道它是否是方阵等。除此之外,表达式表示"construct a new row containing the ith row from the original matrix, and add on the last element of each of the rows from min(i+1, last)
to min(i+1+grab, last)
"
我在以下结构中设置了数据
A ,B ,C ,D ,E
1 a1,b1,c1,d1,e1
2 a2,b2,c2,d2,e2
3 a3,b3,c3,d3,e3
4 a4,b4,c4,d4,e4
我想从 E 列下的每一行中提取 x 个字段,以便可以发生以下影响
A ,B ,C ,D ,E
1 a1,b1,c1,d1,e1,e2,e3
2 a2,b2,c2,d2,e2,e3,e4
3 a3,b3,c3,d3,e3,e4,etc
4 a4,b4,c4,d4,e4,etc,etc
我只是使用了 etc,因为我想像我正在尝试做的事情的想法会被接受。
本质上转置数据只是让我把一列的一个区域变成一行,但是转置后我不想删除数据,我只是想复制那些区域,但是我有一个很大的传播 sheet(目前有 1500 行并且还在增加)
虽然我使用的是扩展 sheet,但这只是一个 CSV,如果需要,我很乐意使用各种脚本语言或编程语言,我考虑过 C++,python ,javascript 等等,但我不确定实现此目标的最简单方法是什么。
这基本上可以满足您的要求。首先是一些虚拟数据:
def char_range(c1, c2):
"""Generates the characters from `c1` to `c2`, inclusive."""
for c in xrange(ord(c1), ord(c2)+1):
yield chr(c)
matrix = [[c + str(i) for c in char_range('a', 'e')] for i in range(1, 5)]
这给了我们:
[['a1', 'b1', 'c1', 'd1', 'e1'],
['a2', 'b2', 'c2', 'd2', 'e2'],
['a3', 'b3', 'c3', 'd3', 'e3'],
['a4', 'b4', 'c4', 'd4', 'e4']]
现在一些变量使表达式更易于阅读:
# How many values you want to grab for each row
x = 3
# Length of the last column in the matrix
last = len(matrix)
现在是魔术。我假设,如果我们已经到达最后一行的末尾,我们只会输入更少的值。这应该只适用于你的最后 x
行。
[mr + [row[-1] for row in matrix[min(i+1, last):min(i+1+grab, last)]]
for (i, mr) in enumerate(matrix)]
导致:
[['a1', 'a2', 'a3', 'a4', 'b4', 'c4', 'd4'],
['b1', 'b2', 'b3', 'b4', 'c4', 'd4', 'e4'],
['c1', 'c2', 'c3', 'c4', 'd4', 'e4'],
['d1', 'd2', 'd3', 'd4', 'e4'],
['e1', 'e2', 'e3', 'e4']]
注意这里使用了 min
因为我们不想超出矩阵的边界,我们不知道它是否是方阵等。除此之外,表达式表示"construct a new row containing the ith row from the original matrix, and add on the last element of each of the rows from min(i+1, last)
to min(i+1+grab, last)
"