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
为什么我这样做......
print(type(new_list[0][0]))
它将类型返回为字符串?我需要它像我分配它一样的int。当我打印出new_list时。它没有显示内容为[" 0"," 0",....等]它将它们显示为[0,0,0 ......等]他们必须是整数。我在这里想念的是什么感谢。
进入函数(myList)的参数I看起来像这样......
['10 20', '1 1', '10 20', '-----------------------------------------', '| | | | | | | | |', '|-+ +-+-+ + + + + + +-+ + + +-+-+ + + +-|'...etc
我想去
new_list[0][0] = 3
但它不会让我!?
答案 0 :(得分:-3)
myList,你的输入有字符串值。
myList = ['| | | | | | | | |', '|-+ +-+-+ + + + + + +-+ + + +-+-+ + + +-|']
您将列表转换为2d列表。
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
我不知道您可能拥有二进制数据的确切输入。如果是这种情况,那么你必须相应地编码/解码。
我没有遇到你的问题。
print(type(new_list[0][0]))
<class "int">
因此您获得了意外的输入值。
您可以尝试强制使用int。
int(new_list[0][0])
如果您需要有关正在打印的内容的更多信息,请使用repr方法。
print(repr(new_list[0][0]))
像Jeff Langemeier建议的那样列举。
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