0

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

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

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

采取以下措施:

'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 项,第 3412 项),因为 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() 
4

1 回答 1

3

使用 a 来积累东西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总会有你最后一次见到item,或None

如果您需要跟踪过去看到的所有item时间,请向上移动到 adefaultdict以将元组累积(item, i)到 alist中。

于 2013-11-02T05:42:16.090 回答