问题:
我有一个案例,每当句子中的单词发生变化时,我需要用dc
替换句子中的单词。这句话是:
Sweet Bad No No Long Yes Bike No Yes
在接下来的几次中,这句话将继续改为:
Sweet Bad No No Long Yes Bike Yes No
Sweet Bad No No Short Yes Car Yes No
因此,输出应如下所示:
Sweet Bad No No dc Yes dc dc dc
我想用dc
替换第一个更改的实例,从第一个句子到下一个句子。我写了一段代码,但似乎不对。我哪里错了?
dont_care = "dc"
hyp_curr = []
hyp_next = []
def create_hypothesis(stVal):
splitVal = stVal.split()
global hyp_curr
global hyp_next
if not hyp_curr:
hyp_curr = (' '.join(w for w in splitVal if w not in hyp_curr))
return hyp_curr
else:
hyp_next = splitVal
print hyp_curr
print hyp_next
for wordc in hyp_curr.split():
for wordn in hyp_next:
if hash(wordc) != hash(wordn):
hyp_curr = hyp_curr.replace(wordn,dont_care)
return hyp_curr
答案 0 :(得分:3)
>>> s = "Sweet Bad No No Long Yes Bike No Yes"
>>> s1 = "Sweet Bad No No Long Yes Bike Yes No"
>>> map(lambda x: x[0]==x[1] and x[0] or 'dc', zip(s.split(), s1.split()))
['Sweet', 'Bad', 'No', 'No', 'Long', 'Yes', 'Bike', 'dc', 'dc']
>>> ' '.join(_)
'Sweet Bad No No Long Yes Bike dc dc'
或:
>>> [x[0]==x[1] and x[0] or 'dc' for x in zip(s.split(), s1.split())]
['Sweet', 'Bad', 'No', 'No', 'Long', 'Yes', 'Bike', 'dc', 'dc']
答案 1 :(得分:0)
也许是这样的?
#!/usr/local/cpython-3.3/bin/python
import pprint
def rep_dc(list_):
result = []
for sublist1, sublist2 in zip(list_[:-1], list_[1:]):
assert len(sublist1) == len(sublist2)
for tuple_ in zip(*list_):
set_ = set(tuple_)
if len(set_) > 1:
result.append('dc')
else:
result.append(tuple_[0])
return result
def main():
list1 = 'Sweet Bad No No Long Yes Bike No Yes'.split()
list2 = 'Sweet Bad No No Long Yes Bike Yes No'.split()
list3 = 'Sweet Bad No No Short Yes Car Yes No'.split()
master_list = [ list1, list2, list3 ]
pprint.pprint(master_list)
result = rep_dc(master_list)
print(result)
result_string = ' '.join(result)
print(result_string)
main()