1

假设我有两个元组,比如说:

l = (water, lily , 6)
m = (history , book, 5)

我想将其转换为具有 2 个键和一个值的字典。

dict = {(water,lily): 6} {(history, book) : 5}

对于python中的多行元组。

我将如何做到这一点?

4

1 回答 1

2

您可以使用列表理解。

l = [('water', 'lily' , 6), ('history' , 'book', 5)]
x = {(one, two): three for one, two, three in l}
print(x) # Prints {('water', 'lilly'): 6, ('history', 'book'): 5)}
于 2019-04-09T01:46:58.327 回答