简体   繁体   English

迭代 python 字典的键,当键是整数时,我收到此错误,“TypeError:'int' 类型的参数不可迭代”

[英]Iterating over the keys of a python dictionary, when keys are integers, I get this error, "TypeError: argument of type 'int' is not iterable"

I am working on pay raise of employees with particular ids .我正在为具有特定idsemployees pay raise Suppose, I have 5 employees in my company.假设我的公司有5名员工。 I have shown them in a employee_id_list .我已经在employee_id_list中展示了它们。 I want python to take input from me which consists of the particular ids of the employees , I want to raise the pay, along with their salary .我希望 python 从我那里获取包含employees特定ids的输入,我想提高工资以及他们的salary Then, I am creating the dictionary out of these inputs.然后,我从这些输入中创建dictionary Now I want to iterate over the employee_id_list such that it matches with of the input ids .现在我想遍历employee_id_list使其与input ids匹配。 If it matches, I want to take the respective value of key which is salary and raise the pay.如果匹配,我想取value of keysalary并加薪。 But I am getting an error.但我收到一个错误。 I have searched for everything present on stackoverflow but nothing matches my problem我已经搜索了 stackoverflow 上的所有内容,但没有任何内容符合我的问题

employee_id_list = [27, 25, 98, 78, 66]
employee_dict = dict()
while True:
    x = input("Enter an key to continue and 'r' for result: ").lower()
    if x== 'r':
        break
    try:
        employee_id = int(input("Enter key the Employee id: ")) 
        salary = int(input(f"Enter the {employee_id}'s salary: "))
        employee_dict[employee_id] = salary
    except ValueError:
        print("Please Enter the Integers!")
        continue 
print(employee_dict)
for e_ids in employee_id_list:
    for key, value in employee_dict.items():
        if e_ids in employee_dict[key] :
            employee_dict[value] = 0.8*value + value
print(employee_dict)

I am getting this error我收到此错误

TypeError: argument of type 'int' is not iterable

It's this:是这样的:

if e_ids in employee_dict[key] :

employee_dict is a dictionary of string-integer pairs, and you are trying to check whether e_ids is in employee_dict[key] , which is an int , not an iterable like a list, where you can check if an element is contained in it. employee_dict是字符串-整数对的字典,您正在尝试检查e_ids是否在employee_dict[key]中,这是一个int ,而不是像列表一样的可迭代对象,您可以在其中检查元素是否包含在其中。

Also, don't you mean employee_dict[key] = 0.8*value + value ?另外,您不是说employee_dict[key] = 0.8*value + value吗?

You confused with key and value variable of for loop您对 for 循环的keyvalue变量感到困惑

Try this:尝试这个:

employee_id_list = [27, 25, 98, 78, 66]
employee_dict = dict()
while True:
    x = input("Enter an key to continue and 'r' for result: ").lower()
    if x == 'r':
        break
    try:
        employee_id = int(input("Enter key the Employee id: "))
        salary = int(input(f"Enter the {employee_id}'s salary: "))
        employee_dict[employee_id] = salary
    except ValueError:
        print("Please Enter the Integers!")
        continue
for e_ids in employee_id_list:
    for key, value in employee_dict.items():
        if e_ids in employee_dict:
            employee_dict[key] = 0.8*value + value
print(employee_dict)

You have to write employee_dict[key] = 0.8*value+value rather than employee_dict[value] = 0.8*value+value .你必须写employee_dict[key] = 0.8*value+value而不是employee_dict[value] = 0.8*value+value

You can also write employee_dict[key] = value*1.8 rather than employee_dict[key] = 0.8*value+value .您也可以写employee_dict[key] = value*1.8而不是employee_dict[key] = 0.8*value+value

Putting the correct ideas of @Konny and @Sunderam together, and adding my own change for checking if statement, the answer is:将@Konny 和@Sunderam 的正确想法放在一起,并添加我自己的更改来检查 if 语句,答案是:

employee_id_list = [27, 25, 98, 78, 66]
    employee_dict = dict()

    while True:
        x = input("Enter an key to continue and 'r' for result: ").lower()
        if x== 'r':
            break
        try:
            employee_id = int(input("Enter key the Employee id: "))
            salary = int(input(f"Enter the {employee_id}'s salary: "))
            employee_dict[employee_id] = salary
        except ValueError:
            print("Please Enter the Integers!")
            continue

    print(employee_dict)
    for e_ids in employee_id_list:
        for key, value in employee_dict.items():
            if e_ids == key:    # This is my contribution
                employee_dict[key] = 1.8*value
    print(employee_dict)

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

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