我希望根据多个条件筛选字符串列表。将输出添加到新列表中。
list1 = ['Bike', 'Street Bike', 'Custom Bike', 'Custom Street Bike', 'City Bike',
'Cruiser Street Bike']
list2 = []为了本例的目的,我想提取包含“Street”和“Bike”的列表项
我尝试过以下方法,但它正在返回列表中的所有项
list2 = [s for s in list1 if 'Street' and 'Bike' in s]预期产出
print(list2)
['Street Bike', 'Custom Street Bike', 'Cruiser Street Bike']发布于 2021-12-14 05:15:25
只要换到
list2 = [s for s in list1 if 'Street' in s and 'Bike' in s]发布于 2021-12-14 05:17:07
以下是一个应该有效的版本
list1 = ['Bike', 'Street Bike', 'Custom Bike', 'Custom Street Bike', 'City Bike', 'Cruiser Street Bike']
list2 = [s for s in list1 if 'Street' in s and 'Bike' in s]
print(list2) # Should Give this ['Street Bike', 'Custom Street Bike', 'Cruiser Street Bike']https://stackoverflow.com/questions/70344049
复制相似问题