我想处理两种生成器结果。e、 g:
import random
def gen():
for _ in range(10):
i=random.randint(0,100)
if i%2:
yield '_' + str(i)
else:
yield str(i)
def test():
for i in gen():
if i.startwith('_'):
process1()
else:
process2()
除了if/else,还有没有更好的方法来处理test()中的两种结果?像不同种类的发生器和不同的接收器
Tags:
返回一个
tuple
,包含任意多的位和片段生成过程函数而不是字符串:
注意我删除了这些字符串,因为原始代码没有使用这些字符串。如果使用该字符串,则像@gnibbler那样传递/接收一对process函数和tuple字符串
相关问题 更多 >
编程相关推荐