新手Python列表。Int';标准普尔;串

新手Python列表。Int';标准普尔;串,python,list,Python,List,为什么当我这么做的时候 def changeList(myList): myList = myList[3:] new_list = [] count = -1 for line in myList: count += 1 new_list += [list(myList[count])] count1 = -1 for i in new_list: count1 += 1

为什么当我这么做的时候

def changeList(myList):
    myList = myList[3:]
    new_list = []

    count = -1
    for line in myList:
        count += 1
        new_list += [list(myList[count])]

    count1 = -1
    for i in new_list:

         count1 += 1
         count2 = -1

         for j in i:

             count2 += 1

             if j == "-":
                 new_list[count1][count2] = 0 #- 0 - Can't go there.
             elif j == "|":
                 new_list[count1][count2] = 0 #| 0 - Can't go there.
             elif j == "+":
                 new_list[count1][count2] = 0 #+ 0 - Can't go there.
             elif j == " ":
                 new_list[count1][count2] = 1 #Blank - 1 for empty cell.

    for i in new_list:

       print(i)

    return new_list
它以字符串形式返回类型?我需要它是一个整数,就像我分配的一样。当我打印新的_列表时。它不会将内容显示为[“0”、“0”、…等]而是将其显示为[0,0,0…等]它们必须是整数。我错过了什么。谢谢

我在函数(myList)中输入的参数如下所示

print(type(new_list[0][0]))
我想去

['10 20', '1 1', '10 20', '-----------------------------------------', '|     |       | | |       |     | |     |', '|-+ +-+-+ + + + + + +-+ + + +-+-+ + + +-|'...etc

但它不会让我

myList,您的输入具有字符串值

new_list[0][0] = 3
可以将列表转换为二维列表

myList = ['|     |       | | |       |     | |     |', '|-+ +-+-+ + + + + + +-+ + + +-+-+ + + +-|']
现在我们循环将字符串值更改为整数

new_list = [list(i) for i in myList]
我不知道你的确切输入,你可能有二进制数据。如果这是您的情况,那么您必须相应地编码/解码

我没有经历过你的问题

count1 = -1
for i in new_list:
    count1 += 1
    count2 = -1

    for j in i:

        count2 += 1
        if j == "-":
            new_list[count1][count2] = 0 #- 0 - Can't go there.
        elif j == "|":
            new_list[count1][count2] = 0 #| 0 - Can't go there.
        elif j == "+":
            new_list[count1][count2] = 0 #+ 0 - Can't go there.
        elif j == " ":
            new_list[count1][count2] = 1 #Blank - 1 for empty cell.
         # else: # Currently you don't have an else, so that makes me guess that you are getting a value you are not expecting.
         else:
             print("ERROR: THERE IS AN ISSUE HERE")
             new_list[count1][count2] = -1
如果需要有关打印内容的更多信息,请使用repr方法

int(new_list[0][0])
像Jeff Langemeier建议的枚举

print(repr(new_list[0][0]))

首先修复缩进-不可能读取该代码,然后显示如何调用函数-即,您将向itI传递哪些数据。我修复了您要求的内容。因此,我复制列表类型生成“str”的唯一方法是在转换为int之前放置打印(类型(my_list[0][0])。同样,“不允许您”在哪里将新的_列表[0][0]设置为3,因为这可能表明另外一个问题。此外,就像代码审阅类型一样,您确实在将一些事情放在一起,我建议您研究for循环的枚举函数(这将使它们更pythonic,更易于阅读/调试)。查看列表理解,以构建新列表的第一关,同样是可读性和pythonic-ness。是的,您打印的类型在哪里,作业中出现了什么错误?我想op是在询问打印(类型(新列表[0][0])这就是为什么我回答时会提示repr和int转换。我还解释了他并没有改变输入列表中的每一个值,这可能就是为什么会有字符串值。我看不出这有什么关系。OP试图打印的是
类型
,而不是值本身加上OP统计,他可以在打印列表时将列表中的值视为
Int
。@Himal OP特别询问如何获取整数。“我需要它是一个int,就像我分配给它一样。”他的打印可能是从字符串中打印一个字符。我不能肯定他在印刷方面的问题。我只是想解决他的问题,得到他想要的整数。我对答案进行了编辑,以解释他试图像列表一样循环遍历字符串。此外,你的回答完全违背了问题的直觉。您在回答时考虑了my_列表中的前3个值,而OP显然会将它们从工作列表中删除。
print(repr(new_list[0][0]))
for i, li in enumerate(new_list):
    for j, item in enumerate(li):
        if item == "-":
            li[j] = 0 #- 0 - Can't go there.
        elif item == "|":
            li[j] = 0 #| 0 - Can't go there.
        elif item == "+":
            li[j] = 0 #+ 0 - Can't go there.
        elif item == " ":
            li[j] = 1 #Blank - 1 for empty cell.
        else:
            print("ERROR")
            li[j] = -1