Python 使用嵌套的while循环替换列表列表的元素
我想用此函数的输出更改现有嵌套列表的元素Python 使用嵌套的while循环替换列表列表的元素,python,list,while-loop,nested-loops,Python,List,While Loop,Nested Loops,我想用此函数的输出更改现有嵌套列表的元素 listoflist = [[None, None, None],[None, None, None]] def _execute(): user_input = input("type in: ") return user_input 输出应为: output execute() = 1 [[1, None, None],[None, None, None]] output execute() = 4 [[1, 4, None
listoflist = [[None, None, None],[None, None, None]]
def _execute():
user_input = input("type in: ")
return user_input
输出应为:
output execute() = 1
[[1, None, None],[None, None, None]]
output execute() = 4
[[1, 4, None],[None, None, None]]
任务是不使用新列表
listoflist = [[None, None, None],[None, None, None]]
# id(..) shows the in memory address of an object
print(f'starting address listoflist {id(listoflist)}')
# Test routine placing result in dummy_list
dummy_list = insertdata(listoflist, _execute)
print(f'ending address listoflist {id(listoflist)}')
print(f'dummy list address {id(dummy_list)}')
print(f'listof list: {listoflist}')
print(f'dummy_list: {dummy_list}')
starting address listoflist 139769341482560
type in: 1
type in: 2
type in: 3
type in: 4
type in: 5
type in: 6
ending address listoflist 139769341482560
dummy list address 139769341482560
listof_list: [['1', '2', '3'], ['4', '5', '6']]
dummy_list: [['1', '2', '3'], ['4', '5', '6']]
insertdata
的预期行为应为
数据从def\u execute():
传递到insertdata
列表中的现有值将被逐个替换,直到每个值都被替换为止
我的aproach是一个嵌套的while循环,嵌套列表的行和列有两个计数器:
主while循环启动第二个while循环
在第二个while循环中,第一行的值被替换
如果是这样的话,该操作应处理到下一行
def insertdata(data):
row_index = 0
while row_index != len(listoflist):
# inner loop
data_added = False
n = len(listoflist[row_index])
index = 0
while not data_added and index != n:
if listoflist[row_index][index] is None:
listoflist[row_index][index] = data
data_added = True
else:
index += 1
# gets not executed
if index == n:
row_index += 1
break
正确的行为是第一个传入值替换列表中所有现有值
所以看起来第二个循环没有重新启动来逐个替换每个值
我错过了什么
完整示例:
"""
tasks
"""
listoflist = [[None, None, None],[None, None, None]]
def _execute():
user_input = input("type in: ")
return user_input
def insertdata(data):
row_index = 0
while row_index != len(listoflist):
# inner loop
data_added = False
n = len(listoflist[row_index])
index = 0
while not data_added and index != n:
if listoflist[row_index][index] is None:
listoflist[row_index][index] = data
data_added = True
else:
index += 1
# gets not executed
if index == n:
row_index += 1
break
while True:
insertdata(_execute())
print(listoflist)
为什么不在数组上迭代并将数据放入
for i in range(len(listoflist)):
for j in range(len(listoflist[i]):
listoflist[i][j] = data
这可以更简单地通过以下方式实现:
def _execute():
user_input = input("type in: ")
return user_input
def insertdata(lst, get_data):
""" Inplace replacement of data in list
using function get_data """
for row in range(len(lst)):
for column in range(len(lst[row])):
# Replacing items 1 by 1
# using function get_data
lst[row][column] = get_data()
return lst
测试
listoflist = [[None, None, None],[None, None, None]]
# id(..) shows the in memory address of an object
print(f'starting address listoflist {id(listoflist)}')
# Test routine placing result in dummy_list
dummy_list = insertdata(listoflist, _execute)
print(f'ending address listoflist {id(listoflist)}')
print(f'dummy list address {id(dummy_list)}')
print(f'listof list: {listoflist}')
print(f'dummy_list: {dummy_list}')
starting address listoflist 139769341482560
type in: 1
type in: 2
type in: 3
type in: 4
type in: 5
type in: 6
ending address listoflist 139769341482560
dummy list address 139769341482560
listof_list: [['1', '2', '3'], ['4', '5', '6']]
dummy_list: [['1', '2', '3'], ['4', '5', '6']]
输出
listoflist = [[None, None, None],[None, None, None]]
# id(..) shows the in memory address of an object
print(f'starting address listoflist {id(listoflist)}')
# Test routine placing result in dummy_list
dummy_list = insertdata(listoflist, _execute)
print(f'ending address listoflist {id(listoflist)}')
print(f'dummy list address {id(dummy_list)}')
print(f'listof list: {listoflist}')
print(f'dummy_list: {dummy_list}')
starting address listoflist 139769341482560
type in: 1
type in: 2
type in: 3
type in: 4
type in: 5
type in: 6
ending address listoflist 139769341482560
dummy list address 139769341482560
listof_list: [['1', '2', '3'], ['4', '5', '6']]
dummy_list: [['1', '2', '3'], ['4', '5', '6']]
我们看到listoflist具有相同的结束和开始地址(139769341482560),因此我们只是对列表进行了变异,而不是创建一个新的列表
虚拟列表与listoflist的地址相同,因此我们返回了相同的列表。我看到您编辑了您的问题。现在看来,您不希望数组中的所有值都相同,而是希望逐个输入它们。如果是这样,您可以使用xbello方法