如何过滤一组字典,以便我可以根据*另一个*键是否满足特定条件从一个键中提取值?

How do I filter a group of dictionaries so that I can extract values from one key based on whether *another* key meets certain conditions?

这里是业余到中级 Python 程序员。

我有一个 Excel .csv,我正试图从中提取一些特定数据。我已经设法将原始数据提取到 Python 中的字典列表中。数据由大约十几个不同的键组成(Excel sheet中的列),但我需要的只是两个键中的数据。

我想根据second键中对应的值是否满足一定条件,提取其中一个键的值。两个key分别是“UnitId”和“A5000 Overall satisfaction”(这是客服相关的数据),我想提取每一个对应A5000值为5的UnitId值。

问题到现在为止,我尝试执行此操作的 none 个函数设法提取 任何东西 ,并且总是产生一个空字典或列表(取决于我尝试的方法)。我最近的函数尝试给了我 一些 进步,但结果并不包含我想要的所有数据。

我试图简单地提取每本 A5000 值为 5 的字典,希望一旦我将列表缩小到我需要的字典,就可以从那里提取 UnitId。但是,它没有给我所有 A5000 值为 5 的字典,而是给了我 A5000 值为 5 的 单个 字典结果的所有键。

这是我最近尝试的代码:

import csv

compiled_data = []
all_5_ratings = dict()
with open("C:\%%%%%%%%%%%\CW test file.csv") as CW_test_file:
#Here is the code I used to display the raw data in the console
    CWtest_dict = csv.DictReader(CW_test_file)
    for row in CWtest_dict:
        compiled_data.append(row)
    print(compiled_data)
    print("################################################")
#Here is my most recent attempted function to extract the desired data:
    for dict in compiled_data:
        for key, value in dict.items():
            if key == "A5000 Overall satisfaction" and value == "5":
                all_5_ratings.update(dict)
            else:
                pass
    print(all_5_ratings)
    #Below is a failed previous attempt
    all_fives = [x for x in compiled_data if x["A5000 Overall satisfaction"] >= "5"]
    print(all_fives)

这里有一个pastebin link到compiled_data的样例供参考(这里直接post太长了,而且完整的东西对于免费的pastebin用户来说太大了):

这里是我用现有代码得到的结果:

{
    "VisitDate": "10/28/2021 21:58",
    "UnitId": "2",
    "A4000 Type of visit": "2",
    "A5000 Overall satisfaction": "5",
    "A6000 Likelihood to return": "5",
    "A7000 Likelihood to recommend": "5",
    "A8000 Value for Price": "5",
    "A9000 Friendliness of host/hostess": "5",
    "A10000 Wait time to be seated": "5",
    "A11000 Friendliness of server": "4",
    "A12000 Wait time for drinks": "5",
    "A13000 Wait time for food": "5",
    "A14000 Server's menu knowledge": "5"
}

最后,我的问题是:如何提取所有个对应A5000值为5的UnitId值?

你的问题是你在需要列表的地方使用了字典。 all_5_ratings.update(dict) 正在覆盖您之前的任何条目。您可以使用列表,然后使用 'append' 而不是像您第一次读取数据时那样使用 'update'。

我不确定我是否明白你想做什么。但此脚本将 A5000 为 5 的所有评论放入结果数组中:

