[英]Reading from a file diagonally and having the diagonal characters create a list
I need to read a file containing information on different lines - for example the file may contain 我需要读取一个包含不同行信息的文件-例如,该文件可能包含
12345678910
abcdefghij
zyxwvutsrq
I will then need to read the code diagonally so my list would be: 然后,我将需要对角阅读代码,因此我的列表将是:
(1bx)(2cw)(3dv)
I have tried using zip and just can't figure out a way to get it to work. 我曾尝试使用zip,但无法找到一种使其正常工作的方法。
EDIT 编辑
Is there anyway to also make it take into account the diagonals before the top left corner for example: 无论如何,是否还要考虑到左上角之前的对角线,例如:
(ay)(z)
in the example file I used. 在我使用的示例文件中。
Edit 2: this is my almost complete code 编辑2:这是我几乎完整的代码
with open(FileName) as diagonal :
a = diagonal.read().splitlines()
l = [a[i][i:] for i in range(len(a))]
Diaglist = [''.join(i) for i in zip(*l)]
with open(FileName) as diagonal1 :
b = diagonal1.read().splitlines()
o = [b[i][:i] for i in range(len(b))]
Diaglist1 = [''.join(i) for i in zip(*o)]
When I run the file I get the correct diagonals for the first with so from the top right to left but the second with so from the top right downwards I just get an empty list. 当我运行文件时,我得到的第一个正确的对角线是从右上到左,而第二个正确的对角线是从右上向下。
Do you mean: 你的意思是:
>>> with open('file') as f:
... l = f.read().splitlines()
>>> l
['12345678910', 'abcdefghij', 'zyxwvutsrq']
>>> l = [l[0]] + [l[1][1:]] + [l[2][2:]] # remove 'a' from `l[1]` and `zy` from `l[2]`
>>> l
['12345678910', 'bcdefghij', 'xwvutsrq']
>>> list(zip(*l)) # zip them
[('1', 'b', 'x'), ('2', 'c', 'w'), ('3', 'd', 'v'), ('4', 'e', 'u'), ('5', 'f', 't'), ('6', 'g', 's'), ('7', 'h', 'r'), ('8', 'i', 'q')]
>>> [''.join(i) for i in list(zip(*l))] # also join them
['1bx', '2cw', '3dv', '4eu', '5ft', '6gs', '7hr', '8iq']
>>>
If you don't know how many lines in your file, we can use some magic code like [a[i][i:] for i in range(len(a))]
. 如果您不知道文件中有多少行,我们可以
[a[i][i:] for i in range(len(a))]
使用一些魔术代码,例如[a[i][i:] for i in range(len(a))]
。
Try: 尝试:
with open('file') as f:
a = f.read().splitlines()
l = [a[i][i:] for i in range(len(a))]
final_list = [''.join(i) for i in zip(*l)]
print(final_list)
As your edit, you can change a[i][i:]
to a[i][:i]
. 在进行编辑时,可以将
a[i][i:]
更改为a[i][:i]
。 Very simple: 很简单:
with open('file') as f:
a = f.read().splitlines()
l = [a[i][:i] for i in range(len(a))]
final_list = [''.join(i) for i in zip(*l)][1:] # since the first element in the list will be empty (`''`), remove it.
print(final_list)
The following will work for an arbitrary number of lines of the same length , and wraps the final diagonals . 以下内容适用于任意长度的相同长度的行,并包装最终的对角线 。 This may not be what you want.
这可能不是您想要的。
def diagonals(lines):
size = len(lines[0])
positions = [[(i + x) % size for x in range(len(lines))]
for i in range(size)]
return ["".join([lines[i][p] for i, p in enumerate(posn)])
for posn in positions]
>>> print(diagonals(['1234567891', 'abcdefghij', 'zyxwvutsrq']))
['1bx', '2cw', '3dv', '4eu', '5ft', '6gs', '7hr', '8iq', '9jz', '1ay']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.