Python 在字典中循环

Python 在字典中循环,python,dictionary,Python,Dictionary,我正在做一个拉丁方块拼图,我正在做代码来打开一个文本文件并将数据存储在字典中。然而,当我循环代码时,它会重复打印出来。 例如,在我的文本文档中 ABC CAB BCA 当我运行代码时,我希望输出是我得到的结果 ABC ABCCAB ABCCABBCA 我目前的代码是: d={} while True: try: filename=input("Enter the name of the file to open: ") + ".txt" with o

我正在做一个拉丁方块拼图,我正在做代码来打开一个文本文件并将数据存储在字典中。然而,当我循环代码时,它会重复打印出来。 例如,在我的文本文档中

ABC
CAB
BCA
当我运行代码时,我希望输出是我得到的结果

ABC
ABCCAB
ABCCABBCA 
我目前的代码是:

d={}
while True:
    try:
        filename=input("Enter the name of the file to open: ") + ".txt"
        with open(filename,"r") as f:
            for line in f:
                splitLine=line.split()
                d[splitLine[0]]=splitLine[1:]
                #print(line, end="")
                print(d)
            break

    except FileNotFoundError:
        print("File name does not exist; please try again")
我需要做什么来停止打印上面的行。我认为这与:

d[splitLine[0]]=splitLine[1:]

但我不知道如何解决这个问题,将你的电路板存储在二维列表中更有意义:

with open(filename, 'r') as f:
    board = [list(line) for line in f.read().splitlines()]

for row in board:
    print(row)
输出:

['A', 'B', 'C']
['C', 'A', 'B']
['B', 'C', 'A']
然后可以根据其坐标拾取一个值:

board[0][0] # top left 'A'
board[2][2] # bottom right 'A'

正如其他人所指出的,对于游戏板来说,列表可能是更好的选择。您可以这样做:

d = []
while True:
    try:
        filename = 'foo.txt'
        with open(filename, "r") as f:
            for line in f:
                d.append(list(line.strip()))
            break

    except FileNotFoundError:
        print("File name does not exist; please try again")

print(d)

for line in d:
    print(''.join(line))
这将产生:

[['A', 'B', 'C'], ['C', 'A', 'B'], ['B', 'C', 'A']]
ABC
CAB
BCA

for循环打印出来的内容与您在

中读取的内容完全相同。您指出问题的地方是正确的

您需要清空变量:

d
每次你循环

例如:

d={}
while True:
    try:
        filename=input("Enter the name of the file to open: ") + ".txt"
        with open(filename,"r") as f:
            for line in f:
                splitLine=line.split()
                d[splitLine[0]]=splitLine[1:]
                print(line, end="")
                d={}
            break

    except FileNotFoundError:
        print("File name does not exist; please try again")

首先,你最好只是把它存储为一个列表(或列表列表),而不是把它变成一个字典

d=[]
while True:
    try:
        #filename=input("Enter the name of the file to open: ") + ".txt"
        with open("file.txt","r") as f:
            for line in f:
                splitLine=line.split()
                d.extend(splitLine)
                #print(line, end="")

            break

    except FileNotFoundError:
        print("File name does not exist; please try again")

for elem in d:
        print elem 
如果您使用这样的列表结构,然后最后使用循环遍历所有元素,您将获得所需的输出

输出:

ABC
CAB
BCA

你能举个例子说明你想要的是什么样子吗?为什么你用字典来做游戏板而不是2D列表?@m_callens呃,我只是用我熟悉的东西。我应该把它做成一个列表,然后打印出来吗?@Garrett R嗯,我希望它打印出来的方式与它存储在文本文件中的方式相同,所以简单地说是ABC,然后是新行CAB,然后是新行BCA@Xrin:但您正在将其存储为字典。因此,如果您删除代码中的打印(d)并将其放在所有行之后,您将获得字典,而不会重复它。但是你会得到一对值;你能解释一下
d.append(list(line.strip())
部分吗?我知道追加会在列表“d”的末尾添加一个列表(行);但是这条带子到底在做什么呢?当你读入文件中的每一行时,在这一行的末尾会有一个换行符。调用
list('ABC\n')
将给出
['A','B','C','\n']
。要删除换行符,可以使用删除多余的空格。