Python-从列表列表中删除列表(类似于.pop()的功能) [英] Python - Remove list(s) from list of lists (Similar functionality to .pop() )

查看:278
本文介绍了Python-从列表列表中删除列表(类似于.pop()的功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a=[[1,2,3],[4,5,6],[7,8,9]]

.pop()不仅可以删除列表中的元素,还可以返回该元素.

.pop() has the capacity to not only remove an element of a list but also return that element.

我正在寻找一个类似的函数,该函数可以删除并返回可能存在于另一个列表中间的整个列表.

I am looking for a similar function that can remove and return a whole list that could exist in the middle of another list.

例如,有一个函数将从上面的列表a中删除[4,5,6],并将其返回.

E.g is there a function that will remove [4,5,6] from the above list a, and return it.

该问题的原因是我正在通过itemgetter对列表进行排序,并且标题行(字符串)与其余数据(datetime)之间存在冲突.因此,我希望有效地弹出代表标题的列表,进行排序,然后再将其插入.

The reason for the question is that I'm sorting a list through itemgetter and there's a collision between the headings row (string) and the rest of the data (datetime). As such, I'm looking to effectively pop the list which represents the headings, do a sort, then insert it back in.

推荐答案

嵌套列表只是外部列表中的值.只需在外部列表上使用.pop():

The nested lists are just values in the outer list. Just use .pop() on that outer list:

inner_list = a.pop(1)

演示:

>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> a.pop(1)
[4, 5, 6]
>>> a
[[1, 2, 3], [7, 8, 9]]

如果标题行妨碍您的使用,则可以使用切片将第一行从考虑中删除:

You could just use a slice to remove the first row from consideration if a header row is in the way:

result = rows[:1] + sorted(rows[1:], key=itemgetter(1))

这篇关于Python-从列表列表中删除列表(类似于.pop()的功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