2011-10-05 63 views
0

輸入任意數量

datas2 = [[("01/01/2011", 1), ("02/02/2011", "No"), ("03/03/2011", 11)], 
[("01/01/2011", 2), ("03/03/2011", 22), ("22/22/2222", "no")], 
[("01/01/2011", 3), ("03/03/2011", 33), ("22/22/2222", "333")]] 

預期輸出

[("01/01/2011", 1, 2, 3), ("03/03/2011", 11, 22, 33)] 

[更新]

有人問我關於真實的數據和更多示例(歷史上的凌亂代碼):

A      B      C 
09.05.2011;1.561  12.04.2011;14.59  12.04.2011;1.5 
10.05.2011;1.572  13.04.2011;14.50  13.04.2011;1.5  
11.05.2011;1.603  14.04.2011;14.56  14.04.2011;1.5  
12.05.2011;1.566  15.04.2011;14.54  15.04.2011;1.5  
13.05.2011;1.563  18.04.2011;14.54  18.04.2011;1.5  
16.05.2011;1.537  19.04.2011;14.52  19.04.2011;1.5  
17.05.2011;1.528  20.04.2011;14.53  20.04.2011;1.5  
18.05.2011;1.543  21.04.2011;14.59  21.04.2011;1.5  
19.05.2011;1.537  26.04.2011;14.65  26.04.2011;1.6  
20.05.2011;1.502  27.04.2011;14.68  27.04.2011;1.6  
23.05.2011;1.503  28.04.2011;14.66  28.04.2011;1.6  
24.05.2011;1.483  29.04.2011;14.62  29.04.2011;1.6  
25.05.2011;1.457  02.05.2011;14.65  02.05.2011;1.6  
26.05.2011;1.491  03.05.2011;14.63  03.05.2011;1.6  
27.05.2011;1.509  04.05.2011;14.54  04.05.2011;1.5  
30.05.2011;1.496  05.05.2011;14.57  05.05.2011;1.5  
31.05.2011;1.503  06.05.2011;14.57  06.05.2011;1.5  
01.06.2011;1.509  09.05.2011;14.61  09.05.2011;1.6  
03.06.2011;1.412  10.05.2011;14.66  10.05.2011;1.6  
06.06.2011;1.380  11.05.2011;14.71  11.05.2011;1.7  
07.06.2011;1.379  12.05.2011;14.71  12.05.2011;1.7  
08.06.2011;1.372  13.05.2011;14.70  13.05.2011;1.7  
09.06.2011;1.366  16.05.2011;14.75  16.05.2011;1.7  
10.06.2011;1.405  17.05.2011;14.69  17.05.2011;1.6  
13.06.2011;1.400  18.05.2011;14.65  18.05.2011;1.6  
14.06.2011;1.414  19.05.2011;14.69  19.05.2011;1.6 
  • 如果我打開A和B,它將包含所有值。
  • 如果我解壓A,B和C,其將包含:

    [ [ 「09.05.2011」,1.561,14.61,1.6], [ 「10.05.2011」,1.572,14.66,1.6 ], [「11.05.2011」,1.603,14.71,1.7], [「12.05.2011」,1.566,14.71,1.7], [「13.05.2011」,1.563,14.70,1.7], [ 16.05.2011「,1.537,14.75,1.7], [」17.05.2011「,1.528,14.69,1.6], [」18.05.2011「,1.543,14.65,1.6], [」19.05.2011「, 1.537,14.69,1.6] ]

,從而有文件即列A,B,C,每個日期必須有儘可能多的價值......

+0

它必須驗證日期嗎? – JBernardo

+0

@JBernardo:在這種情況下,我只想讓元組解包工作。驗證將是另一個問題。 – hhh

+0

好吧,讓我看看我是否有這個權利。對於每個列表中找到的每個日期,您想要一個由(a)日期和(b)每個列表中每個元組的相關值組成的元組? –

回答

3
from collections import defaultdict 
import itertools 

d = defaultdict(list) 
for i,j in itertools.chain.from_iterable(datas2): 
    if not isinstance(j, str): 
     d[i].append(j) 

d將是一個字典,如:

{'01/01/2011': [1, 2, 3], '03/03/2011': [11, 22, 33]} 

因此,您可以稍後將其格式化爲d.items()

請注意,「22/22/2222」未驗證,但在for循環內很容易執行。

+0

他的示例數據集包括'333',他的輸出不包括那個,所以我想他只是想篩選出任何不是數字的東西。 – steveha

+0

@steveha是啊,現在我注意到了這一點......這個問題應該更具體... – JBernardo

+0

+1使用「Python化」迭代機械 – Peter

2

此代碼的編寫工作同樣適用於Python 2.x或Python 3.x.我用Python 2.7和Python 3.2測試了它。

from collections import defaultdict 

datas2 = [ 
    [("01/01/2011", 1), ("02/02/2011", "No"), ("03/03/2011", 11)], 
    [("01/01/2011", 2), ("03/03/2011", 22), ("22/22/2222", "no")], 
    [("01/01/2011", 3), ("03/03/2011", 33), ("22/22/2222", "333")] 
] 


def want_value(val): 
    """return true if val is a value we want to keep""" 
    try: 
     # detect numbers by trying to add to 0 
     0 + val 
     # no exception means it is a number and we want it 
     return True 
    except TypeError: 
     # exception means it is the wrong type (a string or whatever) 
     return False 

result = defaultdict(list) 

for lst in datas2: 
    for date, val in lst: 
     if want_value(val): 
      result[date].append(val) 

final_result = list(result.items()) 
print(final_result) 
+0

,我不喜歡你如何驗證「不」參數... – JBernardo

+0

嘛,平時的時候我想和數字的東西的工作,我只是價值強制使用'INT(VAL)一些'或'浮動( VAL)'。在這種情況下,他有一個「333」字符串的例子,他不想包括這個。我認爲一般認爲Pythonic使用異常來分類小麥,而且通常認爲用'isinstance()'檢查類型有點困難。這個函數會篩選出'no''','''','333','None',對象實例以及其他可能出現在其中的東西,同時傳遞類似數字的任何東西。你會推薦我做什麼? – steveha

+0

這就是爲什麼我不喜歡它......你阻止了一切,以後可能很難調試。但這只是我的觀點 – JBernardo