【问题标题】:Extract value from dictionary - modifying it从字典中提取值 - 修改它
【发布时间】:2020-05-19 15:29:37
【问题描述】:

我被困在学校这个问题上的时间比我愿意承认的要长......我已经完成了大约 5 次不同的迭代来解决这个问题,我最近的一次低于这个问题。这是我正在上的一门课。

给定以下员工和薪水字典,创建个性化的薪水信息,让每位员工知道他们获得了 2% 的加薪和新的总薪水。

预期结果:

John, your current salary is 54000.00. You received a 2% raise. This makes your new salary 55080.0
Judy, your current salary is 71000.00. You received a 2% raise. This makes your new salary 72420.0
Albert, your current salary is 38000.00. You received a 2% raise. This makes your new salary 38760.0
Alfonzo, your current salary is 42000.00. You received a 2% raise. This makes your new salary 42840.0
employeeDatabase = {
    'John': 54000.00,
    'Judy': 71000.00,
    'Albert': 38000.00,
    'Alfonzo': 42000.00
}

我的许多尝试之一(我现在意识到我应该保存以前的尝试,我只是使用随机在线 IDE):

newdict = employeeDatabase.copy()

for x in newdict:
    newsal = newdict[x]
    newsal = newsal *.02 + newsal
    for i in employeeDatabase:
        print (i + ' your current salary is %s You received a 2%% raise. This makes your new salary %d'  % (employeeDatabase[i], newsal))

【问题讨论】:

  • 您似乎拥有我们所说的XY problem,因为您的问题陈述中没有任何内容表明您需要将新工资存储在任何地方(尽管也许您可能想要存储?)所以如果没有必要要存储新工资,无需创建newdict。但是,如果您确实愿意,可以写 employeeDatabase[x] = newsal 来更新原始员工数据库。

标签: python dictionary


【解决方案1】:

你不需要在那里使用newdict,你可以使用items来获取姓名和薪水,然后打印这两个值加上增加的值。我也将其更改为使用新的字符串格式化语法,因为旧的 % 样式已不再使用:

for employee, salary in employeeDatabase.items():
        print ("{}, your current salary is {:.2f}. You received a 2% raise. This makes your new salary {:.2f}".format(employee, salary, salary * 1.02))

【讨论】:

  • 非常简洁的解决方案,但也许向提问者解释 for key, value in someDict.items() 的工作原理?
  • @CrystalDuck .items() 方法,为每个键:值在元组列表上打开一个字典。像 {'key': 'value', 'otherKey': 'other value'} 这样的 dict 会返回 [('key', 'value'), ('otherKey', 'other value')]
  • 非常感谢您的帮助,这是有道理的。我有一个问题(我对实际条款不是最好的,所以请耐心等待)。所以看起来 {:.2f} 可以用作变量替换 {} 以及所述变量 ':.2f' 的格式? @Diego Magalhães @CrystalDuck
  • 是的,完全正确。请参阅 pyformat.info 以获得有关字符串格式化的出色指南
【解决方案2】:

由于 dict 结构,您可以使用键来更改数据,所以:

employeeDatabase = {
    'John': 54000.00,
    'Judy': 71000.00,
    'Albert': 38000.00,
    'Alfonzo': 42000.00
}
employeeDatabase['John'] = 58000.00

现在约翰的工资是 58000

将所有员工的工资提高 2% 这样做:

def raise_salary(employeers):
    for i in employeers.keys():
        print(f'{i} your current salary is {employeers[i]} You received a 2% raise. This makes your new salary {employeers[i] + employeers[i] *.02}')

注意:我使用的是 f-strings,这适用于 python3.6+

【讨论】:

    【解决方案3】:

    你可以在python中使用dict对象的函数.items()。我给你一个键值对,你可以用 for 循环进行迭代。此外,通过使用字符串函数.format(),您可以使用默认消息设置一个变量,然后在 for 循环中填充特定值(姓名、薪水等)

    message = '{}, your current salary is {}. You received a 2% raise. This makes your new salary {}'
    employeeDatabase = {
        'John': 54000.00,
        'Judy': 71000.00,
        'Albert': 38000.00,
        'Alfonzo': 42000.00
    }
    
    for employee, salary in employeeDatabase.items():
        print(message.format(employee, salary, salary * 1.02))
    

    变量message中的{}表示你想用dict中的那个来自定义默认信息的地方。

    【讨论】:

      猜你喜欢
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 2017-08-08
      • 2013-06-09
      • 1970-01-01
      相关资源
      最近更新 更多