Python 列表理解中的条件

Python 列表理解中的条件,python,conditional-statements,list-comprehension,Python,Conditional Statements,List Comprehension,有没有一种方法可以更简洁地完成以下内容?如果我在条件中有很多值,我的脚本将变得非常不可读 当前代码 import xlrd filename = r'H:\Book1.xls' wb = xlrd.open_workbook(filename) sh1 = wb.sheet_by_index(0) data = [sh1.row_values(row) for row in range(sh1.nrows) if 1 in sh1.row_values(row) or 9 in sh1.ro

有没有一种方法可以更简洁地完成以下内容?如果我在条件中有很多值,我的脚本将变得非常不可读

当前代码

import xlrd  
filename = r'H:\Book1.xls'
wb = xlrd.open_workbook(filename)
sh1 = wb.sheet_by_index(0)
data = [sh1.row_values(row) for row in range(sh1.nrows) if 1 in sh1.row_values(row) or 9 in sh1.row_values(row) ]
print(data)
代码结果

[[1.0, 2.0, 3.0, '', 4.0, '', 6.0], ['', '', '', '', 9.0, '', '']]
所需语法

data = [sh1.row_values(row) for row in range(sh1.nrows) if 1,9 in sh1.row_values(row)]
此外,是否可以传入包含1、9等的列表或对象?

使用集合:

if {1,9}.intersection(sh1.row_values(row))

if{1,9}&set(sh1.row_值(row))
也许?您可以使用
any
如果有(sh1中的i.row_值(row)表示(1,9)中的i))
谢谢AnthonySottile和njzk2@Dot_Py你刚刚编辑了标签吗?嗯,这是一种获得复印编辑徽章的方法