Python同步遍历多个列表的示例

Python的for循环十分灵活,使用for循环我们可以很轻松地遍历一个列表,例如:

a_list = ['z', 'c', 1, 5, 'm']
for each in a_list:
 print(each)

运行结果:

但是,有时遍历一个列表并不能满足我们的需求,在一些特殊的场合,我们可能会需要遍历两个甚至多个列表,例如,有两个列表,第一个列表存放的是人物的姓名,第二个列表存放的是人物的年纪,他们之间的关系是对应的,这时候该怎么办呢?

①使用zip()函数 (推荐)

name_list = ['张三', '李四', '王五']
age_list = [54, 18, 34]
for name, age in zip(name_list, age_list):
 print(name, ':', age)

运行结果:

下面了解一下zip()函数:

name_list = ['张三', '李四', '王五']
age_list = [54, 18, 34]
print(zip(name_list, age_list))
print(type(zip(name_list, age_list)))
print(*zip(name_list, age_list))
print(list(zip(name_list, age_list)))
print(dict(zip(name_list, age_list)))

运行结果:

可以看出,直接输出zip(list1, list2)返回的是一个zip对象, 在前面加上*, 它输出了三个元组, 正是两个列表中的三个数据一一对应的结果,我们可以将此对象直接转化成列表,甚至字典!

当然,使用zip()来遍历三个及以上的列表也是可行的:

list1 = [1, 2, 3, 4, 5]
list2 = ['a', 'b', 'c', 'd', 'f']
list3 = ['A', 'B', 'C', 'D', 'F']

for number, lowercase, capital in zip(list1, list2, list3):
 print(number, lowercase, capital)

运行结果:

②利用下标

既然列表的内容是一一对应的,我们可以自己设置好一个下标,同样使用一个for循环也可以遍历。

list1 = [1, 2, 3, 4, 5]
list2 = ['a', 'b', 'c', 'd', 'f']

n = 0
for each in list1:
 print(each, list2[n])
 n += 1

运行结果:

同样也得到了我们想要的效果~

