2011-09-12 15 views
-3

可能性の重複:
Overriding the newline generation behaviour of Python's print statement
PPM image to ASCII art in PythonPythonのリストの質問

これは私のコードでは、私は印刷文字を持っていますが、私は彼らが同じ行にあることとに分割する必要がありますラインの終わり。

import sys 

def main(filename): 
    image = open(filename) 
    #reads through the first three lines 
    color = image.readline().splitlines() 
    size_width, size_height = image.readline().split() 
    max_color = image.readline().splitlines() 

    #reads the body of the file 
    pixels = image.read().split() 
    red = 0 
    green = 0 
    blue = 0 
    r_g_b_value = [] 
    #pulls out the values of each tuple and coverts it to its grayscale value 
    for i in pixels: 
     if i != "\n" or " ": 
     if len(i) == 3: 
      red = int(i[0]) * .3 
      green = int(i[1]) * .59 
      blue = int(i[2]) * .11 
     elif len(i) == 2: 
      red == int(i[0]) 
      green == int(i[1]) 
      blue == 0 
     elif len(i) == 1: 
      red == int(i[0]) 
      green == 0 
      blue == 0 

     r_g_b_value = [red + green + blue] 
     grayscale = [] 
     character = [] 

     for j in r_g_b_value: 
      if int(j) <= .2: 
       character = "M" 
      elif int(j) > .2 and int(j) <= .4: 
       character = "#" 
      elif int(j) > .4 and int(j) <= .6: 
       character = "A" 
      elif int(j) > .6 and int(j) <= .8: 
       character = "@" 
      elif int(j) > .8 and int(j) <= 1: 
       character = "$" 
      elif int(j) > 1 and int(j) <= 1.2: 
       character = "0" 
      elif int(j) > 1.2 and int(j) <= 1.4: 
       character = "e" 
      elif int(j) > 1.4 and int(j) <= 1.6: 
       character = "a" 
      elif int(j) > 1.8 and int(j) <= 2: 
       character = "o" 
      elif int(j) > 2 and int(j) <= 2.2: 
       character = "=" 
      elif int(j) > 2.25 and int(j) <= 2.5: 
       character = "+" 
      elif int(j) > 2.5 and int(j) <= 2.75: 
       character = ";" 
      elif int(j) > 2.75 and int(j) <= 3: 
       character = ":" 
      elif int(j) > 3 and int(j) <= 3.4: 
       character = "," 
      elif int(j) > 3.4 and int(j) <= 3.9: 
       character = "." 
      else: 
       character = " " 
      character += character 
      grayscale = [character] 
      print(grayscale) 

助けてください。

+12

このコードは私を怖がらせる! –

+0

特定の前の質問が実際には重複した、phoojiだとは思わないでください。 – Amber

+0

@asmith:あなたの質問は古いStackoverflow質問の重複としてマークしました。さらに、本質的に非常に似ている多くの質問をあなたが求めている。これは落胆しています(http://blog.stackoverflow.com/2009/04/a-day-in-the-penalty-box/)。 – phooji

答えて

3

end parameter for print()は空の文字列を指定し、それが自動的に改行を追加しません。endため

>>> print('foo', end=''); print('bar'); print('baz') 
foobar 
baz 

デフォルト値は'\n'です。 print()に渡されたすべての正規引数が出力された後に、endが追加されます。たとえば、print('foo', 'bar'); print('baz')は上記と同じ結果を出力します。

印刷される各オブジェクトの間に追加されるsepパラメータもあります。a la join()です。デフォルトは何もありません。ところで


、あなたは以下のブロック全体書き換えることができます。このはるかに簡単なコードで

for j in r_g_b_value: 
     if int(j) <= .2: 
      character = "M" 
     elif int(j) > .2 and int(j) <= .4: 
      character = "#" 
     elif int(j) > .4 and int(j) <= .6: 
      character = "A" 
     elif int(j) > .6 and int(j) <= .8: 
      character = "@" 
     elif int(j) > .8 and int(j) <= 1: 
      character = "$" 
     elif int(j) > 1 and int(j) <= 1.2: 
      character = "0" 
     elif int(j) > 1.2 and int(j) <= 1.4: 
      character = "e" 
     elif int(j) > 1.4 and int(j) <= 1.6: 
      character = "a" 
     elif int(j) > 1.8 and int(j) <= 2: 
      character = "o" 
     elif int(j) > 2 and int(j) <= 2.2: 
      character = "=" 
     elif int(j) > 2.25 and int(j) <= 2.5: 
      character = "+" 
     elif int(j) > 2.5 and int(j) <= 2.75: 
      character = ";" 
     elif int(j) > 2.75 and int(j) <= 3: 
      character = ":" 
     elif int(j) > 3 and int(j) <= 3.4: 
      character = "," 
     elif int(j) > 3.4 and int(j) <= 3.9: 
      character = "." 
     else: 
      character = " " 

を:

# Mapping of values to symbol tuples, ordered from least to greatest upper bound. 
# Format is (symbol, upperbound) - lower bounds are implied by 
# the previous symbol's upper bound, non-inclusive. 
symbol_set = [('M', 0.2), ('#', 0.4), ('A', 0.6), ('@', 0.8), ('$', 1.0), 
    ('0', 1.2), ('e', 1.4), ('a', 1.6), ('o', 2.0), ('=', 2.2), ('+', 2.5), 
    (';', 2.75), (':', 3.0), (',', 3.4), ('.', 3.9)] 

for j in r_g_b_value: 
    for symbol, cutoff in symbol_set: 
     if j <= cutoff: 
      character = symbol 
      break 
    else: 
     character = ' ' 

for: else:建設がちょうどあった場合」という意味ループ内でbreakがトリガーされていない場合は、else:セクションにあるものを実行してください。古いコードの「else」ケースを処理します)

あなたは常にコンピュータがあなたのために仕事をするように努力すべきです - ほとんど同一のelif句を10-15と書くのではなく、ちょっとした巧みさを使ってループで動作させるようにしてください。

+0

おそらく 'bisect'(http://docs.python.org/library/bisect.html#other-examples)を使用する方がより良いオプションです –

+0

はい、「bisect」はループを書くより簡潔な方法です - 欠点は、シンボルセットのために異なるフォーマットが必要であることです(インデックスとは別のカットオフ配列とインデックスにマップされるべきもの)。上記の2つのリストは、上記で使用したリスト形式( 'symbol = [x symbol_set]のxはx [0]')、 'cutoffs = [symbol_setのxはx [1] ')' 'から生成できます。いずれにしても多くの仕事について。あなたのデータをどのように指定するかが本当に分かります。 – Amber

+0

'symbols、cutoffs = zip(* symbol_set)' –