实现一个翻译小程序
1 可以查询单词
2 可以自定义补充单词和解释
3 可以删除某个词
新学知识点
- dict
- set
- tuple
### dict 创建增加
# KV
my_dict = {'a':1,'b':2}
my_dict
{'a': 1, 'b': 2}
my_dict['a']
1
coop = {'name':'coop','city':'beijing','code':'python'}
coop['name']
'coop'
coop['city']
'beijing'
coop
{'name': 'coop', 'city': 'beijing', 'code': 'python'}
coop.get('beijing') # 如果没有,就会返回一个空None
coop.get('name')
'coop'
coop.get('city','abc')
'beijing'
coop['age'] = 18 # 增加成员
coop
{'name': 'coop', 'city': 'beijing', 'code': 'python', 'age': 18}
coop["students"] = ['lilei','tomy'] # 字典中可以包含列表
coop
{'name': 'coop',
'city': 'beijing',
'code': 'python',
'age': 18,
'students': ['lilei', 'tomy']}
book = {'name':'python3','price':100}
coop['books']= book # 追加字典到这个字典中
coop
{'name': 'coop',
'city': 'beijing',
'code': 'python',
'age': 18,
'students': ['lilei', 'tomy'],
'books': {'name': 'python3', 'price': 100}}
coop['books']
{'name': 'python3', 'price': 100}
coop
{'code': 'python',
'age': 18,
'students': ['lilei', 'tomy'],
'books': {'name': 'python3', 'price': 100}}
coop['name'] = 'coop'
coop
{'code': 'python',
'age': 18,
'students': ['lilei', 'tomy'],
'books': {'name': 'python3', 'price': 100},
'name': 'coop'}
del coop['name'] # 删除某个key
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-63-e678b9bec09c> in <module>()
----> 1 del coop['name'] # 删除某个key
KeyError: 'name'
coop
{'code': 'python',
'age': 18,
'students': ['lilei', 'tomy'],
'books': {'name': 'python3', 'price': 100}}
# 改
coop['age'] = 20
coop
{'code': 'python',
'age': 20,
'students': ['lilei', 'tomy'],
'books': {'name': 'python3', 'price': 100}}
coop['books']['name']
'python3'
coop.keys() # key 列出该字典中包含的所有的key
dict_keys(['code', 'age', 'students', 'books'])
type(coop.keys())
dict_keys
[i for i in coop.keys()] # for i in coop.keys();print(i)
['code', 'age', 'students', 'books']
[i for j in coop.keys()] # 理解上面的,就不难理解这个报错了
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-80-ce2eddd0d1ae> in <module>()
----> 1 [i for j in coop.keys()] # 理解上面的,就不难理解这个报错了
<ipython-input-80-ce2eddd0d1ae> in <listcomp>(.0)
----> 1 [i for j in coop.keys()] # 理解上面的,就不难理解这个报错了
NameError: name 'i' is not defined
coop.items() # 显示字典中的所有的项目成员
dict_items([('code', 'python'), ('age', 20), ('students', ['lilei', 'tomy']), ('books', {'name': 'python3', 'price': 100})])
type(coop.items())
dict_items
for i in coop.items(): # for循环打印出来的是元祖
print(i)
('code', 'python')
('age', 20)
('students', ['lilei', 'tomy'])
('books', {'name': 'python3', 'price': 100})
[i for i in coop.items()] # 列表推倒,推出来的是列表
[('code', 'python'),
('age', 20),
('students', ['lilei', 'tomy']),
('books', {'name': 'python3', 'price': 100})]
for i,j in coop.items(): # 格式化f中的i j 与变量是一一映射的关系,i j 分别代表了key和value
print(f'{i}:{j}')
code:python
age:20
students:['lilei', 'tomy']
books:{'name': 'python3', 'price': 100}
for i,j in coop.items():
print('%s:%s' %(i,j))
code:python
age:20
students:['lilei', 'tomy']
books:{'name': 'python3', 'price': 100}
for i ,j in coop.items():
print('{}:{}'.format(i,j))
code:python
age:20
students:['lilei', 'tomy']
books:{'name': 'python3', 'price': 100}
元祖 tuple
why what how
增删改查
cor = (23,56) # 元祖的一种表示方法 地理坐标 元祖不可变
cor
(23, 56)
type(cor)
tuple
cor = 23,44 # 元祖的另一种表示方法
type(cor)
tuple
ip_port = ('192.168.0.1',8001)
host = {}
host['ip_port'] = ip_port # 将元祖转变成字典的形式
host
{'ip_port': ('192.168.0.1', 8001)}
host['ip_port']
('192.168.0.1', 8001)
host['ip_port'][0] # 取值
'192.168.0.1'
host['ip_port'][:1]
('192.168.0.1',)
host['ip_port'][1:]
(8001,)
ip = ('192.168.1.31') # 表示字符串
type(ip)
str
ip = ('192.168.1.1',) # 加个逗号,表示元祖
type(ip)
tuple
host
{'ip_port': ('192.168.0.1', 8001)}
host[ip] = '192.168.1.1' # [ ]中的ip是上面的元组,key可以是字符串也可以是元组
host
{'ip_port': ('192.168.0.1', 8001), ('192.168.1.1',): '192.168.1.1'}
host[ip] = 'admin_pc' # 将元祖作为字典的key,元祖具有不可变性
host
{'ip_port': ('192.168.0.1', 8001), ('192.168.1.1',): 'admin_pc'}
列表与元祖互相转换
name = ['lilei','han×××','coop']
type(name)
list
names = tuple(name) # 将列表转换成元祖
names
('lilei', 'han×××', 'coop')
type(names)
tuple
list(names) # 将元祖转换成列表
['lilei', 'han×××', 'coop']
set 不重复的元素
number = {1,2,3,4,42,5,6,2,3,4}
type(number)
set
number
{1, 2, 3, 4, 5, 6, 42}
for i in number:
print(i)
1
2
3
4
5
6
42
other = [2,3,4,5,6,7,4,3,2,1]
set(other) # 将列表转换成集合
{1, 2, 3, 4, 5, 6, 7}
other
[2, 3, 4, 5, 6, 7, 4, 3, 2, 1]
len(other)
10
others = set(other)
others
{1, 2, 3, 4, 5, 6, 7}
len(others)
7
dir(dict)
['__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'clear',
'copy',
'fromkeys',
'get',
'items',
'keys',
'pop',
'popitem',
'setdefault',
'update',
'values']
host
{'ip_port': ('192.168.0.1', 8001), ('192.168.1.1',): 'admin_pc'}
host.get('ip_port') # D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.如果k在D里面,则返回value,如果不存在,则返回None
('192.168.0.1', 8001)
word = "shu:book:app:banana" # split 的用法S.split(sep=None, maxsplit=-1) -> list of strings\
# maxsplit指定分隔符的作用范围
word.split(':',maxsplit=1)
['shu', 'book:app:banana']
word.split(':',maxsplit=2)
['shu', 'book', 'app:banana']
print("welcome to coop's dictionary".center(50,'*'))
orig_dict = {'中文':'Chinese','书':'book','代码':'code','字典':'dictionary'}
query = input('please input your word:')
if (orig_dict.get(query,'')):
print(f'你查询的中文是:{query} ,它的英文意思是:{orig_dict[query]}')
else:
add = input('没有查到,是否愿意补充该词语(y/n)')
if add == 'y':
print(orig_dict)
print('谢谢,请添加单词和相关解释,用冒号分割')
words = input('示例(书:book)')
words = words.split(':')
orig_dict[words[0]] = words[1]
print(orig_dict)
else:
print('byebye')
***********welcome to coop's dictionary***********
please input your word:长城
没有查到,是否愿意补充该词语(y/n)y
{'中文': 'Chinese', '书': 'book', '代码': 'code', '字典': 'dictionary'}
谢谢,请添加单词和相关解释,用冒号分割
示例(书:book)长城:great wall
{'中文': 'Chinese', '书': 'book', '代码': 'code', '字典': 'dictionary', '长城': 'great wall'}