python Python中的字典或列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python中的字典或列表相关的知识,希望对你有一定的参考价值。

# It is made up of values with a unique key for each of them.

# It is not possible to join dictionaries the way lists can be joined.

# Example: military alphabet

military_alphabet = {'A':'alpha', 'B': 'bravo', 'C': 'charlie', 'D':'delta', 'E':'echo', 'F':'foxtrot', 'G':'golf',
                     'H':'hotel', 'I':'india', 'J':'juliet', 'K':'kilo', 'L':'lima', 'M':'mike', 'N':'november',
                     'O':'oscar', 'P':'papa', 'Q':'quebec', 'R':'romeo', 'S':'sierra', 'T':'tango', 'U':'uniform',
                     'V':'victor', 'W':'whiskey', 'X':'x-ray', 'Y':'yankee', 'Z':'zulu'}

# Get one item from the dictionary
print(military_alphabet['A'])

# Delete an entry
del military_alphabet['A']

# Replace an item in the dictionary
military_alphabet['R'] = 'rurtubia'

#Find the number of items or length for the dicionary
len(military_alphabet)
print('There are ',len(military_alphabet),'letters in the English alphabet... because I just eliminated one!')

#Find the value of an index by passing its key
print("The value of \'X\' is", end=' ')
print(military_alphabet.get("X"))

#Obtain a list of the keys in the dictionary
military_alphabet.keys()
print('\n',military_alphabet.keys())

#Obtain a list of the values in the dictionary
military_alphabet.values()
print(military_alphabet.values())

以上是关于python Python中的字典或列表的主要内容,如果未能解决你的问题,请参考以下文章

Python字典中的值为列表或字典的构造方法

对比Python中的列表元组字典集合字符串等之间异同

[python] 字典和列表中的pop()函数

Python中元组,列表,字典的区别

Python中的字典

python数据类型——列表元组字典集合