THsoul 最近的时间轴更新
THsoul

THsoul

V2EX 第 190317 号会员,加入于 2016-09-04 11:43:44 +08:00
THsoul 最近回复了
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   ·   实用小工具   ·   4079 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms · UTC 05:26 · PVG 13:26 · LAX 21:26 · JFK 00:26
Developed with CodeLauncher
♥ Do have faith in what you're doing.