Monday, 15 September 2014

how to print the list in one line in python? -



how to print the list in one line in python? -

if have code , need print output in 1 line how can that?

l = [('the', 'the'),('cat', 'cow'),('sat', 'sat'),('on', 'on'),('mat', 'mat'),('and', 'a'),('sleep', 'sleep')] def getparaphrases(l): pre_match = 0 mis_match = 0 after_match = 0 paraphrase = [] newpar = [] x in l: if x[0] == x[1]: if not paraphrase == []: print '\n paraphrase:', paraphrase paraphrase = [] pre_match += 1 mis_match = 0 else: if pre_match >= 1: if mis_match == 0: paraphrase = [] paraphrase.append(x) mis_match += 1 if after_match >= 1: paraphrase.append(x) after_match += 1

the output is:

paraphrase: [('cat', 'cow')] paraphrase: [('and', 'a')]

but, how output in 1 line such as,

paraphrase [('cat', 'cow'), ('and', 'a') ]

you can replace whole function list comprehension

l = [('the', 'the'),('cat', 'cow'),('sat', 'sat'),('on', 'on'),('mat', 'mat'),('and', 'a'),('sleep', 'sleep')] [(i,j) i, j in l if != j]

output

[('cat', 'cow'), ('and', 'a')]

python

No comments:

Post a Comment