我试图将'methuselahs
'翻译成二进制代码。所有的点('。')应该变为0,并且所有的O('O')应该变为1.更改列表中的列表的值
我目前有一个可以工作的代码,但它只会返回第一个list_of_lists列表。
list_of_lists = [['.O'],['...O'],['OO..OOO']]
def read_file(list_of_lists):
"""Reads a file and returns a 2D list containing the pattern.
O = Alive
. = Dead
"""
end_list = []
sub_list = []
for A_List in list_of_lists:
for A_String in A_List:
for item in A_String:
#Adding 0's and 1's to sub_list when given the right input
if item == '.':
sub_list.append(0)
elif item == 'O':
sub_list.append(1)
#Adding the sub
end_list.append(sub_list)
return end_list
输出:
[[0,1]]
但预期输出:
[[0,1],[0,0,0,1],[1,1,0,0,1,1,1]]
有谁知道我可以让代码更改所有列表,而不仅仅是第一个?
非常感谢您!我已经搞了两个小时,现在我明白我做错了什么。也感谢您的快速回复! –