2011-09-12 165 views
-3

可能重复:
Overriding the newline generation behaviour of Python's print statement
PPM image to ASCII art in Python蟒蛇名单问题

这是我的代码,我有个字符的打印,但我需要他们在同一行,并在打破行结束。

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()是一个空字符串,并且它不会自动添加一个新行:

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

end的默认值是'\n';在输出所有传递给print()的常规参数后添加end。例如,print('foo', 'bar'); print('baz')将输出与上面相同的内容。

还有一个sep参数被添加到每个打印的对象之间,一个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:部分中的操作,它会处理来自旧代码的'其他'情况。)

你应该总是尽力让计算机为你完成工作 - 而不是写出10-15个几乎相同的elif子句,而是使用一点聪明才能使它成为一个循环。

+0

也许使用'bisect'(http://docs.python.org/library/bisect.html#other-examples)在这里是更好的选择 –

+0

是的,'bisect'会是写循环的更简洁的方式 - 缺点是它需要符号集的不同格式(索引应该映射到的截止值和事物的单独数组)。你可以从上面使用的列表格式生成这两个列表('symbol = [x [0] for x in symbol_set]','cutoffs = [x [1] for x in symbol_set]'),但它会是无论哪种方式,工作量都很大。真的只是归结为你更喜欢指定你的数据。 – Amber

+0

'符号,cutoffs = zip(* symbol_set)' –