以上这篇Python同步遍历多个列表的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 在Python中,不用while和for循环遍历列表的实例

    如下所示: a = [1, 2, 3, 8, 9] def printlist(l, index): if index == len(l): return else: print(l[index]) printlist(l, index + 1) printlist(a, 0) *****for和while循环底层用的是递归实现的 以上这篇在Python中,不用while和for循环遍历列表的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

  • Python 循环语句之 while,for语句详解

    循环语句(有两种): while 语句 for   语句 while 语句: 问题:输入一个整数n,让程序输出n行的: hello 1 hello 2 ....... hell n while 语句: 作用:根据一定条件,重复的执行一条语句或多条语句 语法: while 真值表达式: 语句块1...... else: 语句块2...... 说明: 1,先执行真值表达式,测试布尔值为True或False 2,如果真值表达式的测试值为True,侧执行语句1,然后再返回到第一步重复进行测试 3,如果真

  • Python入门_浅谈for循环、while循环

    Python中有两种循环,分别为:for循环和while循环. 1. for循环 for循环可以用来遍历某一对象(遍历:通俗点说,就是把这个循环中的第一个元素到最后一个元素依次访问一次).for循环的基本结构如下: 具体看这个案例: 设计一个函数,在桌面创建10个文本,用数字从1-10依次给它们命名. def text_create(): path = '/Users/duwangdan/Desktop/' for text_name in range(1,11): # 1-10的范围需要用到r

  • Python中for循环和while循环的基本使用方法

    while循环: while expression: suite_to_repeat while 条件:    语句块 不需要括号哦! >>> x 1.2 >>> while x < 2: print(x) x += 0.2 1.2 1.4 1.5999999999999999 1.7999999999999998 1.9999999999999998 >>> 经常用 : while True: .... if ... : break ....

  • python 循环while和for in简单实例

    python 循环while和for in简单实例 #!/uer/bin/env python # _*_ coding: utf-8 _*_ lucknumber = 5 b = 0 while b <3: print('guss count:',b) a = int(input('you guse number')) if a > lucknumber: print ('youaerbiger') elif a == lucknumber: print ('youare righet')

  • Python 列表(List) 的三种遍历方法实例 详解

    Python 遍历 最近学习python这门语言,感觉到其对自己的工作效率有很大的提升,下面废话不多说,直接贴代码 #!/usr/bin/env python # -*- coding: utf-8 -*- if __name__ == '__main__': list = ['html', 'js', 'css', 'python'] # 方法1 print '遍历列表方法1:' for i in list: print ("序号:%s 值:%s" % (list.index(i)

  • 利用Python循环(包括while&for)各种打印九九乘法表的实例

    一.for循环打印九九乘法表 #注意:由于缩进在浏览器不好控制,请大家见谅,后续会有图片传入. 1.1 左下角 for i in range(1,10): for j in range(1,i+1): print('%d*%d=%2d\t'%(j,i,i*j),end='') print() 效果图: 1.2 右下角 for i in range(1,10): for k in range(i+1,10): print(end=' ') #此处为返回八个空格,请注意 for j in range

  • python 表达式和语句及for、while循环练习实例

    Python中表达式和语句及for.while循环练习 1)表达式 常用的表达式操作符: x + y, x - y x * y, x / y, x // y, x % y 逻辑运算: x or y, x and y, not x 成员关系运算: x in y, x not in y 对象实例测试: x is y, x not is y 比较运算: x < y, x > y, x <= y, x >= y, x == y, x != y 位运算: x | y, x & y,

  • Python同步遍历多个列表的示例

    Python的for循环十分灵活,使用for循环我们可以很轻松地遍历一个列表,例如: a_list = ['z', 'c', 1, 5, 'm'] for each in a_list: print(each) 运行结果: 但是,有时遍历一个列表并不能满足我们的需求,在一些特殊的场合,我们可能会需要遍历两个甚至多个列表,例如,有两个列表,第一个列表存放的是人物的姓名,第二个列表存放的是人物的年纪,他们之间的关系是对应的,这时候该怎么办呢? ①使用zip()函数 (推荐) name_list =

  • python 字典套字典或列表的示例

    文件f1 A 1 a A 1 b A 2 C B 2 a B 2 b 生成如下字典: tdict={'A':{1:['a','b'], 2:['C']}, 'B':{2:['a','b']} } In [22]: tdict={} In [23]: f=open('f1') In [24]: while True: ...: line=f.readline().strip() ...: if not line: ...: break ...: pos1=line.split()[0] ...:

  • python中zip()函数遍历多个列表方法

    在对列表的元素进行找寻时,会频繁的说到遍历的理念.对于复杂的遍历要求,如多个列表中查找就显然不适合用for循环.本篇所要带来的是zip() 函数的方法,能够对多个迭代器进行遍历.下面我们就python中zip的说明.语法.使用注意点进行讲解,然后带来遍历多个列表的实例. 1.说明 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表.(注:在python3中返回的是zip对象) 2.语法 zip(iterable, ...) # 其中 it

  • Python使用zip合并相邻列表项的方法示例

    本文实例讲述了Python使用zip合并相邻列表项的方法.分享给大家供大家参考,具体如下: 1>使用zip()函数和iter()函数,来合并相邻的列表项 >>> x [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> zip(*[iter(x)]*2) [(1, 2), (3, 4), (5, 6), (7, 8)] >>> zip(*[iter(x)]*3) [(1, 2, 3), (4, 5, 6), (7, 8, 9)] >

  • python和pywin32实现窗口查找、遍历和点击的示例代码

    Pywin32是一个Python库,为python提供访问Windows API的扩展,提供了齐全的windows常量.接口.线程以及COM机制等等. 1.通过类名和标题查找窗口句柄,并获得窗口位置和大小 import win32gui import win32api classname = "MozillaWindowClass" titlename = "百度一下,你就知道 - Mozilla Firefox" #获取句柄 hwnd = win32gui.Fin

  • python实现合并两个有序列表的示例代码

    题目描述 将两个升序链表合并为一个新的升序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode原题地址:https://leetcode-cn.com/problems/merge-two-sorted-lists/ 测试用例 示例1 输入:l1 = [1,2,4], l2 = [1,3,4] 输出:[1,1,2,3,4,4] 示例2 输入:l1 = [], l2 = [] 输出:[] 示例3 输入:l1 = [], l2 = [0] 输出:[0] 代码详解 因为Lee

  • Python递归遍历列表及输出的实现方法

    本文实例讲述了Python递归遍历列表及输出的实现方法.分享给大家供大家参考.具体实现方法如下: def dp(s): if isinstance(s,(int,str)): print(s) else: for item in s: dp(item) l=['jack',('tom',23),'rose',(14,55,67)] dp(l) 运行结果如下: jack tom 23 rose 14 55 67 希望本文所述对大家的Python程序设计有所帮助.

  • python 实现交换两个列表元素的位置示例

    在IDLE 中验证如下: >>> numbers = [5, 6, 7] >>> i = 0 >>> numbers[i], numbers[i+1] = numbers[i+1], numbers[i] >>> numbers [6, 5, 7] python是可以一次赋值两个变量的!谢谢! 以上这篇python 实现交换两个列表元素的位置示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

  • Python中遍历列表的方法总结

    Python中遍历列表有以下几种方法: 一.for循环遍历 lists = ["m1", 1900, "m2", 2000] for item in lists: print(item) lists = ["m1", 1900, "m2", 2000] for item in lists: item = 0; print(lists) 运行结果: ['m1', 1900, 'm2', 2000] 二.while循环遍历: li

  • Python创建数字列表的示例

    [一]range()函数 在python中可以使用range()函数来产生一系列数字 for w in range(1,11): print(w) 输出: 1 2 3 4 5 6 7 8 9 10 #注意:这里的到10就结束了,不包括11 [二]list()函数 如果将range()作为参数传递给list()函数,那么就会将数字以列表的形式输出 num=list(range(1,11)) print(num) 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 使用range

随机推荐