首页 > 解决方案 > 使用聚合函数过滤输出

问题描述

我有一个函数,它返回一个正在相互比较的字符串列表和每个观察的分数。我正在尝试将输出过滤为大于或等于 80 的分数。当我应用 .filter 时,当我想要一个分数为 80 或更高的字符串列表时,它返回一个 true 或 false 列表。

#Assign your list1
Test_addrs = my_list1
#Assign your List2 and build the nested loop
target_addr = my_list2
for addr in Test_addrs:
    for target in target_addr:
        distance = string_match(target, addr, ratio_calc = True)
        #write results to a txt file
        mylist.append(f'{target}, {addr}, {distance}')

标签: python

解决方案


zip()与列表理解一起使用:

strings_lst = ['a', 's', 'd', 'f', 'g']
scores_lst = [21, 24, 90, 54, 109]

print([x for x, y in zip(strings_lst, scores_lst) if y >= 80])
# ['d', 'g']

推荐阅读