2015-05-23 48 views
0

这是我在Excel工作表中查找特定值位置的函数。Uncool List Comprehension

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的建议的价值。拥有有意义的名字对于像我这样的初学者来说是一个不同的世界。

+0

每行中是否只有一个单元格的值为“CO”? – inspectorG4dget

+0

你可以发布一个实际运行的独立示例;与进口和所有。这可以让你更容易地尝试你的方法。 –

+0

@ inspectorG4dget'CO'可以在一行中出现多次。 – Kristjan

回答

0

在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()增加了列表的(或可迭代)到另一列表中的元素的方法。这就像多次拨打append()

从Python 2.7开始enumerate()需要一个可选的开始索引。 所以你不需要在x +1y + 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 
0

我首先想到的是要做到这一点:

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) 
0

而不是

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和后者的模式会像英文一样阅读。

相关问题