首页 >后端开发 >Python教程 >Python Day-Dictionary-练习、任务

Python Day-Dictionary-练习、任务

Mary-Kate Olsen
Mary-Kate Olsen原创
2025-01-03 04:44:40598浏览

Python Day-Dictionary- Exercises, Tasks

字典-{}

-->将数据值存储在键:值对
中 -->有序、可更改且不允许重复。

练习:
1.

menu = {'idli':10, 'dosai':20, 'poori':30}
print(menu)

menu['pongal'] = 40 

del menu['idli']
print(menu)

print(menu['dosai'])

输出:

{'idli': 10, 'dosai': 20, 'poori': 30}
{'dosai': 20, 'poori': 30, 'pongal': 40}
20

2。要在空字典中添加键:值对,使用 get() 函数

time_table = {}

time_table['tamil'] = 10
time_table['english']= 10

print(time_table)

print(time_table['tamil'])
print(time_table.get('tamil'))
print(time_table.get('maths'))
print(time_table['maths'])

输出:

{'tamil': 10, 'english': 10}
10
10
None
KeyError: 'maths'

-->如果通常我们输入字典中没有的键,那么输出将是 KeyError。
-->相反,如果我们使用 get() 函数,它不会返回任何内容。

3.获取键、值以及两者

menu = {'idli':10, 'dosai':20, 'poori':30}
print(menu)
print(menu.keys())
print(menu.values())
print(menu.items())

输出:

{'idli': 10, 'dosai': 20, 'poori': 30}
dict_keys(['idli', 'dosai', 'poori'])
dict_values([10, 20, 30])
dict_items([('idli', 10), ('dosai', 20), ('poori', 30)])

Keys-->从字典中打印键(dict_name.keys())。
值-->打印字典中的值(dict_name.values())。
items-->将字典中的键和值打印为元组 (dict_name.items())。

4。从字典中单独获取键、值或项目。

fruits_menu = {'apple':100, 'banana':80, 'grapes':120}

for fruit in fruits_menu.keys():
    print(fruit)

for price in fruits_menu.values():
    print(price)

for fruit, price in fruits_menu.items():
    print(fruit, price)

输出:

apple
banana
grapes
100
80
120
apple 100
banana 80
grapes 120

5。如果键包含字母“e”,则打印值。

fruits_menu = {'apple':100, 'banana':80, 'grapes':120}

for fruit in fruits_menu.keys():
    if 'e' in fruit:
        print(fruits_menu[fruit])

输出:

100
120

6。将字典转换为元组或列表。

fruits_menu = {'apple':100, 'banana':80, 'grapes':120}

print(list(fruits_menu))
print(tuple(fruits_menu))

输出:

['apple', 'banana', 'grapes']
('apple', 'banana', 'grapes')

7。嵌套字典。

emp1 = {'name':'guru prasanna', 'qual':'B.Com'}
emp2 = {'name':'lakshmi pritha', 'qual': 'M.E'}

print(emp1)
print(emp2)

employees = {101:emp1, 102:emp2}
print(employees)

输出:

{'name': 'guru prasanna', 'qual': 'B.Com'}
{'name': 'lakshmi pritha', 'qual': 'M.E'}
{101: {'name': 'guru prasanna', 'qual': 'B.Com'}, 102: {'name': 'lakshmi pritha', 'qual': 'M.E'}}

8。从字典中单独获取员工姓名。

emp1 = {'name':'guru prasanna', 'qual':'B.Com'}
emp2 = {'name':'lakshmi pritha', 'qual': 'M.E'}


employees = {101:emp1, 102:emp2}
print(employees)

for roll_no, employee in employees.items():
    for key, value in employee.items():
        if key == 'name':
            print(employee[key])

输出:

{101: {'name': 'guru prasanna', 'qual': 'B.Com'}, 102: {'name': 'lakshmi pritha', 'qual': 'M.E'}}
guru prasanna
lakshmi pritha

9。单独获取 B.com 员工。

emp1 = {'name':'guru prasanna', 'qual':'B.Com'}
emp2 = {'name':'lakshmi pritha', 'qual': 'M.E'}

employees = {101:emp1, 102:emp2}

for roll_no, employee in employees.items():
    for key, value in employee.items():
        if value == 'B.Com':
            print(employee['name'])

输出:

guru prasanna

10。每个值增加 10%。

fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
for fruit in fruits_menu.values():
    fruit=fruit+(fruit/10)
    print(fruit)

输出:

110.0
88.0
132.0

11。转换键-->价值观与价值-->键。

fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
new_menu = {}

for fruit,price in fruits_menu.items():
    new_menu[price] = fruit

print(new_menu)

输出:

{100: 'apple', 80: 'banana', 120: 'grapes'}

字典理解

fruits_menu = {'apple':100, 'banana':80, 'grapes':120}

menu_dict = {(fruit,price) for fruit,price in fruits_menu.items()}
print(menu_dict)

menu_dict = {fruit: price for fruit,price in fruits_menu.items()}
print(menu_dict)

#To reverse key-->value and value-->key

menu_dict = {price : fruit for fruit,price in fruits_menu.items()}
print(menu_dict)

输出:

{('grapes', 120), ('apple', 100), ('banana', 80)}
{'apple': 100, 'banana': 80, 'grapes': 120}
{100: 'apple', 80: 'banana', 120: 'grapes'}

get()
get() 方法返回具有指定键的项的值。
-->如果字典中不存在键,则返回无。

fruits_menu = {'apple':100, 'banana':80, 'grapes':120}

print(fruits_menu.get('apple',"not available"))
print(fruits_menu.get('kiwi',"not available"))

输出:

100
not available

查找给定字符串中字母的频率

#frequency of each letter in a given string
freq = {}
name = 'guruprasanna'
for letter in name: 
    freq[letter] = freq.get(letter,0)+1

print(freq)

输出:

{'g': 1, 'u': 2, 'r': 2, 'p': 1, 'a': 3, 's': 1, 'n': 2}

将字典转换为集合

csk = {'dhoni':101, 'jadeja':102}
india = {'virat':103, 'jadeja':102}

print(set(csk))
print(set(india))

print(set(csk.keys()))
print(set(india.keys()))

输出:

{'dhoni', 'jadeja'}
{'virat', 'jadeja'}
{'dhoni', 'jadeja'}
{'virat', 'jadeja'}

setdefault()

--> setdefault() 方法返回具有指定键的项目的值。
-->如果键不存在,则插入具有指定值的键。

csk = {'dhoni':101, 'jadeja':102}
india = {'virat':103, 'jadeja':102}

csk.setdefault('rohit',100)
print(csk)
csk.setdefault('dhoni',100)
print(csk)

输出:

menu = {'idli':10, 'dosai':20, 'poori':30}
print(menu)

menu['pongal'] = 40 

del menu['idli']
print(menu)

print(menu['dosai'])

任务:

1。查找:
a) 两个团队都有共同点
b) 出现在任何一支球队
c) 总玩家姓名

{'idli': 10, 'dosai': 20, 'poori': 30}
{'dosai': 20, 'poori': 30, 'pongal': 40}
20

输出:

time_table = {}

time_table['tamil'] = 10
time_table['english']= 10

print(time_table)

print(time_table['tamil'])
print(time_table.get('tamil'))
print(time_table.get('maths'))
print(time_table['maths'])

2。查找字符串中单词的出现频率 - 'a Rose is a Rose is a Rose'。

{'tamil': 10, 'english': 10}
10
10
None
KeyError: 'maths'

输出:

menu = {'idli':10, 'dosai':20, 'poori':30}
print(menu)
print(menu.keys())
print(menu.values())
print(menu.items())

3。在字典中查找总分、平均分和高分。
玩家 = {'jaiswal':75, 'rohit':55, 'virat':95}

{'idli': 10, 'dosai': 20, 'poori': 30}
dict_keys(['idli', 'dosai', 'poori'])
dict_values([10, 20, 30])
dict_items([('idli', 10), ('dosai', 20), ('poori', 30)])

输出:

fruits_menu = {'apple':100, 'banana':80, 'grapes':120}

for fruit in fruits_menu.keys():
    print(fruit)

for price in fruits_menu.values():
    print(price)

for fruit, price in fruits_menu.items():
    print(fruit, price)
apple
banana
grapes
100
80
120
apple 100
banana 80
grapes 120

输出:

fruits_menu = {'apple':100, 'banana':80, 'grapes':120}

for fruit in fruits_menu.keys():
    if 'e' in fruit:
        print(fruits_menu[fruit])

以上是Python Day-Dictionary-练习、任务的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn