简体   繁体   English

Python:使用列表索引号作为键并使用列表元素作为值来创建字典吗?

[英]Python: Create dictionary with list index number as key and list element as value?

All the questions I've seen do the exact opposite of what I want to do: 我所看到的所有问题与我想做的事情完全相反:

Say I have a list: 说我有一个清单:

lst = ['a','b','c']

I am looking to make a dictionary where the key is the element number (starting with 1 instead of 0) and the list element is the value. 我正在寻找一个字典,其中的键是元素编号(以1而不是0开头),而list元素是值。 Like this: 像这样:

{1:'a', 2:'b', 3:'c'}

But for a long list. 但名单很长。 I've read a little about enumerate() but everything I've seen has used the list element as the key instead. 我已经阅读了一些有关enumerate()的内容,但是我所看到的所有内容都使用list元素作为键。

I found this: 我找到了这个:

dict = {tuple(key): idx for idx, key in enumerate(lst)}

But that produces: 但这会产生:

{'a':1, 'b':2, 'c':3}

... which is the opposite of what I want. ...这与我想要的相反。 And, also in a weird notation that is confusing to someone new to Python. 而且,这也是一种怪异的表示法,它使刚接触Python的人感到困惑。

Advice is much appreciated! 意见非常感谢! Thanks! 谢谢!

enumerate has a start keyword argument so you can count from whatever number you want. enumerate有一个start关键字参数,因此您可以从所需的任何数字start计数。 Then just pass that to dict 然后将其传递给dict

dict(enumerate(lst, start=1))

You could also write a dictionary comprehension 您还可以编写字典理解

{index: x for index, x in enumerate(lst, start=1)}

Using Dict Comprehension and enumerate 使用Dict理解和枚举

print({x:y for x,y in enumerate(lst,1)})

{1: 'a', 2: 'b', 3: 'c'} {1:“ a”,2:“ b”,3:“ c”}

Using Dict Comprehension , zip and range- 使用Dict Comprehension,zip和range-

print({x:y for x,y in zip(range(1,len(lst)+1),lst)})

{1: 'a', 2: 'b', 3: 'c'} {1:“ a”,2:“ b”,3:“ c”}

By default enumerate start from 0 , but you can set by this value by second argument which is start , You can add +1 to every iterator if you want to start from 1 instead of zero : 默认情况下,枚举从0开始,但是您可以通过第二个参数start来设置此值,如果要从1开始而不是从零开始,则可以向每个迭代器添加+1:

print({index+1:value for index,value in enumerate(lst)})

output: 输出:

{1: 'a', 2: 'b', 3: 'c'}

Above dict comprehension is same as : 上面的dict理解与:

dict_1={}
for index,value in enumerate(lst):
    dict_1[index+1]=value
print(dict_1)

I think the below code should help. 我认为以下代码应该有所帮助。

my_list = ['A', 'B', 'C', 'D']
my_index = []
my_dict = {}

for i in range(len(my_list)): 
    my_index.append(i+1)

for key in my_index: 
    for value in my_list: 
        my_dict[key] = value

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:将列表转换成字典,其中key是列表元素的索引 - Python: Convert list to a dictionary, where key is the index of the element of the list 如何在列表中查找字典键值的索引? (蟒蛇) - How to find index of a dictionary key value within a list? (python) 如何通过将键分配给 Python 字典中的每个值元素来创建元组列表? - How to create list of tuple by distributing the key to every value element inside a Python dictionary? 如何在python中使用列表索引号访问字典值? - How to access a dictionary value with a list index number in python? 访问给定键的列表值的第n个元素:Python字典 - Accessing nth Element of a list value for a given key: Python dictionary Python-用字典中的键值替换列表中元素的最佳方法 - Python - Best way to replace an element in a list with a key value from a dictionary 如何将 2 元素列表作为键/值对添加到 python 字典中? - How to add a 2-element list to a python dictionary as a key/value pair? 获取字符串子列表的第一个元素作为字典键,其中包含 python 中的值列表 - Get first element of a string sublist as a dictionary key with a list of value in python 如何在 Python 中的字典列表中使用另一个键列表中的值在字典中创建一个新键? - How to create a new key in dictionary with value from another key list from a list of dictionary in Python? Python从具有key:value字符串的列表中创建字典 - Python create dictionary from a list that has key:value strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM