2015-05-23 51 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和後者的模式會像英文一樣閱讀。

相關問題