下面是我的函数,用于在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
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的美丽非常着迷,希望能有任何帮助使我自己的代码更有魅力。
谢谢!
酷列表理解:
def get_data_locations():
""" Find data locations from the first sheet in document. """
first_sheet = Range('Sheet1', 'A1:Z200').value
coordinates = []
for row_i, row in enumerate(first_sheet, 1):
coordinates.extend([(row_i, col_i) for col_i, col in enumerate(row, 1) if col == 'CO'])
return coordinates很大的感谢麦克穆勒谁提出了这个解决方案!我重新发布了他的代码的一个稍微修改的版本,以展示BBrown建议的价值。对于像我这样的初学者来说,拥有有意义的名字会有很大的不同。
发布于 2015-05-23 05:54:02
在Python2.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()将列表(或可迭代)的元素添加到另一个列表中。这就像对append()的多个调用。
从Python2.7开始,enumerate()接受一个可选的开始索引。所以你不需要+1在x +1和y + 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发布于 2015-05-23 06:06:18
我的第一个想法是这样做:
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)发布于 2015-05-23 06:12:36
而不是
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更有意义的变量名,后一种模式读起来就像英语一样。
https://stackoverflow.com/questions/30409352
复制相似问题