gpt4 book ai didi

Python 同时递增两个字符串

转载 作者:太空狗 更新时间:2023-10-30 00:30:18 26 4
gpt4 key购买 nike

我希望能够同时交互 list1 = list('asdf')list2 = list('qwer')。什么是最好的方法?

for i, p in list1, list2:
print(i,p)

i 将递增 list1p 将递增 list2

最佳答案

使用zip (或 itertools.izip 如果两个列表很大):

for i, p in zip(list1, list2):
print(i, p)

或者,如果 list1 的长度可能与 list2 的长度不同,请使用 izip_longest from itertools

关于Python 同时递增两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12186624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com