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
解决方案
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.