-1

I needed to make permutations of the input asked so I have this code

#!/usr/bin/python
import itertools

variable1 = raw_input("input 4-6 carac")
var1list = list(variable1)
lenlist = len(var1list)
itertools.permutations(variable1)
iterlist = list(itertools.permutations(variable1, lenlist))
print iterlist

So I end up with a list of tuplets So for 123 I get (1,2,3),(1,3,2)... The problem is then I need to append each of the tuplets a string, so I can't append a tupplet to a string. I need to convert each tuplet of the list to a string but without () or the commas.

E.g., from the list that contains the permutations of 123:

(1,2,3),(1,3,2)...

I need to get a list that contains each of the members of each tuplet together and separated from the mebers of the other tuplets of the list: I'm clarifying i only want THIS one:

[123, 132...] or ['123', '132'...]

These two ones are similar examples I found on already answered posts, just to clarify that I wanted something different to them.

A literal string of tuplets

"(1,2,3), (1,3,2)..."

Like in this post

or a list with all the tuplets formatted into one single list together

[1,2,3,1,3,2...]

Like in this other post

Any tip for this? I kinda control Python but I've got about none knowledge of how tuplets work.

EDIT: I think the list should be of strings (instead of integers)

James
  • 9
  • 4

3 Answers3

0

You can use string join to build a string with an appropriate delimiter from your tuples:

perms = [(1,2,3), (1,3,2)]
string_perms = [''.join([str(i) for i in t]) for t in perms]
print(string_perms)
# ['123', '132']
slider
  • 12,810
  • 1
  • 26
  • 42
0

For the case where you want this style of output:

[123, 132...]

Try the following:

b = [''.join(map(str,i)) for i in a]
# ['123', '132']

Where a is your list of permutations. It joins the tuples as strings and creates a list of the results.

For the case where you want your output in this style:

[1,2,3,1,3,2...]

import numpy as np
b = np.array(a)
b = map(str, b.reshape(-1).tolist())
# ['1', '2', '3', '1', '3', '2']

This uses numpy array functions to flatten your entries nicely.

killian95
  • 803
  • 6
  • 11
  • The output of the first command was exactly what I wanted. Sorry if I confused any of you. – James Nov 01 '18 at 23:54
-1
a = [(1,2,3),(1,3,2)]
b = [''.join(map(str, s)) for s in a]

print (b)
# ['123', '132']
Ghilas BELHADJ
  • 13,412
  • 10
  • 59
  • 99