在python中的列表列表中插入值
我现在的列表有问题 假设我有以下信息:在python中的列表列表中插入值,python,list,nested-lists,Python,List,Nested Lists,我现在的列表有问题 假设我有以下信息: path = [['B', 'C', 'A', 'A', 'B'],['C', 'B'], 'A'] index = [0, 3, 5, 6, 8, 9, 11, 14] 这个数据意味着在时间指数=0时,我的观察值是B。在时间指数=3时,我的观察值是C,依此类推。应该注意的是,我希望将数据结构保持为列表列表,因为这意味着在时间=0到时间=14的整个过程中,我有3组不同的观察值。第一次观察是从时间0到时间9,依此类推 我实际上想做的是完成列表的列表。也就是
path = [['B', 'C', 'A', 'A', 'B'],['C', 'B'], 'A']
index = [0, 3, 5, 6, 8, 9, 11, 14]
这个数据意味着在时间指数=0时,我的观察值是B。在时间指数=3时,我的观察值是C,依此类推。应该注意的是,我希望将数据结构保持为列表列表,因为这意味着在时间=0到时间=14的整个过程中,我有3组不同的观察值。第一次观察是从时间0到时间9,依此类推
我实际上想做的是完成列表的列表。也就是说,我想在列表(路径)的列表中添加我的观察结果。对于所有缺少的时间(1,2,4,7…在索引中不存在),我想将这个特定的观察添加到我的列表(路径)列表中。这一观察结果与先前的观察结果相似。因此,在时间t=1时,我的观察值将是B(时间t=0时的观察值)。在时间=2时,我的观察值为B(时间=1时的观察值),依此类推
我希望从中得到的最终结果是:
complete_path = [['B', 'B', 'B', 'C', 'C', 'A', 'A', 'A', 'B'], ['C', 'C', 'B', 'B','B'],'A']
如果是一个列表,我想我可以解决这个问题,但我发现对于列表列表来说这很难。谢谢你的好意。祝你今天愉快
编辑:
此问题还有两个条件:
1.有时,路径
可能是常规列表,而不是嵌套列表。因此,我们需要注意到这种可能性。
2.如果最后一个观察值只是一个值,它将生成一个字符串而不是列表。除此之外,路径
将始终是嵌套列表
下面是我创建的工作代码。它看起来真的很混乱,我希望任何人都能告诉我是否有可能以更好的方式/更少的代码行解决这个问题
def complete_list(list_, index, max_index = None):
if max_index == None:
max_index = max(index)
complete_list = [None]*((max_index + 1) - min(index))
complete_list[0] = list_.pop(0)
for true_index in range(min(index) + 1, max_index + 1):
if true_index in index:
complete_list[true_index - min(index)] = list_.pop(0)
else:
complete_list[true_index - min(index)] = complete_list[true_index - min(index) - 1]
return complete_list
def is_lists_of_list(lists):
return np.any([isinstance(y, list) for y in lists])
def get_first_index(lists_of_list):
list_first_index = [0]*len(lists_of_list)
for i in range(1,len(lists_of_list)):
if isinstance(lists_of_list, list):
list_first_index[i] = len(lists_of_list[i-1]) + list_first_index[i-1]
return list_first_index
def complete_lists_of_list(lists_of_list, index, max_index):
result = []
n_lists = len(lists_of_list)
list_first_index = [index[x] for x in get_first_index(lists_of_list)]
for i in range(n_lists - 1):
used_index = [ x for x in index if (x >= list_first_index[i]) and (x < list_first_index[i+1])]
tmp_result = complete_list(list(lists_of_list[i]), used_index, max_index=list_first_index[i+1] - 1)
result.append(tmp_result)
if isinstance(lists_of_list[-1],list):
used_index = [x for x in index if x >= list_first_index[n_lists] and x <= max_index]
tmp_result = complete_list(list(lists_of_list[n_lists]), used_index)
result.append(tmp_result)
else:
tmp_result = [lists_of_list[-1]] * (max_index - index[-1] + 1)
result.append(tmp_result)
return result
def smoothen_path(object, index, max_index):
if is_lists_of_list(object):
return complete_lists_of_list(object, index, max_index)
else:
return complete_list(object, index, max_index)
def complete_list(列表、索引、最大索引=无):
如果max_index==无:
最大索引=最大(索引)
完整列表=[无]*((最大索引+1)-最小索引))
完整列表[0]=列表弹出(0)
对于范围内的真实索引(最小(索引)+1,最大索引+1):
如果为true,则索引中的索引为:
完整列表[真实索引-最小(索引)]=列表弹出(0)
其他:
完整列表[真实索引-最小(索引)]=完整列表[真实索引-最小(索引)-1]
返回完整的列表
def是列表中的列表(列表):
返回np.any([isinstance(y,list)表示列表中的y])
def get_first_索引(列表中的列表):
列表第一个索引=[0]*len(列表的列表)
对于范围内的i(1,len(列表中的列表)):
如果存在(列表中的列表,列表):
列表第一索引[i]=len(列表[i-1]中的列表)+列表第一索引[i-1]
返回列表\u第一个\u索引
def完整列表(列表、索引、最大索引):
结果=[]
n\u list=len(列表中的列表)
list_first_index=[get_first_index(lists_of_list)中x的索引[x]
对于范围内的i(n_列表-1):
使用的索引=[x表示索引中的x,如果(x>=list\u first\u index[i])和(x
used_index=[x for x in index if x>=list_first_index[n_list]和x您可以定义一个递归函数,计算从迭代器重复当前元素的频率,并将同一迭代器传递给用于嵌套列表的递归调用
def repeat(path, repeats):
return [x for y in path for x in
([repeat(y, repeats)] if isinstance(y, list) else [y] * next(repeats))]
要创建repeats
迭代器,可以使用zip
获取索引中的连续元素对,但需要添加另一个元素,以重复最后一个元素的频率(即结果列表及其子列表的总长度)
对于这个例子,res
将是[['B','B','B','C','C','A','A','B'],['C','C','B','B','B'],'A','A']
到目前为止,您做了什么?0努力=没有帮助在为索引13添加条目时,该项目应该在最后一个A
之前的外部列表中,还是在B
之后的第二个内部列表的末尾?@cᴏʟᴅsᴘᴇᴇᴅ “那个复制品,虽然有一个非常相似的标题,但似乎是关于一个完全不同的问题。”tobias_k仔细检查后,我确实匆忙地关闭了它。如果OP可以进一步说明他们所做的事情以及他们的错误所在,这将成为一个合理的问题。目前来看,这个问题太广泛了……请随意重新打开。
path = [['B', 'C', 'A', 'A', 'B'],['C', 'B'], 'A']
index = [0, 3, 5, 6, 8, 9, 11, 14]
index += [16] # end of list
repeats = (b-a for (a,b) in zip(index, index[1:]))
res = repeat(path, repeats)