交替循环A/B字符 [英] Loop character A/B alternately

查看:80
本文介绍了交替循环A/B字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python循环中交替打印A/B字符?

How can I print A/B character alternately in python loop?

我期望得到的结果:

oneA
twoB
threeA
fourB
...

推荐答案

您可以使用 zip 一起使用,以遍历更长的列表,同时重复较短的那一个.例如

You can use itertools.cycle to repeat through a sequence. This is typically used with zip to iterate through a longer list, while repeating the shorter one. For example

import itertools
for i,j in zip(['one', 'two', 'three', 'four'], itertools.cycle('AB')):
    print(i+j)

输出

oneA
twoB
threeA
fourB

这篇关于交替循环A/B字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