0

I have a list with entries similar to the following:

[('sw1', 'sw2'), ('sw1', 'sw3'), ('sw1', 'sw4'), ('sw2', 'sw3'), ('sw2', 'sw4'), ('sw3', 'sw4')]

I would like to convert the tuples to strings and print them on individual lines like so in order to pass them as arguments to another program:

(sw1 , sw2)\n
(sw1 , sw3)\n
(sw1 , sw4)\n
(sw2 , sw3)\n
(sw2 , sw4)\n
(sw3 , sw4)\n

I tried strip() and split() but those don't appear to be supported for tuples. I'm assuming I would need some regex expression to accomplish what I'm trying to do, but even then I'm not sure hot to handle the fact that a potential field separator of , is both within and between the tuples. Any pointers are greatly appreciated. I'm old and just trying to learn to program.

Jerry
  • 70,495
  • 13
  • 100
  • 144

3 Answers3

2
>>>li = [('sw1', 'sw2'), ('sw1', 'sw3'), ('sw1', 'sw4'), ('sw2', 'sw3'), ('sw2', 'sw4'), ('sw3', 'sw4')]

>>> print "\n".join([str(item) for item in li])
('sw1', 'sw2')
('sw1', 'sw3')
('sw1', 'sw4')
('sw2', 'sw3')
('sw2', 'sw4')
('sw3', 'sw4')

Another way

>>> print "\n".join(["%s" %(item,) for item in li])
('sw1', 'sw2')
('sw1', 'sw3')
('sw1', 'sw4')
('sw2', 'sw3')
('sw2', 'sw4')
('sw3', 'sw4')
Amit
  • 19,780
  • 6
  • 46
  • 54
  • you don't need to first create a list comprehension to pass to join, just use a generator expression (i.e. get rid of the brackets) – acushner Mar 26 '14 at 20:48
  • 1
    Please see [here](http://stackoverflow.com/questions/9060653/list-comprehension-without-python/9061024#9061024) for a discussion on the string `join` method. It turns out that list comprehensions will generally be quicker than a generator expression. – Ffisegydd Mar 26 '14 at 21:14
1

You can use string formatting as follows:

a = [('sw1', 'sw2'), ('sw1', 'sw3'), ('sw1', 'sw4'), ('sw2', 'sw3'), ('sw2', 'sw4'), ('sw3', 'sw4')]

for i in a:
    print('({:}, {:})'.format(*i))

# (sw1, sw2)
# (sw1, sw3)
# (sw1, sw4)
# (sw2, sw3)
# (sw2, sw4)
# (sw3, sw4)

This doesn't change your tuples per se but does format the printing of them as you wished. The code simply iterates over your list and prints the tuples with a given format. The string format method is covered here.

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • Thanks for your help! The method you suggested does what I was hoping. I'm very new to Python and programming in general. I read the string format spec trying to understand how the print formatting example is actually working. From what I read the curly braces ({}) contain the replacement fields. The part I'm not understanding is how the colon (:) is resulting in removing the quotes from the other characters. Thanks for any clarification. – user3410622 Mar 26 '14 at 20:37
  • The quotes in your tuples simply state that they are strings, they're not actually there in the string itself. If you wanted to keep the quotes when you print then you could replace your format string with `"('{:}', '{:}')".format(*i)` which would print, for e.g. ('sw1', 'sw2'). – Ffisegydd Mar 26 '14 at 20:42
  • No, I didn't want the quotes. I played around with this a little. It seems like the following all produce the same results: print('({:}, {:})'.format(*i)) print('({}, {})'.format(*i)) print('({0}, {1})'.format(*i)) – user3410622 Mar 26 '14 at 20:45
0

You can just explicitly convert the tuples to strings (this works exactly as you hoped)- and finally join them together with the delimiter as what is between the tuples in your desired output:

>>> lst_tups = [('sw1', 'sw2'), ('sw1', 'sw3'), ('sw1', 'sw4'), ('sw2', 'sw3'), ('sw2', 'sw4'), ('sw3', 'sw4')]
>>> my_string = '\n '.join([str(t) for t in lst_tups])
>>> my_string
"('sw1', 'sw2')\n ('sw1', 'sw3')\n ('sw1', 'sw4')\n ('sw2', 'sw3')\n ('sw2', 'sw4')\n ('sw3', 'sw4')"

You can then easily output it as follows; just by printing it out:

>>> print(my_string)
('sw1', 'sw2')
 ('sw1', 'sw3')
 ('sw1', 'sw4')
 ('sw2', 'sw3')
 ('sw2', 'sw4')
 ('sw3', 'sw4')
anon582847382
  • 19,907
  • 5
  • 54
  • 57