Python将排序后的List转换为Dict,其位置指定为(键,值)对

时间:2021-03-20 19:28:00

Just new to Python, what's the best way to convert a sorted list (yes, elements in list are unique):

刚接触Python,转换排序列表的最佳方法是什么(是的,列表中的元素是唯一的):

[a0, a1, a2, a3, ..., aj, ... ]

to a Dict data type where position looks like following:

到Dict数据类型,其位置如下所示:

{
    a0: {'index':0},
    a1: {'index':1},
    a2: {'index':2},
    ...
    aj: {'index':j},
    ...
}

Just clarify some concerns here:

请在此澄清一些问题:

  • There are actually more pair in the dict such as {'index': j, 'name': wow, ...} and convert from list to such dict is necessary, the other properties, such as 'name' will be added after the dict been create, so basically it looks like following, first create the dict, and second based on the key aj adding other properties, other properties come later;
  • 在dict中实际上有更多的对,例如{'index':j,'name':哇,...}并且从列表转换为这样的dict是必要的,其他属性,例如'name'将在之后添加dict一直在创建,所以基本上它看起来像跟随,首先创建dict,第二个基于键aj添加其他属性,其他属性后来;

  • Explicitly define the index is necessary, it finally will looks like: {'index': myFunc(j)}.
  • 明确定义索引是必要的,它最终将看起来像:{'index':myFunc(j)}。

Deeply appreciate your help!

非常感谢您的帮助!


What I have tried:

我尝试过的:

  1. tried l = zip(mylist, range(len(mylist))) and convert l (which looks like [(a0, 0), (a1, 1), ...]) to dict, however, it's list with tuple inside;
  2. 尝试l = zip(mylist,range(len(mylist)))并将l(看起来像[(a0,0),(a1,1),...])转换为dict,但是,它的列表里面有元组;

  3. tried d = dict(zip(mylist, range(mylist.len))) but still need convert {ai: i} into {ai:{'index': i}} and don't know the brilliant method to solve from here;
  4. 试过d = dict(zip(mylist,range(mylist.len)))但仍然需要将{ai:i}转换成{ai:{'index':i}}并且不知道从这里解决的好方法;

  5. tried naive for loop which works, but not happen with that
  6. 尝试天真的循环有效,但没有发生

1 个解决方案

#1


Using Dict comprehension (one-liner; if you didn't have the two concerns):

使用Dict理解(单行;如果你没有这两个问题):

result = {key: {"index": index} for index, key in enumerate(yourList)}

You can use it like:

您可以像以下一样使用它:

>>> yourList = range(10)
>>> result = {key: {"index": index} for index, key in enumerate(yourList)}
>>> result
{0: {'index': 0}, 1: {'index': 1}, 2: {'index': 2}, 3: {'index': 3}, 4: {'index': 4}, 5: {'index': 5}, 6: {'index': 6}, 7: {'index': 7}, 8: {'index': 8}, 9: {'index': 9}}

For a solution that accounts for those two bullets, I'd suggest something like the below:

对于解释这两个子弹的解决方案,我建议如下:

result = {}
for index, item in enumerate(yourList):
    currentDict = {"name": "wow", .. all your other properties .. }
    currentDict["index"] = index #Or may be myFunc(index)
    result[item] = currentDict

NOTE: I hope you are using hashable items in your original list.

注意:我希望您在原始列表中使用可清洗项目。

#1


Using Dict comprehension (one-liner; if you didn't have the two concerns):

使用Dict理解(单行;如果你没有这两个问题):

result = {key: {"index": index} for index, key in enumerate(yourList)}

You can use it like:

您可以像以下一样使用它:

>>> yourList = range(10)
>>> result = {key: {"index": index} for index, key in enumerate(yourList)}
>>> result
{0: {'index': 0}, 1: {'index': 1}, 2: {'index': 2}, 3: {'index': 3}, 4: {'index': 4}, 5: {'index': 5}, 6: {'index': 6}, 7: {'index': 7}, 8: {'index': 8}, 9: {'index': 9}}

For a solution that accounts for those two bullets, I'd suggest something like the below:

对于解释这两个子弹的解决方案,我建议如下:

result = {}
for index, item in enumerate(yourList):
    currentDict = {"name": "wow", .. all your other properties .. }
    currentDict["index"] = index #Or may be myFunc(index)
    result[item] = currentDict

NOTE: I hope you are using hashable items in your original list.

注意:我希望您在原始列表中使用可清洗项目。