将程序转换为列表理解

时间:2013-06-11 03:53:50

标签: python list-comprehension

我对如何将此程序转换为列表理解感到困惑。有什么提示吗?

这是我写的原始代码(另外,我是python的新手,所以你可以告诉我,如果这很糟糕,我不介意)代码应该使用内置的chr()函数返回一个打印时的字符串,是一个ascii图表,四个条目。(仅使用32到126之间的ascii值)

def GiveAsciiChart5():
    count = 4

    for value in range(32, 127):
        count += 1
        print " |ASCII Value|: ", "|",(value),"|", " |Character|: ", "|",chr(value),"|",
        if count % 4 == 0:
            print "\n",

        #print " |ASCII Value|: ", "|",(value),"|", " |Character|: ", "|",chr(value),"|",
def Main():
    GiveAsciiChart5()

if __name__ == '__main__':
    Main()

这是我尝试制作列表理解......这显然不起作用。我对把count变量放在哪里感到很困惑?我正在尝试使用列表推导和str.join()来减少它。

def GiveAsciiChart6():
    count = 3
    a = [ (i, chr(i))  for i in range(32,127)]
    for count in a:
        if count % 4 == 0:
            print "\n"
            print " |ASCII Value|: ", "|",(i),"|", " |Character|: ", "|",chr(i),"|",



def Main():
    GiveAsciiChart6()

if __name__ == '__main__':
    Main()

我知道它有很长的路要走,目前非常糟糕。我只是很困惑并且对如何以这种方式格式化它感到困惑...任何提示都很受欢迎,并不期待完整的解决方案,因为我想学习并了解正在发生的事情。 谢谢!

4 个答案:

答案 0 :(得分:0)

为了不完全给出答案,我注释了代码。

for count in a: #the type of count would be a tuple containing (i, chr(i))
    if count % 4 == 0:  #so then what should this statement be?
       print "\n"
       print " |ASCII Value|: ", "|",(i),"|", " |Character|: ", "|",chr(i),"|", 
       # ^ remember, we are iterating through a list of tuples, the element
       # our iteration is on is called count (what's 'i' doing here?)

答案 1 :(得分:0)

如果您在终端中执行以下代码段:

>>> count = 3
>>> a = [ (i, chr(i))  for i in range(32,127)]
>>> for count in a:
...     print count

你得到了

(32, ' ')
(33, '!')
(34, '"')
...

现在,如果您尝试使用(33, '!') % 4之类的行,则会获得TypeError: unsupported operand type(s) for %: 'tuple' and 'int'

在我看来,你的列表理解似乎没问题。你遇到了一个问题,因为你正试图找到一个元组的模数。

答案 2 :(得分:0)

这是使用列表推导和enumerate()内置函数的等效解决方案:

def GiveAsciiChart5():
    vals = [(value, chr(value)) for value in range(32, 127)]
    for i, (v, c) in enumerate(vals):
        print " |ASCII Value|: ", "|", v, "|", " |Character|: ", "|", c, "|",
        if not i % 4:
            print "\n",

请注意,理解仅对生成值有用,之后会完成打印部分。通过使用enumerate()并充分利用元组解包,我正在跳过对计数器的需求。

答案 3 :(得分:0)

a = [(i, chr(i)) for i in range(32, 127)]

for v, c in a:
    print ' |ASCII Value|: | {v} | |Character|: | {c} |'.format(v=str(v).rjust(3), c=c),
    if v % 4 == 0:
        print

使用if v % 4 == 3:

