切片具有不同长度的子列表

编程入门 行业动态 更新时间:2024-10-05 21:25:26
本文介绍了切片具有不同长度的子列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个清单清单.每个子列表的长度在1到100之间.每个子列表在一组数据中的不同时间包含一个粒子ID.我想在给定的时间形成所有粒子ID的列表.为此,我可以使用类似的东西:

I have a list of lists. Each sublist has a length that varies between 1 and 100. Each sublist contains a particle ID at different times in a set of data. I would like to form lists of all particle IDs at a given time. To do this I could use something like:

list = [[1,2,3,4,5],[2,6,7,8],[1,3,6,7,8]] list2 = [item[0] for item in list]

list2将包含列表中每个子列表的首个元素.我不仅要对第一个元素执行此操作,还要对1到100之间的每个元素执行此操作.我的问题是,每个子列表都不存在元素号100(或66或77或其他).

list2 would contain the first elements of each sublist in list. I would like to do this operation not just for the first element, but for every element between 1 and 100. My problem is that element number 100 (or 66 or 77 or whatever) does not exists for every sublist.

是否存在创建列表列表的方法,其中每个子列表都是给定时间所有粒子ID的列表.

Is there some way of creating a lists of lists, where each sublist is the list of all particle IDs at a given time.

我已经考虑过尝试使用numpy数组来解决此问题,好像列表的长度都一样,这将是微不足道的.我尝试将-1加到每个列表的末尾以使它们的长度相同,然后掩盖负数,但是到目前为止,这对我没有用.我将在给定的时间使用ID列表来切片另一个单独的数组:

I have thought about trying to use numpy arrays to solve this problem, as if the lists were all the same length this would be trivial. I have tried adding -1's to the end of each list to make them all the same length, and then masking the negative numbers, but this hasn't worked for me so far. I will use the list of IDs at a given time to slice another separate array:

pos = pos[satIDs]

推荐答案

lst = [[1,2,3,4,5],[2,6,7,8],[1,3,6,7,8]] func = lambda x: [line[x] for line in lst if len(line) > x] func(3) [4, 8, 7] func(4) [5, 8]

-更新-

func = lambda x: [ (line[x],i) for i,line in enumerate(lst) if len(line) > x] func(4) [(5, 0), (8, 2)]

更多推荐

切片具有不同长度的子列表

本文发布于:2023-11-29 15:33:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646808.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:切片   长度   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!