每周专业学习周记-3
每周专业学习周记-3
标签(空格分隔): 周记
数据结构篇
本周数据结构以阅读复习书本内容为主
数据结构小结
说实话数据结构这门课程真的很难,学习起来也感觉很吃力。很多算法到目前为止都还是一知半解。要么会写代码而不懂得其中的原理,要么就是懂一点点。最近看到数据结构有些厌烦,想好好调整一下自己,最近以书本和理论题为主。实践算法暂时不做打算。
Python篇
# 6.1 一个简单的字典
alien_0 = {'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points'])
# 6.2 使用字典
alien_0 = {'color':'green','points':5}
alien_0 = {'color':'green'}
# 6.2.1 访问字典中的值
alien_0 = {'color':'green'}
print(alien_0['color'])
alien_0 = {'color':'green','points':'5'}
new_points = alien_0['points']
print("You just earned 5 points.")
# 6.2.2 添加键-值对
alien_0 = {'color':'green','points':5}
print(alien_0)
alien_0['x_postion'] = 0
alien_0['y_postion'] = 25
print(alien_0)
# 6.2.3 先创建一个空字典
alien_0 = {}
alien_0['color'] = 'green'
alien_0['points'] = 5
print(alien_0)
# 6.2.4 修改字典中的值
alien_0 = {'color':'green'}
print("The alien is "+alien_0['color']+".")
alien_0['color'] = 'yellow'
print("The alien is now "+alien_0['color']+".")
alien_0 = {'x_position':0,'y_position':25,'speed':'medium'}
print("Original x_position: "+str(alien_0['x_position']))
# 向右移动外星人
# 根据外星人的移动速度决定将其移动多远
if alien_0['speed'] == 'slow':
x_increment = 1
elif alien_0['speed'] == 'medium':
x_increment = 2
else:
x_increment = 3
# 新位置等于老位置加上增量
alien_0['x_position'] = alien_0['x_position'] + x_increment
print("New x-position: "+str(alien_0['x_position']))
# 6.2.5 删除键-值对
# del语句删除时需要指定字典名和要删除的键
alien_0 = {'color':'green','points':5}
print(alien_0)
del alien_0['color']
print(alien_0)
# 删除完这个键值对就永远消失了
# 6.2.6 由类似对象组成的字典
favorite_languages = {
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python'
}
print("Sarah's favorite language is "+
favorite_languages['sarah'].title()+".")
# 6-1人
message = {
'first_name':'zhang',
'last_name':'yu',
'age':18,
'city':'fuzhou'
}
print(message)
# 6-2 喜欢的数字
number = {
'lihong':1,
'xixi':2,
'liuzi':3,
'yingyingying':33,
'lla':12
}
print("lihong favorite number is "+str(number['lihong']))
print("xixi favorite number is "+str(number['xixi']))
print("liuzi favorite number is "+str(number['liuzi']))
print("yingyingying favorite number is "+str(number['yingyingying']))
print("lla favorite number is "+str(number['lla']))
# 6-3 词汇表
meaning = {
'print':'打印',
'del':'删除',
'reverse':'反向排列列表',
'sorted()':'临时按字母顺序排序列表',
'sort()':'永久按字母顺序排列列表',
'range()':'创建数字',
'list()':'创建数字列表'
}
print("print: "+meaning['print'])
print("del: "+meaning['del'])
print("reverse: "+meaning['reverse'])
print("sorted(): "+meaning['sorted()'])
print("sort(): "+meaning['sort()'])
print("range(): "+meaning['range()'])
print("list(): "+meaning['list()'])
# 6.3.1 遍历所有的键-值对
user_0 = {
'username':'efeimi',
'first':'enrioc',
'lase':'fermi'
}
for key,value in user_0.items():
print('\nkey: '+key)
print('vlalue: '+value)
favorite_languages = {
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python'
}
for name,language in favorite_languages.items():
print(name.title()+"'s favorite language is "+
language.title()+".")
# 6.3.2 遍历字典中的所有键
favorite_languages = {
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python'
}
# 如果不写keys()系统会自动默认
for name in favorite_languages.keys():
print(name)
print()
friend = ['phil','sarah']
for name in favorite_languages:
print(name)
if name in friend:
print("Hi, "+name.title()+" i see you favotite language is "+
favorite_languages[name]+".")
if 'erin' not in favorite_languages.keys():
print("Erin,please take our poll!")
# 6.3.3 按顺序遍历字典中的所有键
favorite_languages = {
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python'
}
for name in sorted(favorite_languages.keys()):
print(name+" thank you for taking the poll.")
# 6.3.4 遍历字典中的所有值
favorite_languages = {
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python'
}
print("The following languages have been mentioned:")
for language in favorite_languages.values():
print(language)
#这种做法提取字典中的所有值,但是没有考虑它们是否重复,可以使用set()提取出不同的
print("The following languages have been mentioned:")
for language in set(favorite_languages.values()):
print(language)
# 6-4 词汇表2
meaning = {
'print':'打印',
'del':'删除',
'reverse':'反向排列列表',
'sorted()':'临时按字母顺序排序列表',
'sort()':'永久按字母顺序排列列表',
'range()':'创建数字',
'list()':'创建数字列表'
}
print("print: "+meaning['print'])
print("del: "+meaning['del'])
print("reverse: "+meaning['reverse'])
print("sorted(): "+meaning['sorted()'])
print("sort(): "+meaning['sort()'])
print("range(): "+meaning['range()'])
print("list(): "+meaning['list()'])
print()
for name,mean in meaning.items():
print(name+" "+mean)
meaning['items()'] = '它可以返回一对键-值列表'
meaning['keys()'] = '它可以返回字典中的键'
meaning['values()'] = '它可以返回字典中的值'
print()
for name,mean in meaning.items():
print(name+" "+mean)
# 6-5 河流
rivers = {
'nile':'egypt',
'huanghe':'china',
'ob river':'russia'
}
for river_name,country in rivers.items():
print("The "+river_name+" runs through "+country)
for river_name in rivers.keys():
print(river_name)
for country in rivers.values():
print(country)
# 6-6 调查
favorite_languages = {
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python'
}
people = ['lcyu','lili','phil','jen','sarah','edward']
for name in favorite_languages.keys():
if name in people:
print(name+" Thank you for you coming to the investigation!")
people.remove(name)
for name in people:
print(name+ " I look forward to your coming to the investigation!")
# 6.4 嵌套
# 有时候需要将一系列字典存储在列表中,或将列表存储在字典中,这称为嵌套。你可以在列表中
# 嵌套字典,在字典中嵌套列表,甚至在字典中嵌套字典。嵌套是一项强大的功能。
# 6.4.1 字典列表
alien_0 = {'color':'green','points':5}
alien_1 = {'color':'red','points':10}
alien_2 = {'color':'yellow','points':15}
aliens = [alien_0,alien_1,alien_2]
for alien in aliens:
print(alien)
print()
# 创建一个用于存储外星人的空列表
aliens = []
for alien in range(30):
new_alien = {'speed':'slow','color':'green','points':5}
aliens.append(new_alien)
for alien in aliens[:5]:
print(alien)
print("....")
print("外星人的数量是"+str(len(aliens)))
for alien in aliens[0:3]:
if alien['color'] == 'green':
alien['color'] = 'yellow'
alien['speed'] = 'mudium'
alien['points'] = '10'
for alien in aliens[:5]:
print(alien)
for alien in aliens[:3]:
alien['color'] = 'red'
alien['speed'] = 'fast'
alien['points'] = 15
for alien in aliens[:5]:
print(alien)
# 6.4.2 在字典中存储列表
pizza = {
'crust':'thick',
'toppings':['mushroom','extra cheese']
}
print("You ordered a "+pizza['crust']+"-crust pizza with the following toppings:")
for topping in pizza['toppings']:
print(topping)
favorite_languages = {
'jen':['python','ruby'],
'sarach':['c'],
'edward':['python','go'],
'phil':['python','haskell']
}
for name,languages in favorite_languages.items():
print(name+"'s favorite language are:")
for language in languages:
print(language)
print("-------")
for name,languages in favorite_languages.items():
if len(languages) > 1:
print(name.title()+" favorite languages are:")
for language in languages:
print(language)
else:
print(name.title()+"favorite language is:")
print(language)
# 6.3.4 在字典中存储字典
users = {
'aenintein':{
'first':'albert',
'last':'einstein',
'location':'princeton'
},
'mucire':{
'first':'marie',
'last':'curie',
'location':'paris'
}
}
for username,user_info in users.items():
print("Username:"+username)
full_name = user_info['first']+" "+user_info['last']
location = user_info['location']
print("Full name:"+full_name)
print("Location:"+location)
# 6-7 人
people = {
'one':{
'first':'zhang',
'last':'yu',
'age':18,
'city':'fuzhou'
},
'two':{
'first':'wang',
'last':'zhang',
'age':19,
'city':'zhangzhou'
},
'three':{
'first':'chen',
'last':'pian',
'age':10,
'city':'quanzhou'
},
'four':{
'first':'wang',
'last':'wei',
'age':22,
'city':'xiamen'
}
}
print()
for num,info in people.items():
print("num:"+num)
full_name = info['first']+info['last']
print("full name:"+full_name)
print("age:"+str(info['age']))
print("city:"+info['city'])
print()
# 6-8 宠物
pets = {
'来福':{
'主人':'王晓霞',
'品种':'泰迪'
},
'旺财':{
'主人':'成程',
'品种':'中华田园犬'
}
}
for pet_name,pet_info in pets.items():
print("宠物名称:"+pet_name)
print("宠物主人:"+pet_info['主人'])
print("宠物品种:"+pet_info['品种'])
print()
# 6-9 喜欢的字典
favorite_places = {
'莉莉':['福州','北京'],
'林红':['上海','深圳','厦门'],
'王涛':['天津']
}
for name,places in favorite_places.items():
print(name+" 最喜欢的地方是:")
for place in places:
print(place,end=" ")
print()
# 6-10 喜欢的数字
numbers = {
'lihong':[1,2],
'xixi':[2,3,4],
'liuzi':[3,45,6,3],
'yingyingying':[33,23,21],
'lla':[12,1,2,5]
}
for name,number in numbers.items():
print(name+"喜欢的数字是:")
for num in number:
print(num,end=" ")
print()
Python小结
在本章中学习了:如何定义字典,以及如何使用存储在字典中的信息。如何访问和修改字典中的元素以及如何遍历字典中的所有信息,如何遍历字典中的键值对、所有的键和所有的值,如何在列表中嵌套字典,在字典中嵌套列表,以及如何在字典中嵌套字典。
目前对于Python的热情还是挺高的,可能是因为我还处于比较基础的阶段,学起来也挺轻松的。还是希望能好好的坚持下去,尽快学习完这本书,然后进入其他Python的扩展书籍,逐步推进,稳中求快。