python - 如何检查列表中是否存在多个逗号分隔的字符串
问题描述
我正在尝试搜索列表中是否存在逗号分隔的字符串,例如列表中是否存在多个字符串。如何执行此操作
我试过这样
location = ["Bangalore", "Delhi"]
locations_list = ["Bangalore", "Delhi", "Mumbai", "Hyderabad", "Uttar Pradesh"]
if any(location in str for str in locations_list ):
print("location present in locations list")
else:
print("location not found")
解决方案
如果您只对是否存在任何元素感兴趣,我建议您使用集合交集:
if set(location) & set(locations_list):
print("location present in locations list")
else:
print("location not found")
编辑:
如果您想检查是否所有位置location
都在location_list
,我建议使用集合的issubset
方法:
if set(location).issubset(set(locations_list)):
print("location present in locations list")
else:
print("location not found")
推荐阅读
- python - Python IPify VPN 检查器 API KeyError
- serial-port - 如何将整数/数字转换为十六进制数据
- java - 尝试在while循环中使用多个输入时Java给出错误
- java - MySQL ConnectException:连接被拒绝:连接
- linux - 从排列和字符组合中删除字符
- java - 码头 10:需求能力:osgi.serviceloader;过滤器:=“(osgi.serviceloader=org.eclipse.jetty.webapp.Configuration)”
- java - 是否可以让 JInternalFrame 表现得像 JFrame?
- javascript - 为每个用户制作独特的个人资料?
- vb.net - 为什么我的数据库命令总是调用 sp_describe_first_result_set?
- synchronization - 对 VK_SUBPASS_EXTERNAL 和 VkSemaphore 感到困惑