Python字典怎么样

时间:2015-04-09 13:30:03

标签: python python-3.x ipython

我试图在python中创建一个字典,如果我输入CS101 我将收到数据室号3004,讲师海恩斯,会议时间是上午8:00。我不是100%肯定如何让讲师,房间号和会议时间到来。这就是我到目前为止所做的。

def get_menu_choice():
    print('Course number, and their value')
    print('-------------------------------')
    print('1. Look up the key CS101')
    print('2. Quit the program')
    print()
    #understand what the user wants
    choice = int(input('Enter your choice: '))

    # make sure it gives you the correct information

    while choice < look_up or choice > Quit:
        choice = int(input( 'Enter a valid choice: '))

    # return the choice you picked
    return choice

不完全确定从这里开始的地方......或在哪里放入我的数据。我希望能够输入CS101,并且有房间号,会议时间和指导员出现

3 个答案:

答案 0 :(得分:1)

python中的字典只是一组key - &gt; value对;

"CS101" -> "Library, Colonel Mustard, Candlestick"
"CS102" -> "Conservatory, Reverend Green, Revolver"
"CS103" -> "Ballroom, Professor Plum, Lead Pipe"

如何存储keysvalues由您决定,您可以将它们设置为strings,如上所述,或者您可以将它们存储为tuples / { {1}}或自定义arrays - 它取决于您。

在python中,您使用objects语法创建一个字典 - 它在标准库文档here中有详细说明。

您还可以使用{key: value, key: value, ...}赋值语法(将覆盖同一个键的旧值)向字典中添加新的对象。

例如,将之前的内容转换为此my_dictionary[key] = value格式;

dict

然后您可以像这样访问字典中的条目:

course_plan = dict() # create a new empty dictionary
course_plan = {}     # or we can do it like this.

course_plan["CS101"] = "Library, Colonel Mustard, Candlestick"
course_plan["CS102"] = "Conservatory, Reverend Green, Revolver"
course_plan["CS103"] = "Ballroom, Professor Plum, Lead Pipe"

所以你需要像这样创建一个字典,然后在你的函数内部访问它。

当然你可以稍微进一步;字典中的print course_plan["CS101"] # This prints: "Library, Colonel Mustard, Candlestick" 可以是value / arrays数据;

tuples

或者在另一个答案中,它们可能是包含一些元数据的词典;

course_plan["CS101"] = ["Library", "Colonel Mustard", "Candlestick"]
course_plan["CS101"] = ("Library", "Colonel Mustard", "Candlestick")

或者您确实可以创建自己的类来存储数据,并且可以很好地替换它;

course_plan["CS101"] = {"location": "Library", "suspect": "Colonel Mustard", "weapon": "Candlestick"}

答案 1 :(得分:0)

实现此目的的一种轻量级方法是使用嵌套字典:

d = {}

d['CS101'] = {'room number': 3004, 'instructor': 'Haynes', 'time': '8:00 am'}

print d
{'CS101': {'instructor': 'Haynes', 'time': '8:00 am', 'room number': 3004}}

print d['CS101']['time']
Out[4]: '8:00 am'

然后你只需要打印字典中的每个项目。你也可以使用一个类,但在进入classes / OOP之前我会先处理字典。

答案 2 :(得分:0)

最好阅读Python基础词典教程。

如何创建词典:

使用dict()创建空字典

>>> d = dict()
>>> type(d)
<type 'dict'>

>>> d1 = {}
>>> type(d1)
<type 'dict'>

分配并获取键和值

>>> d = {"Key1":"Value1"}
>>> d
{'Key1': 'Value1'}
>>> d["Key2"] = "Value2"
>>> d
{'Key2': 'Value2', 'Key1': 'Value1'}
>>> 

具有键值的词典是词典

以下演示是为了获取用户的密钥,房间号和会议时间。

<强>码

result = {}
u_key = raw_input("Enter key:")
room_no = raw_input("Enter room number:")
meeting_time = raw_input("Meeting time:")
result[u_key] = {"room_number":room_no, "meeting_time":meeting_time}

print "\nFinal Result:", result

<强>输出

Enter key:C001
Enter room number:R001
Meeting time:12:30 PM

Final Result: {'C001': {'room_number': 'R001', 'meeting_time': '12:30 PM'}}

注意

对Python 2.x使用raw_input()

使用来自Python 3.x的input()