enumerate - 迭代一个列表的index和item

《Python Cookbook》(Recipe 4.4)描述了如何使用enumerate迭代item和index。

例子如下:

alist = ['a1', 'a2', 'a3']

for i, a in enumerate(alist):
print(i, a)

结果如下:

0 a1
1 a2
2 a3

zip - 同时迭代两个列表

我之前用zip写过同时迭代两个列表的代码.

例子如下:

alist = ['a1', 'a2', 'a3']
blist = ['b1', 'b2', 'b3'] for a, b in zip(alist, blist):
print(a, b)

结果如下:

a1 b1
a2 b2
a3 b3

enumerate 和 zip 结合使用

下面是展示如何使用 enumerate和zip, 迭代两个列表和他们的index。

alist = ['a1', 'a2', 'a3']
blist = ['b1', 'b2', 'b3'] for i, (a, b) in enumerate(zip(alist, blist)):
print(i, a, b)

结果如下:

0 a1 b1
1 a2 b2
2 a3 b3

[译]在Python中如何使用额enumerate 和 zip 来迭代两个列表和它们的index?的更多相关文章

  1. 让 Python 的1、数据库查询返回字典记录--- 2、利用zip函数将两个列表(list)组成字典(dict)

    让 Python 的数据库查询返回字典记录: https://yanbin.blog/python-database-query-return-dictionary-result/#more-9179 ...

  2. Python专题之详解enumerate和zip

    enumerate 第一个是枚举函数. 在我们的日常编程过程中,我们经常遇到一个问题. 在C语言和一些古老的语言中没有迭代器的概念,所以当我们想要遍历数组或容器时,我们只能使用下标.使用迭代器,我们的 ...

  3. Python面试题之Python中的lambda map filter reduce zip

    当年龟叔想把上面列出来的这些都干掉.在 “All Things Pythonic: The fate of reduce() in Python 3000”这篇文章中,他给出了自己要移除lambda. ...

  4. 【译】Python中如何创建mock?

    原文地址:http://engineroom.trackmaven.com/blog/making-a-mockery-of-python/ 今天我们来谈论下mock的使用.当然,请不要误会,这里的m ...

  5. [译]在python中如何有效的比较两个无序的列表是否包含完全同样的元素(不是set)?

    原文来源: https://stackoverflow.com/questions/7828867/how-to-efficiently-compare-two-unordered-lists-not ...

  6. [译]在Python中,如何拆分字符串并保留分隔符?

    原文来源:https://stackoverflow.com/questions/2136556/in-python-how-do-i-split-a-string-and-keep-the-sepa ...

  7. python中使用heapq查看最大与最小的N个元素列表

    怎么从一个集合中获取最大或最小的N个元素列表? heapq模块有两个函数:nlargest() 和 nsmallest() 可以完美解决这个问题. In [39]: import heapq In [ ...

  8. python中的map,filter,zip函数

    map() Return an iterator that applies function to every item of iterable, yielding the results 例如: a ...

  9. Python中的函数递归思想,以及对比迭代和递归解决Fibonacci数列

    什么是递归?简单的说就是:函数自身调用自身. “普通程序员用迭代,天才程序员用递归” 虽然递归 在运行时会不断出栈压栈,调用底层的寄存器,造成空间上的占用以及时间上的缓慢, 但在一些算法上面仍然是递归 ...

随机推荐

  1. JDBC连接数据库时错误提示的解决方案汇总

    今天在连接JDBC时,出现了错误 最开始的URL是这样写的 Connection conn = DriverManager.getConnection("jdbc:mysql://local ...

  2. order by 排序

    [order by] 排序 asc 升序(从小到大),desc降序(从打到小) 语法: select  列名  from  表  where  条件  order by  列1,列2    asc或d ...

  3. 【2018 ICPC焦作网络赛 G】Give Candies(费马小定理+快速幂取模)

    There are N children in kindergarten. Miss Li bought them N candies. To make the process more intere ...

  4. HTTP基本内容

    *********************HTTP基本交互*************************** HTTP请求格式:HTTP 请求由三部分组成:请求行.请求头和请求正文请求行: 请求方 ...

  5. HTML基础全荟

    第一讲 html概述 1.认识HTML <! DOCTYPE html> <html> <style></style> <head>< ...

  6. JZOJ 5941. 乘

    Sample Input Sample Input1: 4 3 9 6 5 8 7 7 Sample Output Sample Output1: 0做法(转自JZOJ):考虑 a 是定值, 而 b ...

  7. hadoop2.5.0 HA高可用配置

    hadoop2.5.0 HA配置 1.修改hadoop中的配置文件 进入/usr/local/src/hadoop-2.5.0-cdh5.3.6/etc/hadoop目录,修改hadoop-env.s ...

  8. AIM Tech Round 5C. Rectangles 思维

    C. Rectangles time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. 004---Python基本数据类型--元祖

    元祖 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px ...

  10. UVA - 12230

    #include <bits/stdc++.h> using namespace std; int n; double d; double p,l,v,ret,sum; ; /* 村庄A, ...