2014-06-19 101 views
0

我試圖在Python中創建list,其中的值從活動的excel表中拉出。我希望它從excel文件中提取步驟#值並將其附加到列表中,同時還包括該元素的哪個編號。例如,1_1它第一次拉1,1_2第二次,1_3第三,等我的代碼如下......計算列表中的元素並追加列表

import win32com.client 
xl = win32com.client.Dispatch("Excel.Application") 
CellNum = xl.ActiveSheet.UsedRange.Rows.Count 
Steps = [] 
for i in range(2,CellNum + 1): #Create load and step arrays in abaqus after importing from excel 
if str(int(xl.Cells(i,1).value))+('_1' or '_2' or '_3' or '_4' or '_5' or '_6') in Steps: 
    StepCount = 1 
    for x in Steps: 
     if x == str(int(xl.Cells(i,1).value))+('_1' or '_2' or '_3' or '_4' or '_5' or '_6'): 
      StepCount+=1 
    Steps.append(str(int(xl.Cells(i,1).value))+'_'+str(StepCount)) 
else: 
    Steps.append(str(int(xl.Cells(i,1).value))+'_1') 

據我所知,沒有Excel文件,該程序將無法運行對於你們任何人,但我只是想知道這是否是我錯過的一些簡單的錯誤。當我運行這個時,StepCount不會高於2,所以我收到一堆1_2,2_2,3_2等元素。我已在下面發佈了我的結果列表。

>>> Steps 
['1_1', '2_1', '3_1', '4_1', '5_1', '6_1', '7_1', '8_1', '9_1', '10_1', '11_1', '12_1', 
'13_1', '14_1', '1_2', '14_2', '13_2', '12_2', '11_2', '10_2', '2_2', '3_2', '9_2', 
'8_2', '7_2', '6_2', '5_2', '4_2', '3_2', '2_2', '1_2', '2_2', '3_2', '4_2', '5_2', 
'6_2', '7_2', '8_2', '9_2', '10_2', '11_2', '12_2', '13_2', '14_2', '1_2', '2_2'] 

編輯#1:所以,如果('_1' or '_2' or '_3' or '_4' or '_5' or '_6')將永遠只用_1,是這行代碼是與我的櫃檯搞亂?

if x == str(int(xl.Cells(i,1).value))+('_1' or '_2' or '_3' or '_4' or '_5' or '_6'): 

,因爲它只使用_1,只會算1_1查不1_2, 1_3, 1_4, etc

編輯#2:現在我用下面的代碼。我的輸入列表也在下面。

from collections import defaultdict 
StepsList = [] 
Steps = [] 
tracker = defaultdict(int) 

for i in range(2,CellNum + 1): 
    StepsList.append(int(xl.Cells(i,1).value)) 

>>> StepsList 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 14, 13, 12, 11, 10, 2, 3, 9, 8, 
7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2] 

for cell in StepsList: 
    Steps.append('{}_{}'.format(cell, tracker[cell]+1)) # This is +1 because the  tracker starts at 0 
    tracker[cell]+=1 

我得到以下錯誤:ValueError: zero length field name in formatfor cell in StepsList:迭代方框

編輯#3:得到它的工作。出於某種原因,它不喜歡

Steps.append('{}_{}'.format(cell, tracker[cell]+1)) 

所以我就改成了

for cell in StepsList: 
    tracker[cell]+=1 
    Steps.append(str(cell)+'_'+str(tracker[cell])) 

感謝您的幫忙!

回答

1

這條線:

if str(int(xl.Cells(i,1).value))+('_1' or '_2' or '_3' or '_4' or '_5' or '_6') in Steps: 

沒有做什麼,你認爲它。 ('_1' or '_2' or '_3' or '_4' or '_5' or '_6')始終返回'_1'。它不會遍歷該系列的or值尋找匹配。

沒有看到預期的輸入與預期的輸出,很難指出你正確的方向去實際得到你想要的代碼,但可能你想要利用itertools.product或其他組合方法之一itertools

更新

根據您的意見,我認爲這是解決你的問題的一種方式。假設下面的輸入列表:

in_list = [1, 1, 1, 2, 3, 3, 4] 

你可以做到以下幾點:

from collections import defaultdict 

tracker = defaultdict(int) # defaultdict is just a regular dict with a default value at new keys (in this case 0) 
steps = [] 

for cell in in_list: 
    steps.append('{}_{}'.format(cell, tracker[cell]+1)) # This is +1 because the tracker starts at 0 
    tracker[cell]+=1 

結果:

>>> steps 
['1_1', '1_2', '1_3', '2_1', '3_1', '3_2', '4_1'] 

有可能更有效的方式來做到這一點使用的itertools組合,但這種方式肯定是最直接的

+0

好的,但如果1_1是在l ist,那麼它應該設置StepCount = 1並遍歷列表元素來計算每次出現1_1,1_2,1_3,否?我的問題似乎是,StepCount或者只是在迭代過程中識別出其中的一個元素,或者StepCount始終將其重置爲1.前者似乎更有可能,但我不明白爲什麼。我是Python的初學者。 – Ryan

+0

不,你會第一次得到1_1,因爲列表中不包含「1_1」,然後是「1_2」,因爲「1_1」已經在列表中,然後再也不會。您唯一要搜索的元素是「X_1」。 – aruisdante

+0

對不起,我解釋得不好。你的第一個錯誤在你的條件下基本上有同樣的錯誤。所以基本上,它只是在尋找''X_1'',一旦找到它就會添加''X_2'',然後引導。 – aruisdante