简体   繁体   English

如何使用列表的元素来设置字典的键和值?

[英]How do I use elements of list to set the key and value of dictionary?

I have to use element 0 of words as a dictionary key and set the value of to_nato for that key to words element 1.我必须使用words的元素 0 作为字典键,并将该键的to_nato的值设置为words元素 1。

I have this:我有这个:

natofile = "nato-alphabet.txt"
to_nato = {} #creates empty string
fh = open(natofile) #opens natofile

for line in fh: 
    clean = line.strip()
    lowerl = clean.lower()
    words = lowerl.split()
    to_nato = {words[0]:words[1]}
    print(to_nato)

nato-alphabet is a text file that looks like this: nato-alphabet 是一个文本文件,如下所示:

A Alfa
B Bravo
C Charlie
D Delta
E Echo
F Foxtrot
G Golf
H Hotel
I India

My code returns a list of dictionaries instead one dictionary.我的代码返回一个字典列表而不是一个字典。

Directly set the key value with dict_object[key] = value :直接用dict_object[key] = value设置键值:

to_nato[words[0]] = words[1]

This can be written more concisely using the dict constructor and a generator expression.这可以使用dict构造函数和生成器表达式更简洁地编写。

to_nato = dict(line.lower().split() for line in fh)

Try this:尝试这个:

natofile = "nato-alphabet.txt"
to_nato = {} #creates empty string
fh = open(natofile) #opens natofile

for line in fh: 
    clean = line.strip()
    lowerl = clean.lower()
    words = lowerl.split()
    to_nato[words[0]] = words[1]

fh.close()

print(to_nato)

This sets the element of to_nato with key words[0] to value words[1] for each pair in the file.这将文件中每一对的to_nato元素的关键字words[0]设置为值words[1]

dict() can convert any list of pairs of values into a dict dict() 可以将任何值对列表转换为字典

lines=open('nato-alphabet.txt').read().lower().splitlines()
lines = [line.strip().split() for line in lines]
my_dict=dict(lines)

暂无
暂无

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

相关问题 如何在条件语句中使用嵌套在列表中的字典键值对的值? - How do I use the value of a key-value pair of a dictionary nested inside a list in a conditional statement? 如何使用字典键,值对来设置类实例属性“pythonic”? - How do I use dictionary key,value pair to set class instance attributes “pythonic”ly? 如何使列表成为字典中的键值? - How do I make a list a key value in a dictionary? 如何更改列表中字典中特定键的值类型? - How do I change the value type of a particular key in a dictionary in a list? 如何按键长度对字典进行排序,然后按值列表中的元素排序? - How do I sort a dictionary by key length, then by an element in the value list? 如何使用Python的pprint模块打印出嵌套在列表中的字典键值对? - How do I use Python's pprint module to pretty print out dictionary key value pairs nested with in a list? 如何使用python从列表中获取字典,将列表条目作为键,并将列表中的项数作为值? - How do I obtain a dictionary from a list, with list entrys as key and the amount of the items in the list as value, using python? 如何删除具有相同特定键值的字典元素列表 - How to remove the list of dictionary elements with the same specific key-value 我如何将key:value字典中的项数设置为python中的键值 - How do it I set the number of items in a key:value dictionary as the keys value in python 如何将字典的值设置为对另一个字典的值的引用? - How do I set a value of a dictionary to a reference to a value of another dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM