如何检索有序键/对集合的上一项?

时间:2013-11-02 05:26:38

标签: python python-2.7

学习一门新语言最令人沮丧的事情之一就是你不知道该怎么做。我想执行,应该是一个简单的任务,但我正在努力实现它。

我想跟踪我已经遍历的项目及其位置。 当我找到一个项目时,我能够回顾这个集合,看看它是否已被看到,如果是,它的位置是什么(行号)。然后,我想查看当前项目之前找到的最后一项,并查看它的名称和位置。

我正在解析一些非结构化文本,有时我会在单词部分的非预期部分进行匹配。

采取以下措施:

'Item 1', 150
'Item 2', 340
'Item 3', 794
'Item 4', 1205
'Item 5', 1869
'Item 2', 3412  <-- I've seen 2, So I want to inspect the item before it (5, 1869)

我的想法是测试2到5之间的距离,并确定它是否是噪音。 在这种情况下,我想要放弃(第2,31212项),因为2应该在5之前,而第3412行与之前的2(第340行)相距很远,并且在最后一次看到之间也有顺序项“项目和这一项。

当然,如果有人有更好的想法,我也是为了这一切。

我不知道如何在python中运行集合。我甚至不确定我应该使用什么类型的收藏品。我此刻似乎赞成配对元组的列表,但这可能只是我的愚蠢。

感谢任何指导。

for line_num, line in enumerate(all_lines):
# matching requires back-tracking - we will always be at least 1 line behind loop  
    if line_num < 1: continue  
        blob = ''.join(all_lines[line_num : line_num + _blob_length_])  
        # evaluate text aginst match expressions
        matches = self.match_patterns_sb(blob) if is_sb_edition else self.match_patterns(blob)  
        #iterate each pattern and test if match was successful
        for pattern in matches.iterkeys():
            if matches[pattern] and line_num >= last_line_matched + 1: #Try not to rematch
                if pattern == last_matched_pattern and line_num < (last_line_matched + 2) :continue  
                #store match info in a local tuple nested within a higher level list
                if not '(continued)' in blob.lower() and not '( continued )' in blob.lower():  
                    print '{0}  -  {1}'.format(pattern, line_num)
                    '''
                    At this point I want to look into last_seen, and
                    1) Get the last seen item that matches this one ('Item 2')
                    2) Get the last item added into last_seen
                    3) do some calculations 
                     '''
                    last_seen[pattern] = line_num
                    if pattern in dict(section_items).keys():
                        test = dict(section_items)
                        existing_line = test[pattern]
                        print '{0} exists with LINE NUMBER {1}'.format(pattern, existing_line)  
                    section_items.append( (pattern, line_num) )
                    # track last match
                    last_line_matched = line_num
                    last_matched_pattern = pattern
                # order and normalize the item matches
                fixed_list = OrderedDict(self.sorted_nicely(section_items, itemgetter(0))).items() 

1 个答案:

答案 0 :(得分:3)

使用dict累计内容,检查您是否在每次之前都看过它。

sequence = [('item 1',150),('item 2',340),('item 3',794),('item 4',1205,),('item 5',1869),('item 2',3412)]
d = {}

for i,tup in enumerate(sequence):
    item,val = tup
    if d.get(item):
        print("I've seen {} before, it was {} at index {}".format(item,*d.get(item)))
    d[item] = (val, i)

#I've seen item 2 before, it was 340 at index 1

d将始终显示您最后一次看到itemNone

如果您需要跟踪所有过去看过item的次数,请向上移动defaultdict以累积(item, i)个元组到你的list

相关问题