2

I am a noob, How do I remove quotations and commas from my list? Or how do I "unstring"?

Without showing the dictionary (dic), the code I am using looks like this:

>>>import itertools
>>>list(itertools.product(dic[2], dic[3])

my results looks like this:

[('A', 'D'), ('A', 'E'), ('A', 'F'), ('B', 'D'), ('B', 'E'), ('B', 'F'), ('C', 'D'), ('C', 'E'), ('C', 'F')]

I would like them to look like this:

AD, AE, AF, BD, BE, BF,

5 Answers 5

12

You want to produce a string, so you can use string manipulation (in particular, the join method):

>>> import itertools
>>> a = ['A', 'B']
>>> b = ['D', 'E', 'F']
>>> print ', '.join(''.join(x) for x in itertools.product(a, b))
AD, AE, AF, BD, BE, BF

Actually you don't even need itertools, you could just use a nested comprehension:

>>> print ', '.join(x + y for x in a for y in b)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use join string method:

>>> import itertools
>>> p = list( itertools.product( ['a','b','c' ],['e','f','g'] ) )
>>> p
[('a', 'e'), ('a', 'f'), ('a', 'g'), ('b', 'e'), ('b', 'f'), ('b', 'g'), ('c', 'e'), ('c', 'f'), ('c', 'g')]
>>> ', '.join( a+b for (a,b) in p )
'ae, af, ag, be, bf, bg, ce, cf, cg'

Comments

0
import itertools
import sys
yourlist = list(itertools.product(dic[2], dic[3])
sys.stdout.write (yourlist[0][0] + yourlist[0][1])
for i in xrange (1, len(yourlist)):
    sys.stdout.write (", " + yourlist[i][0] + yourlist[i][1])

Although other answers use the join() method (the correct and 'python' way to do it) I have given you a simple rough and tough solution so I don't kill your current lesson with you trying to figure out how the join() method works.

7 Comments

That doesn't produce the desired output, though. Maybe you wanted to add a comma at the end of the print statements?
Why write directly to stdout rather than using print()? Also, I don't feel join() is such a hard concept to get, so while I get what you were trying to do, I don't think it benefits anyone.
@Lattyware: It's actually not very intuitive to make print not print trailing whitespace after what you gave it as arguments.
@NiklasB. In 2.x, this is true. Although a better solution might be to do from __future__ import print_function and then use print(..., end="") if you are stuck with 2.6/2.7.
@Lattyware: Sure, I just said it's not very intuitive (and it isn't a lot more readable than stdout.write)
|
0

What about this using list comprehension?

p = [('A', 'D'), ('A', 'E'), ('A', 'F'), ('B', 'D'), ('B', 'E'), ('B', 'F'), ('C', 'D'), ('C', 'E'), ('C', 'F')]

', '.join([''.join(i) for i in p])

and join() to concatenate strings gives:

AD, AE, AF, BD, BE, BF, CD, CE, CF

Alternatively, a generator expression would also work (thanks to @Nicklas B reminding me, for some reason list comprehension comes to my mind first):

', '.join(''.join(i) for i in p)

Comments

0

An alternate solution:

In [61]: l=[('A', 'D'), ('A', 'E'), ('A', 'F'), ('B', 'D'), ('B', 'E'), ('B', 'F'), 
            ('C', 'D'), ('C', 'E'), ('C', 'F')]

In [62]: ', '.join(t[0]+t[1] for t in l)
Out[62]: 'AD, AE, AF, BD, BE, BF, CD, CE, CF'

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.