2017-10-24 5 views
0

私はpython出力を持っています。これは、tKinter GUIに入力された単語のすべての順列です。出力から取り出されたコメントが必要です。私は置換を使用していますが、これはすべての置換後に空白で終わります。置換を使わないでカンマを削除するPython

def exactMatch(entries): 
    words = [entry[1].get() for entry in entries] 
    perms = [p for p in permutations((words))] 
    x1 = str(perms) 

    perms2 = x1.replace("," , '') 
    perms3 = perms2.replace("'" , '') #Takes out the Quotations 
    perms4 = perms3.replace("(" , '[') 
    perms5 = perms4.replace(')' , ']\n') 
    perms6 = perms5.replace (" ", "") 
    print(perms6) 


"a b c hello" 
"a b hello c" 
"a c b hello" 
"a c hello b" 
"a hello b c" 
"a hello c b" 
"b a c hello" 
"b a hello c" 
"b c a hello" 
"b c hello a" 
"b hello a c" 

上記のように、すべての置換出力には、空白で置き換えられたカンマがあります。私の目標は、空白を使用せず、すべての出力が出力の最初の行と並ぶようにカンマを取り出します。これをどうやってやりますか?どんな助けもありがとうございます。

+0

あなたの機能をpermutations' 'ていますか?定義を教えてください。なぜ処理する前に、順列を文字列に変換していますか?あなたはリストの要素を一緒に結合する 'join'コマンドを知っていますか? –

答えて

2

結果を反復処理します。

for perm in perms: 
    print(perm) 
+0

私はこれを完全に理解していません。私のコードでは、空白を取り除くためにこれを入れていますか?私が理解していることから、パーマネントリストで順列を取って印刷しているだけです。これはすでに行っています。 – Warthog1

1

もう一つの方法:permsは型である

for perm in perms: 
    print(*perm) 

<class 'itertools.permutations'>

関連する問題