mercredi 24 mai 2017

For loop outputting duplicates

a = {'1330': ('John', 'Gold', '1330'), "0001":('Matt', 'Wade', '0001'), '2112': ('Bob', 'Smith', '2112')}
com = {'6':['John Gold, getting no points', 'Matt played in this game? Didn\'t notice him','Love this shot!']}
comments_table = []

What I am trying to achieve with this replacer function is replace people's names in the strings found in com(dict) with the a code unique to them which is found in a(dict) via regex. Replacing the name with the code works, but adding that new string with the code instead of the name is where I am going wrong.

def replace_first_name():
for k,v in a.items():
    for z, y in com.items():
        for item in y:
            firstname = a[k][0]
            lastname = a[k][1]
            full_name = firstname + ' ' + lastname
            if firstname in item:
                if full_name in item:
                    t = re.compile(re.escape(full_name), re.IGNORECASE)
                    comment = t.sub(a[k][2], item)
                    print ('1')
                    comments_table.append({
                        'post_id': z, 'comment': comment
                    })
                    continue

                else:

                    t = re.compile(re.escape(firstname), re.IGNORECASE)
                    comment = t.sub(a[k][2], item)
                    print ('2')
                    comments_table.append({
                        'post_id':z, 'comment':comment
                    })
            else:
                print ('3')
                if fuzz.ratio(item,item) > 90:
                    comments_table.append({
                        'post_id': z, 'comment': item
                    })
                else:
                    pass

The problem is with the output as seen below:

[{'comment': '1330, getting no points', 'post_id': '6'}, {'comment': "Matt played in this game? Didn't notice him", 'post_id': '6'}, {'comment': 'Love this shot!', 'post_id': '6'}, {'comment': 'John Gold, getting no points', 'post_id': '6'}, {'comment': "Matt played in this game? Didn't notice him", 'post_id': '6'}, {'comment': 'Love this shot!', 'post_id': '6'}, {'comment': 'John Gold, getting no points', 'post_id': '6'}, {'comment': "0001 played in this game? Didn't notice him", 'post_id': '6'}, {'comment': 'Love this shot!', 'post_id': '6'}]

I don't want comments that already have their name replaced with the number to make their way into the final list. Therefore, I want my expected output to look like this:

[{'comment': '1330, getting no points', 'post_id': '6'},{'comment': '0001,played in this game? Didn\'t notice him', 'post_id': '6', {'comment':'Love this shot', 'post_id':'6'}]

I have looked into using an iterator by making y an iter_list, but I didn't get anywhere. Any help would be appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire