简体   繁体   English

计算字典中键的字符数

[英]Counting number of characters in a key in a dictionary

For homework I have been set the following: 对于家庭作业,我已经设置了以下内容:

Build a dictionary with the names from myEmployees list as keys and assign each employee a salary of 10 000 (as value). 使用myEmployees列表中的名称构建一个字典作为键,并为每个员工分配10 000(作为值)的工资。 Loop over the dictionary and increase the salary of employees which names have more than four letters with 1000 * length of their name. 循环遍历字典并增加员工的工资,这些员工的姓名超过四个字母,其名称长度为1000 *。 Print the dictionary contents before and after the increase. 在增加之前和之后打印字典内容。

I can't figure out how to do it. 我无法弄清楚该怎么做。

This is what I've come up with so far. 这是我到目前为止所提出的。

employeeDict = {"John":'10,000', "Daren":"10,000", "Graham":"10,000", "Steve":"10,000", "Adren":"10,000"}

say = 'Before increase'
print say
print employeeDict

say1 = 'After increase'
print say1

for x in employeeDict:
x = len(employeeDict)
if x > 5:
    print employeeDict[x]

You have some indentation problems, clearly, but the main problems are that you are taking the length of the dictionary (getting the number of keys) not taking the length of the key. 显然,你有一些缩进问题,但主要的问题是你花了字典的长度(得到键的数量)没有占用密钥的长度。 You also have some bad logic. 你也有一些不好的逻辑。

employeeDict = {"John":'10,000', "Daren":"10,000", "Graham":"10,000", "Steve":"10,000", "Adren":"10,000"}

say = 'Before increase'
print say
print employeeDict

say1 = 'After increase'
print say1

for x in employeeDict:
    length = len(employeeDict)  # <---- indent this
    if length >= 5:    # <--- greater than 4
        # convert string to number, add money, convert back to string
        employeeDict[x] = str(int(employeeDict[x]) + 1000 * (length))

print employeeDict[x]

First, change the values to integers/floats. 首先,将值更改为整数/浮点数。

employeeDict = {"John":10000, "Daren":10000, "Graham":10000, "Steve":10000, "Adren":10000}

After doing this, as you know, you need to loop over the items in the dict. 执行此操作后,如您所知,您需要遍历dict中的项目。

for x in employeeDict:
    x = len(employeeDict)
    if x > 5:
        print employeeDict[x]

In the code above, your "x" will be the employee name. 在上面的代码中,您的“x”将是员工姓名。 And as you know, to asign a value to a key in dict, you have to use dict[key] = value , so try to do it in the if x > 5: block statement. 如您所知,要将值赋值给dict中的键,您必须使用dict[key] = value ,因此请尝试在if x > 5: block语句中执行此操作。 Im not trying to give you the full answer, but to push you in to the right direction. 我不是想给你完整的答案,而是要把你推向正确的方向。

Give this a try and analyse it : 试一试并分析它:

employees = {"John":10000, "Daren":10000, "Graham":10000}

for name in employees:
    if len(name) > 5:
        employees[name] += 1000 * len(name)

If you have to stick with the string values you can do this : 如果你必须坚持使用字符串值,你可以这样做:

employees = {"John":"10000", "Daren":"10000", "Graham":"10000"}

for name in employees:
    if len(name) > 5:
        employees[name] = str(int(employees[name]) + 1000 * len(name))

This should give you what your looking for. 这应该给你你想要的东西。

employeeDict = {"John":10000, "Daren":10000, "Graham":10000, "Steve":10000, "Adren":10000}
print "Before increase"
print employeeDict

for name, salary in employeeDict.items():
    if len(name) > 4:
        employeeDict[name] = salary + len(name) * 1000
print "After increase"
print employeeDict

You had a few problems in your version. 你的版本有一些问题。

  • Your idention for your for-loop was not correct 你对for循环的识别是不正确的
  • You were getting the length of the dictionary and not the length of the keys in the dictionary. 你都拿到字典的长度,而不是字典中的键的长度。
  • You should make the values in your dictionary floats/integers. 您应该使字典中的值浮点数/整数。

Also note, I believe your homework said if the name was longer than four characters . 还要注意,我相信你的作业如果名字超过四个字符就会说 So i used 4 instead of five. 所以我用了4而不是5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM