我有我正在排序的字符串列表。列表中有12個不同的關鍵字符串用於排序。因此,我不想編寫12個單獨的列表解析,而是使用空列表和關鍵字符串列表進行排序,然後使用izip執行列表解析。下面是我在做什麼:Python IZIP list comprehension返回空列表
>>> from itertools import izip
>>> tran_types = ['DDA Debit', 'DDA Credit']
>>> tran_list = [[] for item in tran_types]
>>> trans = get_info_for_branch('sco_monday.txt',RT_NUMBER)
>>> for x,y in izip(tran_list, TRANSACTION_TYPES):
x = [[item.strip() for item in line.split(' ') if not item == ''] for line in trans if y in line]
>>> tran_list[0]
[]
我想看到的輸出更像是以下幾點:
>>> tran_list[0]
[['DDA Debit','0120','18','3','83.33'],['DDA Debit','0120','9','1','88.88']]
輸出沒有道理給我; izip返回的對象是列表和字符串
>>> for x,y in itertools.izip(tran_list, TRANSACTION_TYPES):
type(x), type(y)
(<type 'list'>, <type 'str'>)
(<type 'list'>, <type 'str'>)
爲什麼此過程返回空列表?