前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 列表

python 列表

作者头像
用户5760343
发布2019-07-31 16:01:42
1.7K0
发布2019-07-31 16:01:42
举报
文章被收录于专栏:sktj

[ord(s) for s in symbols if ord(s) > 127 ]

list(filter(lamba: c: c>127,map(ord,symbols)))

列表推导性能:

result

listcomp : 0.044 0.043 0.040 0.037 0.035 listcomp + func : 0.056 0.056 0.056 0.055 0.054 filter + lambda : 0.048 0.049 0.052 0.068 0.035 filter + func : 0.027 0.030 0.041 0.042 0.043

import timeit

TIMES = 10000

SETUP = """ symbols = '$¢£¥€¤' def non_ascii(c): return c > 127 """

def clock(label, cmd): res = timeit.repeat(cmd, setup=SETUP, number=TIMES) print(label, *('{:.3f}'.format(x) for x in res))

clock('listcomp :', '[ord(s) for s in symbols if ord(s) > 127]') clock('listcomp + func :', '[ord(s) for s in symbols if non_ascii(ord(s))]') clock('filter + lambda :', 'list(filter(lambda c: c > 127, map(ord, symbols)))') clock('filter + func :', 'list(filter(non_ascii, map(ord, symbols)))')

元组拆包 *a =x,y

image.png

image.png

image.png

元组类

from collections import namedtuple City=namedtuple('City','name country population coordination') tokyo=City('Tokyo''JP',36.93,(35.5,129))

image.png

切片

image.png

多维切片

[1:3,2:2]

排序

list.sort不会新建一个列表 sorted 会新建一个列表

image.png

二分法

bisect

数组

from array import array floats=array('d',(random() for i in range(1000000000)))

image.png

image.png

内存视图memoryview

image.png

numpy 处理矩阵

image.png

image.png

双向队列

image.png

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.07.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • [ord(s) for s in symbols if ord(s) > 127 ]
  • 列表推导性能:
  • result
  • 元组拆包 *a =x,y
  • 元组类
  • 切片
  • 多维切片
  • 排序
  • 二分法
  • 数组
  • 内存视图memoryview
  • numpy 处理矩阵
  • 双向队列
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档