【发布时间】:2014-08-28 13:01:03
【问题描述】:
我有一个正在排序的字符串列表。我用来排序的列表中有 12 个不同的键字符串。因此,我不想编写 12 个单独的列表推导,而是使用一个空列表列表和一个键字符串列表进行排序,然后使用 izip 执行列表推导。这是我正在做的事情:
>>> 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'>)
为什么这个进程返回空列表?
【问题讨论】: