程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Daily Practice (New Question Bank of Niu Ke) - Day 20: Dictionary Practice

編輯:Python

文章目錄

  • 1. 查字典
  • 2. 字典新增
  • 3. 使用字典計數
  • 4. 生成字典
  • 5. 如何讓刷題更加高效呢?

前言

最近很多學了基礎的小伙伴問我該怎麼提升編程水平?學了基礎該上哪刷題?明明學了很多,做項目卻不知道怎麼上手,其實這就是練得太少,只注重了學,卻忽視了刷題,只有不斷練習才能提高和鞏固編程思維和能力!



剛好看到牛客網最近出了Python的新題庫於是體驗了一番感覺還不錯



鏈接地址:牛客網 | Python從入門到實踐四十招,廢話少說速度上號,或者跟著下文一起刷題!!!

1. 查字典

描述
Niumei, who is studying English, has prepared such a dictionary on her notebook:{‘a’: [‘apple’, ‘abandon’, ‘ant’], ‘b’: [‘banana’, ‘bee’, ‘become’], ‘c’: [‘cat’, ‘come’], ‘d’: ‘down’}.
Please create such a dictionary,For Niumei input letters,Find out what words are there?

輸入描述:輸入一個字母,must be in the above dictionary.

輸出描述:Output each word in turn on the same line,Space between words.

實現代碼:

dict1 = {
'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
a = input()
for i in dict1[a]:
print(i,end=' ')

運行結果:

a
apple abandon ant

2. 字典新增

描述:Niu Mei, who is learning English, has created a dictionary:{‘a’: [‘apple’, ‘abandon’, ‘ant’], ‘b’: [‘banana’, ‘bee’, ‘become’], ‘c’: [‘cat’, ‘come’], ‘d’: ‘down’}.Now Niu Mei has learned a new letterletter,and a new wordword,Please add them to the dictionary,Then output the updated dictionary.

輸入描述:無

輸出描述
Enter a new letter on the first lineletter,
Enter a new word on the second lineword.

實現代碼:

dict_2 = {

"a": ["apple", "abandon", "ant"],
"b": ["banana", "bee", "become"],
"c": ["cat", "come"],
"d": "down",
}
key1 = list(input().split())
value1 = list(input().split())
for i in range(0, len(key1)):
dict_2[key1[i]] = value1[i]
print(dict_2)

運行結果:

e
egg
{
'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down', 'e': 'egg'}

3. 使用字典計數

描述
PythonA dictionary can be used to count,Let the element to be counted askey值,How often it appearsvalue值,as long as you meetkeyAfter updating the value it corresponds tovalue即可.Now enter a word,Use a dictionary to count the frequency of each letter in the word.

輸入描述:Enter a string representing the word,只有大小寫字母.

輸出描述:Directly output a dictionary of statistical frequencies.

實現代碼:

a = input()
a = list(a)
a_dict = {
}
for i in a:
if i in a_dict:
a_dict[i]+=1
else:
a_dict[i]=1
print(a_dict)

運行結果:

Nowcoder
{
'N': 1, 'o': 2, 'w': 1, 'c': 1, 'd': 1, 'e': 1, 'r': 1}

4. 生成字典

描述
牛牛有兩份列表,一份記錄了牛客網用戶的名字,另一份記錄他們使用的語言.假設兩份列表一一對應,請使用zip函數將兩份列表封裝為字典,以名字為key,語言為value,然後直接輸出字典.

輸入描述
第一行輸入多個字符串表示用戶名字,以空格間隔.
第二行輸入多個字符串表示使用的語言,以空格間隔.

輸出描述:直接輸出兩個列表組成的字典.

代碼實現:

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'}

5. 如何讓刷題更加高效呢?

嫌博主更新慢的小伙伴牛客網上號自行刷題



鏈接地址:牛客網 | Python從入門到實踐四十招,廢話少說速度上號!!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved