python - 使用聚合函数过滤输出
问题描述
我有一个函数,它返回一个正在相互比较的字符串列表和每个观察的分数。我正在尝试将输出过滤为大于或等于 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}')
解决方案
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']
推荐阅读
- c# - 比较两个文本相同的字符串,只替换字符串后面
- python - 通过 Pyqt5 python 保存文件
- r - R - 使用 purrr::pmap() 进行逐行迭代
- php - 数据库抽象层——那是什么?
- r - 关于特定条件下 group_by 的总和
- java - 陈旧元素的 JSON 状态映射 - Selenium 和 Java
- python - Anaconda (Python) - Windows 10 上的 Cmder 集成
- pandas - 在 Chartify 中为分组条形图添加图例
- keras - 在 keras 中连接多个 CNN 模型
- scala - 带有函数式编程的 ConcurrentHashMap。暂停 unsafeRun 安全吗?