开发者

List comprehension from multiple sources in Python?

Is it possible to replace the following with a list comprehension?

res = []
for a, _, c in myList:
    for i in c:
        res.append((a, i))

For example:

# Input
myList = [("Foo", None, [1, 2, 3]), ("Bar开发者_StackOverflow", None, ["i", "j"])]

# Output
res = [("Foo", 1), ("Foo", 2), ("Foo", 3), ("Bar", "i"), ("Bar", "j")]


>>> [(i, j) for i, _, k in myList for j in k]
[('Foo', 1), ('Foo', 2), ('Foo', 3), ('Bar', 'i'), ('Bar', 'j')]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