python中 列表.pop什么意思,Python-从列表列表中删除列表(与.pop()相似的功能)...

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

.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.

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

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.

解决方案

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

inner_list = a.pop(1)

Demo:

>>> 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))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值