python zip函数/np.where找到满足条件的元素位置/numpy与list转换/eval

一、zip()
描述
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。
实例
以下实例展示了 zip 的使用方法:

a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]
zipped = zip(a,b)     # 打包为元组的列表
print zipped
print zip(a,c)              # 元素个数与最短的列表一致
print zip(*zipped)          # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
print zip(zipped)          # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
'''
输出
[(1, 4), (2, 5), (3, 6)]
[(1, 4), (2, 5), (3, 6)]
[(1, 2, 3), (4, 5, 6)]
[((1, 4),), ((2, 5),), ((3, 6),)]'''

二、np.where()

#coding: utf-8
import numpy as np
l = [[34,465, 56, 4, 7, 8, 3],[34,465, 56, 4, 400, 8, 3]]
l = np.array(l)
print l
a = np.where(l>300)
print a
#输出为(array([0, 1, 1]), array([1, 1, 4]))

三、numpy与list转换

a=[3.234,34,3.777,6.33]

a为python的list类型

将a转化为numpy的array:

a = np.array(a)
print a
结果:array([ 3.234, 34. , 3.777, 6.33 ])

将a转化为python的list

a.tolist()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值