输出
|ASCII Value|: |  32 | |Character|: |   | |ASCII Value|: |  33 | |Character|: | ! | |ASCII Value|: |  34 | |Character|: | " | |ASCII Value|: |  35 | |Character|: | # |
|ASCII Value|: |  36 | |Character|: | $ | |ASCII Value|: |  37 | |Character|: | % | |ASCII Value|: |  38 | |Character|: | & | |ASCII Value|: |  39 | |Character|: | ' |
|ASCII Value|: |  40 | |Character|: | ( | |ASCII Value|: |  41 | |Character|: | ) | |ASCII Value|: |  42 | |Character|: | * | |ASCII Value|: |  43 | |Character|: | + |
|ASCII Value|: |  44 | |Character|: | , | |ASCII Value|: |  45 | |Character|: | - | |ASCII Value|: |  46 | |Character|: | . | |ASCII Value|: |  47 | |Character|: | / |
|ASCII Value|: |  48 | |Character|: | 0 | |ASCII Value|: |  49 | |Character|: | 1 | |ASCII Value|: |  50 | |Character|: | 2 | |ASCII Value|: |  51 | |Character|: | 3 |
|ASCII Value|: |  52 | |Character|: | 4 | |ASCII Value|: |  53 | |Character|: | 5 | |ASCII Value|: |  54 | |Character|: | 6 | |ASCII Value|: |  55 | |Character|: | 7 |
|ASCII Value|: |  56 | |Character|: | 8 | |ASCII Value|: |  57 | |Character|: | 9 | |ASCII Value|: |  58 | |Character|: | : | |ASCII Value|: |  59 | |Character|: | ; |
|ASCII Value|: |  60 | |Character|: | < | |ASCII Value|: |  61 | |Character|: | = | |ASCII Value|: |  62 | |Character|: | > | |ASCII Value|: |  63 | |Character|: | ? |
|ASCII Value|: |  64 | |Character|: | @ | |ASCII Value|: |  65 | |Character|: | A | |ASCII Value|: |  66 | |Character|: | B | |ASCII Value|: |  67 | |Character|: | C |
|ASCII Value|: |  68 | |Character|: | D | |ASCII Value|: |  69 | |Character|: | E | |ASCII Value|: |  70 | |Character|: | F | |ASCII Value|: |  71 | |Character|: | G |
|ASCII Value|: |  72 | |Character|: | H | |ASCII Value|: |  73 | |Character|: | I | |ASCII Value|: |  74 | |Character|: | J | |ASCII Value|: |  75 | |Character|: | K |
|ASCII Value|: |  76 | |Character|: | L | |ASCII Value|: |  77 | |Character|: | M | |ASCII Value|: |  78 | |Character|: | N | |ASCII Value|: |  79 | |Character|: | O |
|ASCII Value|: |  80 | |Character|: | P | |ASCII Value|: |  81 | |Character|: | Q | |ASCII Value|: |  82 | |Character|: | R | |ASCII Value|: |  83 | |Character|: | S |
|ASCII Value|: |  84 | |Character|: | T | |ASCII Value|: |  85 | |Character|: | U | |ASCII Value|: |  86 | |Character|: | V | |ASCII Value|: |  87 | |Character|: | W |
|ASCII Value|: |  88 | |Character|: | X | |ASCII Value|: |  89 | |Character|: | Y | |ASCII Value|: |  90 | |Character|: | Z | |ASCII Value|: |  91 | |Character|: | [ |
|ASCII Value|: |  92 | |Character|: | \ | |ASCII Value|: |  93 | |Character|: | ] | |ASCII Value|: |  94 | |Character|: | ^ | |ASCII Value|: |  95 | |Character|: | _ |
|ASCII Value|: |  96 | |Character|: | ` | |ASCII Value|: |  97 | |Character|: | a | |ASCII Value|: |  98 | |Character|: | b | |ASCII Value|: |  99 | |Character|: | c |
|ASCII Value|: | 100 | |Character|: | d | |ASCII Value|: | 101 | |Character|: | e | |ASCII Value|: | 102 | |Character|: | f | |ASCII Value|: | 103 | |Character|: | g |
|ASCII Value|: | 104 | |Character|: | h | |ASCII Value|: | 105 | |Character|: | i | |ASCII Value|: | 106 | |Character|: | j | |ASCII Value|: | 107 | |Character|: | k |
|ASCII Value|: | 108 | |Character|: | l | |ASCII Value|: | 109 | |Character|: | m | |ASCII Value|: | 110 | |Character|: | n | |ASCII Value|: | 111 | |Character|: | o |
|ASCII Value|: | 112 | |Character|: | p | |ASCII Value|: | 113 | |Character|: | q | |ASCII Value|: | 114 | |Character|: | r | |ASCII Value|: | 115 | |Character|: | s |
|ASCII Value|: | 116 | |Character|: | t | |ASCII Value|: | 117 | |Character|: | u | |ASCII Value|: | 118 | |Character|: | v | |ASCII Value|: | 119 | |Character|: | w |
|ASCII Value|: | 120 | |Character|: | x | |ASCII Value|: | 121 | |Character|: | y | |ASCII Value|: | 122 | |Character|: | z | |ASCII Value|: | 123 | |Character|: | { |
|ASCII Value|: | 124 | |Character|: | | | |ASCII Value|: | 125 | |Character|: | } | |ASCII Value|: | 126 | |Character|: | ~ |

如果您不希望|ASCII Value|: | 32 | |Character|: | |在其自己的行上,请将if v % 4 == 0:更改为if v % 4 == 3: