gll89的个人博客分享 http://blog.sciencenet.cn/u/gll89

博文

python operations about list

已有 2048 次阅读 2017-5-13 03:36 |个人分类:Python|系统分类:科研笔记

I. access multi elements of a list knowing their indices

You can use operator.itemgetter:

>>>from operator import itemgetter

>>> a =[-2,1,5,3,8,5,6]

>>> b =[1,2,5]

>>>print itemgetter(*b)(a)(1,5,5)

Or you can use numpy:

>>>import numpy as np

>>> a = np.array([-2,1,5,3,8,5,6])

>>> b =[1,2,5]>>>print list(a[b])[1,5,5]

Or you can

>>>a =[-2,1,5,3,8,5,6]

>>>b =[1,2,5]

>>>c =[ a[i]for i in b]


II.Get the difference set of two lists

>>>list(set(temp1)- set(temp2))







https://blog.sciencenet.cn/blog-1969089-1054641.html

上一篇:video recognition using attention visual
下一篇:SSH Commands
收藏 IP: 128.227.206.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-7-27 04:24

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部