In [1]: #欢迎来到“只读课堂”!
In [2]: #今天我们来建个列表库
In [3]: #就以QQ联系人列表为例把
In [4]: """QQ friends numbers book:Python3.6"""
Out[4]: 'QQ friends numbers book:Python3.6'
In [5]: Q_number = {}
In [6]: #它等同于“Q_number = dict()”。
In [7]: #下面我们往里面添加内容:
In [8]: Q_number["Tencet"] = 10000
In [9]: Q_number["MyQQ"] = xxxxx
In [10]: Q_number["AnotherQQ"] = xxxxx
In [11]: #添加了一些,我们可以尝试搜索以下以前的内容:
In [12]: Q_number["Tencet"]
Out[12]: 10000
In [13]: #它就会自动返回那个值。
In [14]: Q_number["pythonTeacher1"] = xxxxx
In [15]: Q_number["MyQQ"]
Out[15]: xxxxx
In [16]: #这样方便查找
In [17]: #可是,如果重复了呢?所以我们在新加入QQ号前可以先查找一下,为了方便,我们使用以下代码:
In [18]: value Q_number.get("Tencet")
In [19]: #如果Tencet在列表中,则返回“True”,如果不在,则返回“None”。
In [20]: #还有,这个程序会自动把你的QQ联系人列表包装:
In [21]: print Q_number
File "<ipython-input-21-1869e871ea69>", line 1
print Q_number
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(Q_number)?
In [22]: #哦,它这里要用括号。
In [23]: print(Q_number)
{'Tencet': 10000, 'MyQQ': xxxxx, 'AnotherQQ': xxxxx, 'pythonTeacher1': xxxxx}
In [24]: #这样就非常简洁,美观。
In [25]: #下面我们再来用自创代码来防止重复,进行查找。
In [26]: def check_Q_number(name):
...: if Q_number.get(name):
...: print("True name")
...: else:
...: Q_number[name] = True
...: print("unregister")
...:
In [27]: #我们来测试几次:
In [28]: check_Q_number("Tencet")
True name
In [29]: #表示已经在列表里了。
In [30]: check_Q_number("MyTeacher")
unregister
In [31]: #表示还没有在列表里。
In [32]: print(Q_number)
{'Tencet': 10000, 'MyQQ': xxxxx, 'AnotherQQ': xxxxx, 'pythonTeacher1': xxxxx, 'MyTeacher': True}
In [33]: #你会发现,刚刚搜索的东西竟然在里面!所以,这种代码不仅能查找,还能导入已查找的记录。
In [34]: #——————————————————————————————————————————————————————————————————————
In [35]: #所有代码如下:
In [36]: #添加
...:
...: Q_number = {}
...:
...: Q_number["Tencet"] = 10000
...: Q_number["MyQQ"] = xxxxx
...: Q_number["AnotherQQ"] = xxxxx
...: Q_number["pythonTeacher1"] = xxxxx
...:
In [37]: #查找
In [38]: def check_Q_number(name):
...: if Q_number.get(name):
...: print("True name")
...: else:
...: Q_number[name] = True
...: print("unregister")
...:
...: check_Q_number("Tencet")
...: check_Q_number("MyTeacher")
...:
True name
unregister
In [39]: #查看
In [40]: print(Q_number)
{'Tencet': 10000, 'MyQQ': xxxxx, 'AnotherQQ': xxxxxx, 'pythonTeacher1': xxxxx, 'MyTeacher': True}
In [40]:
In [41]: #以上代码输出输入的内容都只是个例子。
In [42]: print("\n本次“只读课堂”的python教程就到这了,欢迎继续收看!\n")
本次“只读课堂”的python教程就到这了,欢迎继续收看!