3

我很困惑如何将这个程序转换为列表理解。有小费吗?

这是我写的原始代码(另外,我是 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()

这是我尝试进行列表理解的尝试……显然行不通。我很困惑将计数变量放在哪里?我正在尝试使用列表推导和 str.join()s 来减少它。

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

4 回答 4

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?)
于 2013-06-11T04:06:03.967 回答
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'.

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

于 2013-06-11T04:08:41.547 回答
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()和充分利用元组解包,我跳过了对计数器的需要。

于 2013-06-11T04:08:56.863 回答
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:

于 2013-06-11T04:09:59.083 回答