python - 以列表为值迭代两个字典

标签 python dictionary key-value

我有关于员工打卡时间时钟的数据。

员工可以通过自动化流程(通过他的 key 卡或指纹)或通过简单的网络表格手动报告他的开始时间。

问题是有些员工不小心用不止一种方法报告了时间

输入流数据来自两个字典,其值如下:

如果员工在特定日期登录,我正在尝试迭代两个字典并计数(不是总和)。

def count_type(logins, punch_clock_data):
    counter = 0
    ans_dict = {}
    for punch_clock_key, punch_clock_value in punch_clock_data.items():
        for element in punch_clock_value:
            if element not in ans_dict:
                l1 = logins.get(element)
                for i in range(len(l1)):
                    if i == 1:
                        counter+=1
                        break
            ans_dict[punch_clock_key] = counter
        print(f'this employee was at work {counter} days this week by {punch_clock_key} login')
    return ans_dict


# each item in the array is a representation of weekday. for example, 
# on Sunday there was not any log in data.
# on Tuesday, this employee reported login both on web form and by 
# card(flag = 1) etc.


logins = { 'card'        :[0, 1, 1, 0, 0, 0, 0],
           'fingerprint' :[0, 0, 0, 1, 1, 0, 0],
           'web form'    :[0, 0, 1, 1, 0, 1, 1]
}


# dictionary contains data on types of punch clock
punch_clock_data  = { 'automated' :['card', 'fingerprint'],
                      'manual'    :['web form'],
                      'all_types' :['card', 'fingerprint', 'web form']
}

res = count_type(logins, punch_clock_data)
print(res)

我的输出不符合预期。 这是我的输出

{'automated': 2, 'manual': 3, 'all_types': 6}

但我想得到的是:

{'automated': 4, 'manual': 4, 'all_types': 6}
  • automated 应为 4,因为有四天 flag 等于 1(星期一、星期二通过卡,星期三、星期四通过指纹)
  • manual 应为 4,因为有四天 flag 等于 1(星期二、星期三、星期五、星期六)
  • all_types 应为 6,因为这六天中至少有一个标志等于 1

我认为我的问题是我需要按索引而不是按值迭代所有工作日列表。 对于一周中的每一天,获取正确的索引并计算它(垂直而不是水平)

最佳答案

看起来您只想在员工通过特定打卡时钟类别中的一种以上方法登录的日子里计算一次登录。您可以将每个类别的登录方法列表压缩在一起,并测试是否有任何一个可以登录。

logins = {'card': [0, 1, 1, 0, 0, 0, 0], 'fingerprint' :[0, 0, 0, 1, 1, 0, 0], 'web form': [0, 0, 1, 1, 0, 1, 1]}
punch_clock_data = { 'automated': ['card', 'fingerprint'], 'manual': ['web form'], 'all_types': ['card', 'fingerprint', 'web form']}

results = {}
for group, keys in punch_clock_data.items():
    results[group] = sum(any(t) for t in zip(*[logins[k] for k in keys]))

print(results) 
# {'automated': 4, 'manual': 4, 'all_types': 6}

根据您的评论请求一个版本,以便更容易看到所涉及的步骤。这里有一些分割。

results = {}
for group, keys in punch_clock_data.items():
    # list of lists of logins for each method in the category
    all_logins = [logins[k] for k in keys]

    # zip all_logins by day of the week
    logins_per_day = zip(*all_logins)

    # add 1 for each day where any of the values in the tuple are not zero
    results[group] = sum(any(t) for t in logins_per_day)

关于python - 以列表为值迭代两个字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58203163/

相关文章:

python - 二维矩阵 : Finding and deleting columns that are subsets of other columns

c# - 连接两个属性以在 DropDownList 中显示

python - 如何从 dict 获取值列表?

C:从带有链表和哈希表的txt文件实现的字典无法找到文件中存在的单词

java - 如何将键值对从jsp传递到servlet?

python - 从 Tensorflow 摘要获取 CSV

python - 使用 python 解析具有异常分隔符的文本文件

python - 使用Python在PC中选择随机目录

PHP:对数组进行排序

Python:如何创建包含字典键和值对的列表