如果我有两个向量Vec({'a', 'b', 'c'}, {'a':0})
和Vec({'a', 'b', 'c'}, {'b':0})
,我怎样才能轻松创建一个for循环来通过a和b?另外,我想在不导入任何内容的情况下这样做。
我目前的错误代码是这样的:
for x in b.f and a.f:
print(x)
a
当我真的想要打印时:
a
b
答案 0 :(得分:1)
如果您使用的是Python 3.3 +:
def chain(*xss):
for xs in xss:
yield from xs
for x in chain(['a', 'b'], ['c', 'd']):
print(x)
或者:
def chain(*xss):
for xs in xss:
for x in xs:
yield x
for x in chain(['a', 'b'], ['c', 'd']):
print(x)