前言
最近很多學了基礎的小伙伴問我該怎麼提升編程水平?學了基礎該上哪刷題?明明學了很多,做項目卻不知道怎麼上手,其實這就是練得太少,只注重了學,卻忽視了刷題,只有不斷練習才能提高和鞏固編程思維和能力!
剛好看到牛客網最近出了Python的新題庫於是體驗了一番感覺還不錯
鏈接地址:牛客網 | Python從入門到實踐四十招,廢話少說速度上號,或者跟著下文一起刷題!!!
描述:
創建一個依次包含鍵-值對{‘name’: ‘Niuniu’和’Student ID’: 1}的字典my_dict_1,
創建一個依次包含鍵-值對{‘name’: ‘Niumei’和’Student ID’: 2}的字典my_dict_2,
創建一個依次包含鍵-值對{‘name’: ‘Niu Ke Le’和’Student ID’: 3}的字典my_dict_3,
創建一個空列表dict_list,使用append()方法依次將字典my_dict_1、my_dict_2和my_dict_3添加到dict_list裡,
使用for循環遍歷dict_list,對於遍歷到的字典,使用print()語句一行輸出類似字符串"Niuniu’s student id is 1."的語句以打印對應字典中的內容.
輸入描述:無
輸出描述:按題目描述進行輸出即可.
Niuniu’s student id is 1.
Niumei’s student id is 2.
Niu Ke Le’s student id is 3.
實現代碼:
my_dict_1 = {
'name': 'Niuniu', 'Student ID': 1}
my_dict_2 = {
'name': 'Niumei', 'Student ID': 2}
my_dict_3 = {
'name': 'Niu Ke Le', 'Student ID': 3}
dict_list = []
dict_list.append(my_dict_1)
dict_list.append(my_dict_2)
dict_list.append(my_dict_3)
for i in dict_list:
# 字典獲取元素的方法i['key值'],或者i.get('key值')
print(f"{i['name']}'s student id is {i.get('Student ID')}.")
運行結果:
Niuniu's student id is 1.
Niumei's student id is 2.
Niu Ke Le's student id is 3.
描述: 創建一個依次包含鍵-值對’Beijing’: {Capital: ‘China’}、‘Moscow’: {Capital: ‘Russia’}和’Paris’: {Capital: ‘France’}的字典cities_dict,
請使用for循環遍歷"已使用sorted()函數按升序進行臨時排序的包含字典cities_dict的所有鍵的列表",
對於每一個遍歷到的城市名,使用print()語句一行輸出類似字符串’Beijing is the capital of China!'的語句.
輸入描述:無
輸出描述:按題目描述進行輸出即可.
Beijing is the capital of China!
Moscow is the capital of Russia!
Paris is the capital of France!
實現代碼:
cities_dict={
'Beijing': {
"Capital": 'China'},
'Moscow': {
"Capital": 'Russia'},
'Paris': {
"Capital": 'France'}
}
for i in sorted(cities_dict.keys()):
print(f"{i} is the capital of {cities_dict[i]['Capital']}!")
運行結果:
Beijing is the capital of China!
Moscow is the capital of Russia!
Paris is the capital of France!
描述:
駝瑞馳調查了班上部分同學喜歡哪些顏色,並創建了一個依次包含鍵-值對’Allen’: [‘red’, ‘blue’, ‘yellow’]、‘Tom’: [‘green’, ‘white’, ‘blue’]和’Andy’: [‘black’, ‘pink’]的字典result_dict,作為已記錄的調查結果.
現在駝瑞馳想查看字典result_dict的內容,你能幫幫他嗎?
使用for循環遍歷"使用sorted()函數按升序進行臨時排序的包含字典result_dict的所有鍵的列表",對於每一個遍歷到的名字,先使用print()語句一行輸出類似字符串"Allen’s favorite colors are:"的語句,然後再使用for循環遍歷該名字在字典result_dict中對應的列表,依次輸出該列表中的顏色.
輸入描述:無
輸出描述:按題目描述進行輸出即可.
Allen’s favorite colors are:
red
blue
yellow
Andy’s favorite colors are:
black
pink
Tom’s favorite colors are:
green
white
blue
實現代碼:
result_dict = {
'Allen': ['red', 'blue', 'yellow'],
'Tom': ['green', 'white', 'blue'],
'Andy': ['black', 'pink']
}
for i in sorted(k for k in result_dict):##List comprehension generationkey的列表
print("%s's favorite colors are:" % i)
for x in result_dict[i]:
print(x)
運行結果:
Allen's favorite colors are:
red
blue
yellow
Andy's favorite colors are:
black
pink
Tom's favorite colors are:
green
white
blue
描述:
Niuniu has two lists,A record of the names of Niuke.com users,Another record of the language they use.Suppose the two lists are in one-to-one correspondence,請使用zipThe function wraps the two lists as dictionaries,by namekey,語言為value,Then output the dictionary directly.
輸入描述:
On the first line, enter multiple strings representing the user name,以空格間隔.
The second line enters multiple strings indicating the language used,以空格間隔.
輸出描述:Directly output a dictionary of two lists.
代碼實現:
a =input()
b = input()
names = a.split()
language = b.split()
dict_a = dict(zip(names,language))
print(dict_a)
運行結果:
Niuniu NIumei Niukele
C C++ Python
{
'Niuniu': 'C', 'NIumei': 'C++', 'Niukele': 'Python'}
嫌博主更新慢的小伙伴牛客網上號自行刷題
鏈接地址:牛客網 | Python從入門到實踐四十招,廢話少說速度上號!!!