data = [{'VisitDate': '10/11/2021 11:26', 'UnitId': '23', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '2', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 11:27', 'UnitId': '14', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '3', 'A8000 Value for Price': '3', 'A9000 Friendliness of host/hostess': '3', 'A10000 Wait time to be seated': '2', 'A11000 Friendliness of server': '2', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 11:38', 'UnitId': '22', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 11:52', 'UnitId': '11', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 12:02', 'UnitId': '14', 'A4000 Type of visit': '1', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 12:09', 'UnitId': '18', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 12:11', 'UnitId': '23', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 12:12', 'UnitId': '23', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '3', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 12:17', 'UnitId': '14', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 12:27', 'UnitId': '23', 'A4000 Type of visit': '1', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '3', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '3', 'A9000 Friendliness of host/hostess': '3', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '2', 'A12000 Wait time for drinks': '3', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:31', 'UnitId': '1', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:34', 'UnitId': '1', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:39', 'UnitId': '18', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 12:41', 'UnitId': '11', 'A4000 Type of visit': '1', 'A5000 Overall satisfaction': '3', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:44', 'UnitId': '17', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 12:49', 'UnitId': '2', 'A4000 Type of visit': '1', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 12:49', 'UnitId': '17', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '3', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '3', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:54', 'UnitId': '17', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:59', 'UnitId': '18', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 13:00', 'UnitId': '18', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 13:19', 'UnitId': '1', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 13:23', 'UnitId': '14', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '3', 'A11000 Friendliness of server': '3', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '3', "A14000 Server's menu knowledge": '1'}, {'VisitDate': '10/11/2021 13:36', 'UnitId': '2', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '3', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '2', 'A8000 Value for Price': '3', 'A9000 Friendliness of host/hostess': '3', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '3', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 13:37', 'UnitId': '18', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 13:38', 'UnitId': '2', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 13:41', 'UnitId': '16', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '3', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '3', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '2'}, {'VisitDate': '10/11/2021 13:45', 'UnitId': '21', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 13:55', 'UnitId': '16', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '3', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '3', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '3', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 14:07', 'UnitId': '23', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '3', 'A11000 Friendliness of server': '3', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}]
result = []
for row in data:
    if row['A5000 Overall satisfaction'] == '5':
        result.append(row)

print(result)

重新阅读问题后,这里是将 UnitId 放入 A5000 为 5 的数组中的版本

data = [{'VisitDate': '10/11/2021 11:26', 'UnitId': '23', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '2', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 11:27', 'UnitId': '14', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '3', 'A8000 Value for Price': '3', 'A9000 Friendliness of host/hostess': '3', 'A10000 Wait time to be seated': '2', 'A11000 Friendliness of server': '2', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 11:38', 'UnitId': '22', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 11:52', 'UnitId': '11', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 12:02', 'UnitId': '14', 'A4000 Type of visit': '1', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 12:09', 'UnitId': '18', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 12:11', 'UnitId': '23', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 12:12', 'UnitId': '23', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '3', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 12:17', 'UnitId': '14', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 12:27', 'UnitId': '23', 'A4000 Type of visit': '1', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '3', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '3', 'A9000 Friendliness of host/hostess': '3', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '2', 'A12000 Wait time for drinks': '3', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:31', 'UnitId': '1', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:34', 'UnitId': '1', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:39', 'UnitId': '18', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 12:41', 'UnitId': '11', 'A4000 Type of visit': '1', 'A5000 Overall satisfaction': '3', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:44', 'UnitId': '17', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 12:49', 'UnitId': '2', 'A4000 Type of visit': '1', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 12:49', 'UnitId': '17', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '3', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '3', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:54', 'UnitId': '17', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}, {'VisitDate': '10/11/2021 12:59', 'UnitId': '18', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 13:00', 'UnitId': '18', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 13:19', 'UnitId': '1', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 13:23', 'UnitId': '14', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '3', 'A11000 Friendliness of server': '3', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '3', "A14000 Server's menu knowledge": '1'}, {'VisitDate': '10/11/2021 13:36', 'UnitId': '2', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '3', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '2', 'A8000 Value for Price': '3', 'A9000 Friendliness of host/hostess': '3', 'A10000 Wait time to be seated': '4', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '3', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 13:37', 'UnitId': '18', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 13:38', 'UnitId': '2', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 13:41', 'UnitId': '16', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '3', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '3', 'A11000 Friendliness of server': '4', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '2'}, {'VisitDate': '10/11/2021 13:45', 'UnitId': '21', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '5', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '5', 'A8000 Value for Price': '5', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '5', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '4'}, {'VisitDate': '10/11/2021 13:55', 'UnitId': '16', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '3', 'A6000 Likelihood to return': '5', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '3', 'A9000 Friendliness of host/hostess': '5', 'A10000 Wait time to be seated': '5', 'A11000 Friendliness of server': '5', 'A12000 Wait time for drinks': '3', 'A13000 Wait time for food': '5', "A14000 Server's menu knowledge": '5'}, {'VisitDate': '10/11/2021 14:07', 'UnitId': '23', 'A4000 Type of visit': '2', 'A5000 Overall satisfaction': '4', 'A6000 Likelihood to return': '4', 'A7000 Likelihood to recommend': '4', 'A8000 Value for Price': '4', 'A9000 Friendliness of host/hostess': '4', 'A10000 Wait time to be seated': '3', 'A11000 Friendliness of server': '3', 'A12000 Wait time for drinks': '4', 'A13000 Wait time for food': '4', "A14000 Server's menu knowledge": '3'}]
result = []
for row in data:
    if row['A5000 Overall satisfaction'] == '5':
        result.append(row["UnitId"])

print(result)

这里是单行的:

result = [row['UnitId'] for row in data if row['A5000 Overall satisfaction'] == '5']