Python 用于循环输出重复项
我试图用这个replacer函数实现的是用一个唯一的代码替换com(dict)中字符串中的人名,该代码通过正则表达式在(dict)中找到。用代码替换名称是可行的,但用代码而不是名称添加新字符串是我出错的地方Python 用于循环输出重复项,python,list,loops,if-statement,iterator,Python,List,Loops,If Statement,Iterator,我试图用这个replacer函数实现的是用一个唯一的代码替换com(dict)中字符串中的人名,该代码通过正则表达式在(dict)中找到。用代码替换名称是可行的,但用代码而不是名称添加新字符串是我出错的地方 a = {'1330': ('John', 'Gold', '1330'), "0001":('Matt', 'Wade', '0001'), '2112': ('Bob', 'Smith', '2112')} com = {'6':['John Gold, getting no point
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 = []
问题在于输出如下所示:
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
我不希望那些已经用数字代替名字的评论进入最终列表。因此,我希望我的预期输出如下所示:
[{'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'}]
我曾研究过使用迭代器,将y作为iter_列表,但我没有取得任何进展。任何帮助都将不胜感激。谢谢 不确定为什么要进行regexp替换,因为您正在检查
中的中是否存在名字/全名。也不确定案例3中的fuzz.ratio(item,item)
应该做什么,但下面是如何进行简单/简单的替换:
[{'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'}]
这将产生以下输出:
#!/usr/bin/python
import re
def replace_names(authors, com):
res = []
for post_id, comments in com.items():
for comment in comments:
for author_id, author in authors.items():
first_name, last_name = author[0], author[1]
full_name = first_name + ' ' + last_name
if full_name in comment:
comment = comment.replace(full_name, author_id)
break
elif first_name in comment:
comment = comment.replace(first_name, author_id)
break
res.append({'post_id': post_id, 'comment': comment})
return res
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!']}
for comment in replace_names(a, com):
print comment
要理解您对原始代码的意图有点棘手,但是(其中一个)您得到重复代码的原因是您在outher循环中处理作者,这意味着您将为每个作者处理一次每个注释。通过交换循环,可以确保每个注释只处理一次
您可能还打算在有continue
的地方进行中断
,但我不能完全确定我是否理解您的原始代码应该如何工作
全局变量的使用也有点混乱。不确定为什么要执行regexp replace,因为您正在检查中的是否有名字/全名。也不确定案例3中的fuzz.ratio(item,item)
应该做什么,但下面是如何进行简单/简单的替换:
[{'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'}]
这将产生以下输出:
#!/usr/bin/python
import re
def replace_names(authors, com):
res = []
for post_id, comments in com.items():
for comment in comments:
for author_id, author in authors.items():
first_name, last_name = author[0], author[1]
full_name = first_name + ' ' + last_name
if full_name in comment:
comment = comment.replace(full_name, author_id)
break
elif first_name in comment:
comment = comment.replace(first_name, author_id)
break
res.append({'post_id': post_id, 'comment': comment})
return res
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!']}
for comment in replace_names(a, com):
print comment
要理解您对原始代码的意图有点棘手,但是(其中一个)您得到重复代码的原因是您在outher循环中处理作者,这意味着您将为每个作者处理一次每个注释。通过交换循环,可以确保每个注释只处理一次
您可能还打算在有continue
的地方进行中断
,但我不能完全确定我是否理解您的原始代码应该如何工作
全局变量的使用也有点混乱