V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  THsoul  ›  全部回复第 1 页 / 共 1 页
回复总数  1
2018-08-05 17:45:20 +08:00
回复了 Laccuse 创建的主题 Python 请问 Python 如何实现两个数字一行,三个数字一行交替输出?
获取到 4 位数中能被 3 整除不能被 6 整除的数
list = filter(None, [i if(i%3 is 0 and i%6 is not 0) else None for i in range(1000, 10000)])

写一个返回指定长度并从元列表中剔除这些元素的方法
def push_num(list, reqlen):
count = 0
req = []
while count is not reqlen:
req.append(list[0])
del list[0]
count += 1
return req

再写一个交替判断
def list_slice(list):
req = []
req.append(push_num(list, 2))
while True:
if len(list) is 0:
return req
if len(req[-1]) is 3:
req.append(push_num(list, 2))
elif len(req[-1]) is 2:
req.append(push_num(list, 3))

最后 结合一下
list_slice( filter(None, [i if(i%3 is 0 and i%6 is not 0) else None for i in range(1000, 10000)]) )
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5358 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 12ms · UTC 07:29 · PVG 15:29 · LAX 23:29 · JFK 02:29
Developed with CodeLauncher
♥ Do have faith in what you're doing.