0

我正在解析 100 多个遵循相同格式的文件。例如,对于一个文件,我有一个由列表中的两个键和多个值组成的字典。

   temp2 = {
  '0.1995': ['X3:GATE', 'IN1', 'IN1', 'X7:GATE', 'X4:GATE', 'IN2', 'IN2', 'X8:GATE'],
  '0.399': ['X4:GATE', 'Y', 'Y', 'X3:GATE', 'Y', 'X8:SRC', 'X1:GATE', 'IN0', 'IN0', 'X5:GATE']

下面是创建 temp2 字典的代码:

#temp_delvt_list, temp_pin_list is from an external function
for delvt, pin in zip(temp_delvt_list, temp_pin_list):
                temp[delvt].append(pin) #dictionary with delvt and pin names for cell

            for line in SPFFile1:
#                   print line
                for delvt_keys,pin_values in temp.items():
#                   if re.match('[RC].*(?:i%s)' % '|'.join(pin_values), line): //couldn't get working
                    if re.search('(?i)' '|'.join(pin_values),line) and re.match('R',line):
                        #print delvt_keys
                        line_split = line.split(" ")
                        temp_list.append(delvt_keys)
                        temp_list.append(delvt_keys) #duplicated to create balanced list with temp_search_list
                        temp_search_list.append(line_split[1])
                        temp_search_list.append(line_split[2])
#                       print temp_list
#                       print temp_search_list
            SPFFile1.close()
            for d, p in zip(temp_list, temp_search_list):
                #print temp2[d]
            #   print re.sub(':SRC',':ISI',p)
                temp2[d].append(p) #dictionary with delvt and pin names for cell
#           print temp2

}

我正在尝试搜索每个键的值,然后搜索没有 Y 值的键 (0.1995),删除为该键出现的所有值,这些值也出现在具有 Y 值的键 (0.399) 中。键值为没有硬编码,可能会改变。

输出应该是:

temp2 = {
  '0.1995': ['X3:GATE', 'IN1', 'IN1', 'X7:GATE', 'X4:GATE', 'IN2', 'IN2', 'X8:GATE'],
  '0.399': ['Y', 'Y','Y', 'X8:SRC', 'X1:GATE', 'IN0', 'IN0', 'X5:GATE']
}

这是我到目前为止想出的代码:

        for test_d, test_p in temp2.items():
            if not re.search('Y', ' '.join(test_p)) :
                for x in test_p:
                    print x
            else:
                for y in test_p:
                    print y

我认为我应该用我比较的任何一个列表替换打印语句。

4

1 回答 1

2

可能你正在寻找

>>> temp2 = {
  '0.1995': ['X3:GATE', 'IN1', 'IN1', 'X7:GATE', 'X4:GATE', 'IN2', 'IN2', 'X8:GATE'],
  '0.399': ['X4:GATE', 'Y', 'Y', 'X3:GATE', 'Y', 'X8:SRC', 'X1:GATE', 'IN0', 'IN0', 'X5:GATE']
}
>>> _set = set(temp2['0.1995'])
>>> temp2['0.399'] = [e for e in temp2['0.399'] if e not in _set]
>>> import pprint
>>> pp = PrettyPrinter(indent = 4)
>>> pp.pprint(temp2)
{   '0.1995': [   'X3:GATE',
                  'IN1',
                  'IN1',
                  'X7:GATE',
                  'X4:GATE',
                  'IN2',
                  'IN2',
                  'X8:GATE'],
    '0.399': ['Y', 'Y', 'Y', 'X8:SRC', 'X1:GATE', 'IN0', 'IN0', 'X5:GATE']}
>>> 
于 2013-01-16T03:53:10.677 回答