Python 非冷却清单理解
下面是我在Excel工作表中查找特定值位置的函数Python 非冷却清单理解,python,excel,python-3.x,list-comprehension,xlwings,Python,Excel,Python 3.x,List Comprehension,Xlwings,下面是我在Excel工作表中查找特定值位置的函数 from xlwings import Workbook, Range workbook = Workbook('workbook location') def get_data_locations(): """ Find data locations from the first sheet in document. """ first_sheet = Range('Sheet1', 'A1:Z200').value
from xlwings import Workbook, Range
workbook = Workbook('workbook location')
def get_data_locations():
""" Find data locations from the first sheet in document. """
first_sheet = Range('Sheet1', 'A1:Z200').value
coordinates = []
for x in range(len(first_sheet)):
coordinate_pair = [[x + 1, y + 1] for y, z in enumerate(first_sheet[x]) if z == 'CO']
if not coordinate_pair == []:
coordinates.append(coordinate_pair)
coordinates = [cp for [cp] in coordinates]
print(coordinates)
就我所见,代码按预期工作。然而,出于某种原因,我觉得我在这里杀小狗
例如,在这里添加嵌套列表似乎是多余的
[x + 1, y + 1]
需要另一行代码来消除这种愚蠢
coordinates = [cp for [cp] in coordinates]
我真的被Python的美迷住了,如果有人能帮助我使自己的代码更迷人一点,我将不胜感激
谢谢
酷列表理解:
非常感谢Mike Müller提出了这个解决方案!我重新发布了他的代码的一个稍加修改的版本,以体现BBrown建议的价值。对于像我这样的初学者来说,有一个有意义的名字会有很大的不同。在Python 2.7及更高版本中,这应该是可行的:
def get_data_locations():
""" Find data locations from the first sheet in document. """
first_sheet = Range('Sheet1', 'A1:Z200').value
coordinates = []
for x, row in enumerate(first_sheet, 1):
coordinates.extend([[x, y] for y, z in enumerate(row, 1) if z == 'CO'])
return coordinates
extend方法将列表或iterable的元素添加到另一个列表中。这就像要追加的多个调用一样
从Python2.7开始,enumerate采用可选的开始索引。
所以你不需要x+1和y+1中的+1
相应的一行程序不再是真正的一行程序:
def get_data_locations():
""" Find data locations from the first sheet in document. """
first_sheet = Range('Sheet1', 'A1:Z200').value
coordinates = [[x, y] for x, row in enumerate(first_sheet, 1)
for y, z in enumerate(row, 1) if z == 'CO']
return coordinates
我的第一个想法是:
def get_data_locations():
""" Find data locations from the first sheet in document. """
first_sheet = Range('Sheet1', 'A1:Z200').value
coordinates = []
for x,row in enumerate(first_sheet):
coordinate_pair = [[x+1, y+1] for y,z in enumerate(row) if z == 'CO']
if coordinate_pair:
coordinates.append(coordinate_pair)
coordinates = [cp for [cp] in coordinates]
print(coordinates)
但在我看来,CO只出现在excel表格每行的一个单元格中。如果是这样,我会这样做:
def get_data_locations():
""" Find data locations from the first sheet in document. """
first_sheet = Range('Sheet1', 'A1:Z200').value
coordinates = []
for x,row in enumerate(first_sheet):
for row y,z in enumerate(row):
if z != "CO": continue
coordinates.append([x+1, y+1])
break
print(coordinates)
当然,嵌套for循环始终有一个单行程序:
def get_data_locations():
""" Find data locations from the first sheet in document. """
first_sheet = Range('Sheet1', 'A1:Z200').value
coordinates = [list(itertools.chain.from_iterable([[x+1,y+1] for y,z in enumerate(row) if z=="CO"])) for x,row in enumerate(first_sheet)]
print(coordinates)
而不是
for x in range(len(list_of_some_things)):
do_something_with(list_of_some_things[x])
使用模式
for thing in list_of_some_things:
do_something(thing)
使用比x更有意义的变量名,后一种模式将读作英语。每行中是否只有一个单元格的值为CO?您能否发布一个实际运行的自包含示例;进口和所有的。这将使试验您的方法更加容易。@inspectorG4dget“CO”可以在一行中出现多次。@MikeMüller添加了您要求的代码。在这种情况下,您确定您的代码符合您的要求吗?列表comp似乎表明CO每行只出现一次
for thing in list_of_some_things:
do_something(thing)