14

I've written a function in python that returns a list, for example

[(1,1),(2,2),(3,3)] 

But i want the output as a string so i can replace the comma with another char so the output would be

'1@1' '2@2' '3@3' 

Any easy way around this?:) Thanks for any tips in advance

0

8 Answers 8

18

This looks like a list of tuples, where each tuple has two elements.

' '.join(['%d@%d' % (t[0],t[1]) for t in l])

Which can of course be simplified to:

' '.join(['%d@%d' % t for t in l])

Or even:

' '.join(map(lambda t: '%d@%d' % t, l))

Where l is your original list. This generates 'number@number' pairs for each tuple in the list. These pairs are then joined with spaces (' ').

The join syntax looked a little weird to me when I first started woking with Python, but the documentation was a huge help.

Sign up to request clarification or add additional context in comments.

2 Comments

instead of (' ') could i add \n somewhere so the output returns as a column with one string on each new line?
Sure you can! Why not try print '\n'.join('%d@%d' % t for t in l) in the Python interpreter?
4

You could convert the tuples to strings by using the % operator with a list comprehension or generator expression, e.g.

ll = [(1,1), (2,2), (3,3)]
['%d@%d' % aa for aa in ll]

This would return a list of strings like:

['1@1', '2@2', '3@3']

You can concatenate the resulting list of strings together for output. This article describes half a dozen different approaches with benchmarks and analysis of their relative merits.

Comments

3

' '.join([str(a)+"@"+str(b) for (a,b) in [(1,1),(2,2),(3,3)]])

or for arbitrary tuples in the list,

' '.join(['@'.join([str(v) for v in k]) for k in [(1,1),(2,2),(3,3)]])

Comments

2
In [1]: ' '.join('%d@%d' % (el[0], el[1]) for el in [(1,1),(2,2),(3,3)])
Out[1]: '1@1 2@2 3@3'

Comments

1
[ str(e[0]) + ',' + str(e[1]) for e in [(1,1), (2,2), (3,3)] ]

This is if you want them in a collection of string, I didn't understand it if you want a single output string or a collection.

Comments

0
[str(item).replace(',','@') for item in [(1,1),(2,2),(3,3)]]

Comments

0

You only need join and str in a generator comprehension.

>>> ['@'.join(str(i) for i in t) for t in l]
['1@1', '2@2', '3@3']

>>> ' '.join('@'.join(str(i) for i in t) for t in l)
'1@1 2@2 3@3'

Comments

0

you could use the repr function and then just replace bits of the string:

>>> original = [(1,1),(2,2),(3,3)]
>>> intermediate = repr(original)
>>> print intermediate
[(1, 1), (2, 2), (3, 3)]
>>> final = intermediate.replace('), (', ' ').replace('[(','').replace(')]','').replace(', ','@')
>>> print final
1@1 2@2 3@3

but this will only work if you know for certain that none of tuples have the following character sequences which need to be preserved in the final result: ), (, [(, )], ,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.