gpt4 book ai didi

python - 我如何在Python中将嵌套列表作为字典值进行迭代和排序?

转载 作者:行者123 更新时间:2023-12-02 19:19:55 24 4
gpt4 key购买 nike

我有一本这样的字典:

dict =  {'x': [(['a'], ['b'], ['c'], ['d'], ['e'], ['f'])],'y': [(['a'], ['b'], ['c'], ['d'], ['e'], ['f'])]}

我想迭代嵌套列表并向列表添加一个数字。结果应该像下面的字典一样:

res = {'x': [(['a',1], ['b',2], ['c',3], ['d',4], ['e',5], ['f',6])]}

如何在嵌套列表中添加数字?

最佳答案

>>> dct =  {'x': [(['a'], ['b'], ['c'], ['d'], ['e'], ['f'])]}
>>> {k:[tuple([c for c in l]+[i+1] for i,l in enumerate(t)) for t in v ] for k,v in dct.items()}
{'x': [(['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5], ['f', 6])]}

关于python - 我如何在Python中将嵌套列表作为字典值进行迭代和排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63202292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com