如何将每个小写的符号与数字中的每个符号配对?
lowercase = 'abcdef'
digits = '012'
以下返回生成器:
(((i + (lambda x: y)(digits)) for y in digits) for i in lowercase)
我发现的是 list()或 [] 在生成器周围应该返回正确的内容,但以下都没有工作过:
print(list((((i + (lambda x: y)(digits)) for y in digits) for i in lowercase)))
print(list(((i + (lambda x: y)(digits)) for y in digits) for i in lowercase))
print(list((i + (lambda x: y)(digits)) for y in digits) for i in lowercase)
print(list(i + (lambda x: y)(digits) for y in digits) for i in lowercase)
print([(i + (lambda x: y)(digits) for y in digits)] for i in lowercase)
[<generator object <genexpr>.<genexpr> at 0x03329D80>, <generator object <genexpr>.<genexpr> at 0x03329DB0>, <generator object <genexpr>.<genexpr> at 0x03329DE0>, <generator object <genexpr>.<genexpr> at 0x03329E10>, <generator object <genexpr>.<genexpr> at 0x03329E40>, <generator object <genexpr>.<genexpr> at 0x03329E70>]
[<generator object <genexpr>.<genexpr> at 0x03329D80>, <generator object <genexpr>.<genexpr> at 0x03329DB0>, <generator object <genexpr>.<genexpr> at 0x03329DE0>, <generator object <genexpr>.<genexpr> at 0x03329E10>, <generator object <genexpr>.<genexpr> at 0x03329E40>, <generator object <genexpr>.<genexpr> at 0x03329E70>]
<generator object <genexpr> at 0x01605E70>
<generator object <genexpr> at 0x01605E70>
<generator object <genexpr> at 0x01605E70>
答案 0 :(得分:1)
如果我理解正确的话,我想你想要的是什么
lowercase = 'abcdef'
digits = '012'
[letter+digit for letter in lowercase for digit in digits]
>>>['a0', 'a1', 'a2', 'b0', 'b1', 'b2', 'c0', 'c1', 'c2', 'd0', 'd1', 'd2', 'e0', 'e1', 'e2', 'f0', 'f1', 'f2']
答案 1 :(得分:0)
圆括号包围的列表推导创建一个生成器,方括号创建一个列表: 顺便说一句。你可以使用字符串格式重写lambda表达式:
>>> lowercase = 'abcdef'
>>> digits = '012'
>>>
>>> ["%s%s" % (c, d) for c in lowercase for d in digits]
['a0', 'a1', 'a2', 'b0', 'b1', 'b2', 'c0', 'c1', 'c2', 'd0', 'd1', 'd2', 'e0', 'e1', 'e2', 'f0', 'f1', 'f2']
>>> ("%s%s" % (c, d) for c in lowercase for d in digits)
<generator object <genexpr> at 0x7f0d558b64b0>