【问题标题】:python list question [duplicate]python列表问题[重复]
【发布时间】:2011-09-12 04:41:22
【问题描述】:

可能重复:
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)

任何帮助将不胜感激。

【问题讨论】:

  • 这段代码吓到我了!!
  • 不要认为上一个问题实际上是一个很好的重复,phooji。
  • @asmith:我已将您的问题标记为与旧的 stackoverflow 问题重复。此外,您提出了许多性质非常相似的问题;不鼓励这样做 (blog.stackoverflow.com/2009/04/a-day-in-the-penalty-box)。
  • @Amber:你是对的stackoverflow.com/questions/2623470/… 可能更接近。请注意,OP 的其他问题非常相似。
  • 别再问同一个问题了。

标签: python list python-3.x


【解决方案1】:

end parameter for print()指定为空字符串,不会自动添加换行符:

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

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

还有一个sep 参数添加在每个正在打印的对象之间,例如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'情况。)

您应该始终努力让计算机为您完成工作 - 不要写出 10 到 15 个几乎相同的 elif 子句,而是使用一点点聪明来让它与循环一起工作。

【讨论】:

  • 也许在这里使用bisect (docs.python.org/library/bisect.html#other-examples) 是一个更好的选择
  • 是的,bisect 将是一种更简洁的循环编写方式 - 缺点是它需要不同的符号集格式(单独的截止数组和索引应该映射到的东西)。您可以从我上面使用的列表格式(symbols = [x[0] for x in symbol_set]cutoffs = [x[1] for x in symbol_set])生成这两个列表,但无论哪种方式都差不多。真的只是归结为你喜欢如何指定你的数据。
  • symbols, cutoffs = zip(*symbol_set)
  • 好电话。 ;) 为什么我似乎总是忘记zip() 的用处,即使我在过去 24 小时内使用过它?但是,我将保留答案,因为它没有平分,因为我认为将其写出来更好地了解事情是如何自动化的(相对于elif 堆栈)。