python的map和zip操作
来源:互联网 发布:java log4j 多线程 编辑:程序博客网 时间:2024/06/02 05:19
如果要将一个string list转换成int list (list里每个string都转成int),比如
['0','1','2'] -> [0,1,2]
可以使用:
[int(x) for x in list]
或者使用map操作: map(func, list) 对list里的每个元素apply func.
map(int, list)
假设有一个2维数组(用list实现):
list = [[0,1,2],[3,1,4]]
如果要得到每行之和,可以用以下两种方式:
>>> list = [[0,1,2],[3,1,4]]>>> [sum(x) for x in list][3, 8]>>> map(sum,list)[3, 8]
如果要得到每列之和,需要用zip(*list)先unzip list,得到一个元组list,其中第i个元组包含了每行的第i个元素:
>>> list = [[0,1,2],[3,1,4]]>>> zip(*list)[(0, 3), (1, 1), (2, 4)]>>> [sum(x) for x in zip(*list)][3, 2, 6]>>> map(sum,zip(*list))[3, 2, 6]
下面的例子是关于zip和unzip(其实是zip和*一起用)如何work的:
>>> x=[1,2,3]>>> y=[4,5,6]>>> zipped = zip(x,y)>>> zipped[(1, 4), (2, 5), (3, 6)]>>> x2,y2=zip(*zipped)>>> x2(1, 2, 3)>>> y2(4, 5, 6)>>> x3,y3=map(list,zip(*zipped))>>> x3[1, 2, 3]>>> y3[4, 5, 6]
- python的map和zip操作
- Python基础 5----python的map和zip操作
- map()和zip()操作
- python的map,reduce和zip
- Python:map,filter和zip
- python学习笔记之map和zip的使用
- Python中zip和map函数
- python学习笔记--zip()和map()
- python的filter,map,zip,reduce,apply
- Python的 enumerate zip map reduce filter
- python zip map
- python zip(), map()
- python中的zip与map和filter与reduce函数的区别
- Python中zip,filter,map和推导式列表的用法
- Python 之 zip,map,lambda
- Python基础-zip lambda map
- Python checkio Transposed Matrix map与zip的使用
- python的lambda、filter、map、reduce、zip函数
- 一个简单的找数方法
- java初始代码块
- js判断浏览器语言
- VRRP协议介绍
- CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别 2010-11-8 23:10
- python的map和zip操作
- C++基础题目
- url 2010-11-8 23:23
- 经典书目
- java跨平台
- SQL集合函数中case when then 使用技巧
- ios 字符串
- android 多线程断点续传下载 三
- MFC在状态栏实时显示系统时间