Python 对于数据错误检查:有没有一种方法可以避免对列表使用字典

Python 对于数据错误检查:有没有一种方法可以避免对列表使用字典,python,dictionary,Python,Dictionary,我有如下数据: Observation 1 Type : 1 Color: 2 Observation 2 Color: 2 Resolution: 3 最初,我所做的是尝试创建一个csv,看起来像: 1,2 2,3 # Only problem here is that the data should look like this 1,2,\n ,2,3 # 我执行了以下操作: while linecache.getline(filename, curli

我有如下数据:

Observation 1  
Type : 1  
Color: 2  

Observation 2  
Color: 2  

Resolution: 3
最初,我所做的是尝试创建一个csv,看起来像:

1,2  
2,3  # Only problem here is that the data should look like this 1,2,\n ,2,3 #  
我执行了以下操作:

while linecache.getline(filename, curline):  
    for i in range(2):    
        data_manipulated = linecache.getline(filename, curline).rstrip()    
        datamanipulated2 = data_manipulated.split(":")  
        datamanipulated2.pop(0)  
        lines.append(':'.join(datamanipulated2))  

这是一个相当大的数据集,我试图找到方法来验证上述问题没有发生,这样我就可以通过检查适当地编译数据。我遇到过字典,但是,性能对我来说是个大问题,如果可能的话,我更喜欢列表(至少,我的理解是,字典可能会慢很多?)。我只是想知道是否有人对最快速、最可靠的方法有什么建议?

比如:

input_file = open('/path/to/input.file')
results = []
for row in file:
    m = re.match('Observation (\d+)', row)
    if m:
        observation = m.group(1)
        continue
    m = re.match('Color: (\d+)', row)
    if m:
        results.append((observation, m.group(1),))
        print "{0},{1}".format(*results[-1])

您可以使用预编译的正则表达式来加快速度。

嗯,您到底有什么问题?