2016-03-03 76 views
4

我以這種形式有一個清單,輸入:冷凝多個列表綜合

open_info = ['Cube 1, 9:30am to 10:00am, Thursday, March 3, 2016', 'Cube 2, 5:00pm to 5:30pm, Thursday, March 3, 2016'] 

我想通過這些信息來分析建立在這種形式的一個新的列表:

open_times = [[9, 30, 'am'],[5, 0, 'pm']] 

與時針第一個指數,第二個分鐘,第三個指數am/pm。我只記錄每個列表元素的第一次值,因爲我處理的時間間隔總是30分鐘。

我已經通過下面的Python列表理解做到了這一點:

open_times = [x.split(",")[1].replace(" ","").split("to") for x in open_info] 
open_times = [x[0].split(":")+x[1].split(":") for x in open_times] 
open_times = [[int(x[0]),int(x[1][:2]),x[1][2:]] for x in open_times] 

我想知道是否有創建嵌套列表理解了所有這些的。我已經看過python文檔並閱讀了關於這個主題的一些博客,但是我仍然無法完成這個任務。

+1

你會反對非列表理解的解決方案?你知道嗎,以便人們能夠正確地消化實際的問題?作爲進一步的問題,您希望列表的長度是多少? – Makoto

+0

迴應上述評論,你能解釋爲什麼你想/需要使用列表解析而不是更傳統的(和可讀的)解析函數嗎? –

+0

我不會全都反對。我對python比較陌生,所以我試圖通過練習更多地瞭解我遇到的困難。我知道這個解決方案很混亂,所以我來到這裏。爲了回答你的其他問題,列表長度是可變的,但它總是在0到幾百之間。 @Makoto –

回答

2

您可以使用下列內容:

open_info = ['Cube 1, 9:30am to 10:00am, Thursday, March 3, 2016', 'Cube 2, 5:00pm to 5:30pm, Thursday, March 3, 2016'] 

answer = [[int(s.split(':',1)[0][-2:]), int(s.split(':')[1][:2]), 
      s.split(':')[1][2:4]] for s in open_info] 
print(answer) 

輸出

[[9, 30, 'am'], [5, 0, 'pm']] 

在這種情況下,但是,它可能更容易閱讀使用map代替list理解:

def func(s): 
    hour = int(s.split(':')[0][-2:]) 
    minute = int(s.split(':')[1][:2]) 
    suffix = s.split(':')[1][2:4] 
    return [hour, minute, suffix] 

answer = map(func, open_info) 
print(answer) 

輸出

[[9, 30, 'am'], [5, 0, 'pm']] 
2

要回答如何「鳥巢」名單解析,你可能會做這行1和2相結合的問題....

open_times = [y[0].split(":")+y[1].split(":") for y in [x.split(",")[1].replace(" ","").split("to") for x in open_info]] 

...但這是非常的混亂。這3條線更容易理解和更清潔。你也可以考慮把它寫成一系列的循環,因爲在理解中有很多內容會更清晰。

2

您可以使用正則表達式提取時間:

>>> import re 
>>> 
>>> [[int(val) if val.isdigit() else val for val in re.search(r'(\d+):(\d+)(am|pm)',item, re.I).groups()] for item in open_info] 
[[9, 30, 'am'], [5, 00, 'pm']] 

但不是,它可能引發AttributeError如果是的話,如果你不知道,你可以我們try-except表達不能的正則表達式匹配來處理錯誤。

times = [] 
for item in open_info: 
    match = re.search(r'(\d+):(\d+)(am|pm)',item, re.I) 
    try: 
     h, m, b = match.groups() 
    except (AttributeError, ValueError): 
     pass # or append a proper value to times, instead. 
    else: 
     times.append([int(h), int(m), b]) 
    times.append(match) 
+0

不錯,刪除評論;) –

2

而是推動全部列出理解expresion您可以簡單地創建處理功能的邏輯。

爲了提高可讀性,我重命名了一些值。

def extract(s): 
    time_from, time_to = s.split(",")[1].replace(" ", "").split("to") 
    hour, min_am_pm = time_from.split(":") 
    min = min_am_pm[:2] 
    am_pm = min_am_pm[2:] 
    return [int(hour), int(min), am_pm] 


open_info = ['Cube 1, 9:30am to 10:00am, Thursday, March 3, 2016', 'Cube 2, 5:00pm to 5:30pm, Thursday, March 3, 2016'] 
open_times = [extract(x) for x in open_info] 
-1
from csv import reader 
answer = [[int(a), int(b[:2]), c[2:]] for a, b, c in (inf[1].split(":") 
      for inf in reader(open_info, skipinitialspace=True))] 

裏面居然符合您的預期輸出:

[[9, 30, 'am'], [5, 0, 'pm']] 

一個簡單的功能將是一個更好的主意,也不需要不斷保持反覆分裂同一行:

def spl(l): 
    for inf in l: 
     a, b, c = inf.split(", ", 2)[1].split(":", 2) 
     yield [int(a), int(b[:2]), c[2:]] 

print(list(spl(open_info))) 

輸出:

[[9, 30, 'am'], [5, 0, 'pm']] 

或者讓CSV LIB解析項目:

from csv import reader 

def spl(l): 
    for inf in reader(l, skipinitialspace=True): 
     a, b, c = inf[1].split(":", 2) 
     yield [int(a), int(b[:2]), c[2:]] 
print(list(spl(open_info)))