Python:操纵列表

Python:操纵列表,python,list,python-3.x,Python,List,Python 3.x,问题 我必须从一个文本文件到一个列表,对角线,从上到下。它应该适用于letters.txt的任何维度。该文件如下所示: 文本文件:letters.txt(虽然这很难,但我从我原来的帖子中删除了“Y”和“Z” 列表应如下所示: topButtom_List = ['AGMS', 'BHNT', 'CIOU', 'DJPV', 'EKQW', 'FLRX'] bLeftToURight = ['A', 'GB', 'MHC', 'SNID', 'TOJE', 'UPKF', 'VQL', 'WR'

问题

我必须从一个文本文件到一个列表,对角线,从上到下。它应该适用于
letters.txt
的任何维度。该文件如下所示:

文本文件:letters.txt(虽然这很难,但我从我原来的帖子中删除了“Y”和“Z”

列表应如下所示:

topButtom_List = ['AGMS', 'BHNT', 'CIOU', 'DJPV', 'EKQW', 'FLRX']

bLeftToURight = ['A', 'GB', 'MHC', 'SNID', 'TOJE', 'UPKF', 'VQL', 'WR', 'X']
# top to buttom
topButtom_List = [] #should be ['AGMS', 'BHNT', 'CIOU', 'DJPV', 'EKQW', 'FLRX']

openFile = open("letters.txt")
for i in openFile:
    i = i.replace(" ","")
length = len(i)
openFile.close()

openFile = open("letters.txt")   
counter = 0   
for eachIterration in range(length):
    for line in openFile:
        line = line.replace(" ","")
        # counter should be added by 1 each time inner loop itterates x4, and outter loop x1.
        topButtom_List.append(line[counter]) 
    counter = counter + 1
openFile.close()
从上到下的当前代码:

topButtom_List = ['AGMS', 'BHNT', 'CIOU', 'DJPV', 'EKQW', 'FLRX']

bLeftToURight = ['A', 'GB', 'MHC', 'SNID', 'TOJE', 'UPKF', 'VQL', 'WR', 'X']
# top to buttom
topButtom_List = [] #should be ['AGMS', 'BHNT', 'CIOU', 'DJPV', 'EKQW', 'FLRX']

openFile = open("letters.txt")
for i in openFile:
    i = i.replace(" ","")
length = len(i)
openFile.close()

openFile = open("letters.txt")   
counter = 0   
for eachIterration in range(length):
    for line in openFile:
        line = line.replace(" ","")
        # counter should be added by 1 each time inner loop itterates x4, and outter loop x1.
        topButtom_List.append(line[counter]) 
    counter = counter + 1
openFile.close()
我试图用上面的代码做什么:

topButtom_List = ['AGMS', 'BHNT', 'CIOU', 'DJPV', 'EKQW', 'FLRX']

bLeftToURight = ['A', 'GB', 'MHC', 'SNID', 'TOJE', 'UPKF', 'VQL', 'WR', 'X']
# top to buttom
topButtom_List = [] #should be ['AGMS', 'BHNT', 'CIOU', 'DJPV', 'EKQW', 'FLRX']

openFile = open("letters.txt")
for i in openFile:
    i = i.replace(" ","")
length = len(i)
openFile.close()

openFile = open("letters.txt")   
counter = 0   
for eachIterration in range(length):
    for line in openFile:
        line = line.replace(" ","")
        # counter should be added by 1 each time inner loop itterates x4, and outter loop x1.
        topButtom_List.append(line[counter]) 
    counter = counter + 1
openFile.close()
我试图从文本文件中获取从顶部到按钮的字符,并将其放入名为
topButtom_list
的列表中。我使用计数器定义索引,对于外部循环所做的每一次迭代,索引将被添加1。依我看,外部循环将启动,内部循环将在
topButtom_List
在第一次迭代时,外循环将再次迭代并向计数器添加1。
BHNTZ
将在第二次迭代时添加,依此类推,外循环将再次迭代并向计数器添加1

从文本文件:letters.txt 我想填充
topButtom\u列表

我得到的输出:

topButtom_List = ['AGMS', 'BHNT', 'CIOU', 'DJPV', 'EKQW', 'FLRX']

bLeftToURight = ['A', 'GB', 'MHC', 'SNID', 'TOJE', 'UPKF', 'VQL', 'WR', 'X']
# top to buttom
topButtom_List = [] #should be ['AGMS', 'BHNT', 'CIOU', 'DJPV', 'EKQW', 'FLRX']

openFile = open("letters.txt")
for i in openFile:
    i = i.replace(" ","")
length = len(i)
openFile.close()

openFile = open("letters.txt")   
counter = 0   
for eachIterration in range(length):
    for line in openFile:
        line = line.replace(" ","")
        # counter should be added by 1 each time inner loop itterates x4, and outter loop x1.
        topButtom_List.append(line[counter]) 
    counter = counter + 1
openFile.close()
预期输出:

topButtom_List = ['AGMS', 'BHNT', 'CIOU', 'DJPV', 'EKQW', 'FLRX']

bLeftToURight = ['A', 'GB', 'MHC', 'SNID', 'TOJE', 'UPKF', 'VQL', 'WR', 'X']
# top to buttom
topButtom_List = [] #should be ['AGMS', 'BHNT', 'CIOU', 'DJPV', 'EKQW', 'FLRX']

openFile = open("letters.txt")
for i in openFile:
    i = i.replace(" ","")
length = len(i)
openFile.close()

openFile = open("letters.txt")   
counter = 0   
for eachIterration in range(length):
    for line in openFile:
        line = line.replace(" ","")
        # counter should be added by 1 each time inner loop itterates x4, and outter loop x1.
        topButtom_List.append(line[counter]) 
    counter = counter + 1
openFile.close()


有趣的问题!试着自己解决它,用你所缺少的东西发布你的尝试来完成它,我们很乐意帮助:)添加代码。我不知道我该怎么问,我的帖子总是投反对票。我修正了投票,但是仅仅发布你的代码对我们没有帮助。你还需要解释什么不适用于它,并给出输入和输出的示例…纠正了一些变量你只是问如何做向上/向下或向左/向右谢谢!那很有用!有没有办法做到不间断?嗨,我的代码有问题,如果我在你的代码中的字段变量上使用不同维度的文本,它只会得到其中的一部分,其他的只会在列表上打印一个空格。