【发布时间】:2011-07-27 23:59:02
【问题描述】:
我正在尝试将二维数组拆分为特定格式,但不知道最后一步。我的数据样本结构如下:
# Original Data
fileListCode = [['Seq3.xls', 'B08524_057'],
['Seq3.xls', 'B08524_053'],
['Seq3.xls', 'B08524_054'],
['Seq98.xls', 'B25034_001'],
['Seq98.xls', 'D25034_002'],
['Seq98.xls', 'B25034_003']]
我正在尝试拆分它,使其看起来像这样:
# split into [['Seq3.xls', {'B08524_057':1,'B08524_053':2, 'B08524_054':3},
# ['Seq98.xls',{'B25034_001':1,'D25034_002':2, 'B25034_003':3}]
字典键 1,2,3 基于条目的原始位置,从文件名第一次出现开始。为此,我首先创建了一个数组来获取所有唯一的文件名(任何 .xls 都是文件名)
tmpFileList = []
tmpCodeList = []
arrayListDict = []
# store unique filelist in a tempprary array:
for i in range( len(fileListCode)):
if fileListCode[i][0] not in tmpFileList:
tmpFileList.append( fileListCode[i][0] )
但是,我正在为下一步而苦苦挣扎。我想不出一个好的方法来提取代号(例如B08524_052),然后将它们转换成一个字典,并根据它们的位置进行索引。
# make array to store filelist, and codes with dictionary values
for i in range( len(tmpFileList)):
arrayListDict.append([tmpFileList[i], {}])
这段代码只产生[['Seq3.xls', {}], ['Seq98.xls', {}]];我不确定是否应该先生成结构然后尝试添加代码和字典值,或者是否有更好的方法。
--
编辑:我只是通过更改 fileListCode 中的值使示例更加清晰@
【问题讨论】:
-
您应该多描述一下 {'B08524_052':1,'B08524_053':2, 'B08524_054':3} 中的值的数字 1、2、3 是什么。
-
谢谢。因此,数字 1,2,3 是某个文件名的组中出现的索引。顺便说一句,请注意 1,2,3 不是字典的键,而是值
标签: python arrays dictionary