简体   繁体   English

使用聚合函数过滤输出

[英]Using an aggregate function to filter the output

I have a function that returns a list of strings that are being compared to one another and a score for each observation. 我有一个函数,该函数返回要相互比较的字符串列表和每个观察值的分数。 I am trying to filter the output to scores that are greater than or equal to 80. When I applied the .filter it returned a list of true or false when I wanted a list of strings with a score of 80 or more. 我试图将输出筛选为分数大于或等于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}')

Use zip() with a list-comprehension: zip()与list-comprehension一起使用:

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']

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

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