2017-10-17 15 views
1

句読記号を無視して、単語の長さを数えるためにpythonで関数を作成しました。たとえば、私は文を持っている場合:[6, 5, 4, 1, 9, 5]垂直方向ではなく、Python 3のリスト結果を水平方向に表示

import string 
def word_length_list(given_string): 
     clean_string = given_string.translate(str.maketrans('','',string.punctuation)) 
     word_lenght_list = list(map(len, clean_string.split())) 
     return word_lenght_list 
    given_string = "Haven't there been 3 blackouts today?" 
    word_length_list(given_string) 

この機能は私にある期待される結果を与える:私は、私が作成した以下の機能が、これらの結果を得ることができる午前[6,5,4,1,9,5]"Haven't there been 3 blackouts today?は結果は以下でなければなりません

[7, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
15, 
3, 
6, 
2, 
3, 
8] 

given_string = "Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function." 

それは私に次のように結果を与える:私はそれが本当に長い文字列などとして与えるがあれば3210

誰かが私のリストを短い文字列と長い文字列の両方に対して結果を水平方向に返すようにする方法はありますか?どんな提案も大歓迎です。別々のセルに

+1

IDEを使用していますか? IPython? –

+1

再現できません:出力がうまく水平です。 – Prune

+0

ちょうどスペルの訂正:word_lenght_list - > word_length_list –

答えて

0

これはかなりの印刷をオフにコマンド%PPRINT

を実行します。リストが水平方向に表示されるようになりました。

関連する問題