一些Python代码的一些帮助

时间:2009-05-16 14:49:35

标签: python ctypes

有谁能告诉我为什么num_chars和num_rows必须相同?

from ctypes import *

num_chars = 8
num_rows = 8
num_cols = 6

buffer = create_string_buffer (num_chars*num_rows*num_cols+num_chars)

for char in range(num_chars):
        for row in range(num_rows):
                for col in range(num_cols):
                        if char == num_chars-1 and col == num_cols-1:
                                buffer[row*num_rows*num_cols+char*num_cols+col+row] = '|'
                                buffer[row*num_rows*num_cols+char*num_cols+col+row+1] = '\n'
                        elif col == num_cols-1:
                                buffer[row*num_rows*num_cols+char*num_cols+col+row] = '|'
                        else:
                                buffer[row*num_rows*num_cols+char*num_cols+col+row] = ('.', '*')[char>row]

print buffer.value

输出

.....|*****|*****|*****|*****|*****|*****|*****|
.....|.....|*****|*****|*****|*****|*****|*****|
.....|.....|.....|*****|*****|*****|*****|*****|
.....|.....|.....|.....|*****|*****|*****|*****|
.....|.....|.....|.....|.....|*****|*****|*****|
.....|.....|.....|.....|.....|.....|*****|*****|
.....|.....|.....|.....|.....|.....|.....|*****|
.....|.....|.....|.....|.....|.....|.....|.....|

现在将num_chars更改为15。

.....|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
.....|*****|*****|*****|*****|*****|*****|*****|

2 个答案:

答案 0 :(得分:5)

你说你正在使用ctypes,因为你需要可变的char缓冲区。但是你可以从列表理解中获得你想要的输出

num_chars = 5
num_rows = 8
empty = ['.' * num_chars]
full = ['*' * num_chars]
print '\n'.join(
    '|'.join(empty * (i + 1) + (num_rows - i - 1) * full)
    for i in xrange(num_rows)
)

.....|*****|*****|*****|*****|*****|*****|*****
.....|.....|*****|*****|*****|*****|*****|*****
.....|.....|.....|*****|*****|*****|*****|*****
.....|.....|.....|.....|*****|*****|*****|*****
.....|.....|.....|.....|.....|*****|*****|*****
.....|.....|.....|.....|.....|.....|*****|*****
.....|.....|.....|.....|.....|.....|.....|*****
.....|.....|.....|.....|.....|.....|.....|.....

修改

我将向您展示如何使用列表推导来绘制您想要绘制的任何char位图。这个想法很简单。在要打印字符的位置构建一个带True的布尔数组,否则为False。只需使用'或'技巧打印正确的角色。这个例子将构建一个像棋盘一样的象棋。您可以使用相同的概念绘制任何您想要的形状。

rows = 5
cols = 6
char = '#'
empty = '.'
bitmap = [[ (i + j)%2 == 0 for i in xrange(cols)] for j in xrange(rows)]
print '\n'.join(
    '|'.join(bitmap[j][i] * char or empty for i in xrange(cols))
    for j in xrange(rows)
)

答案 1 :(得分:1)

我们走了。我有行 num_rows而不是行 num_chars我必须要一个Dr Pepper。顺便说一句,这不是功课。这是一个LCD项目。

num_chars = 10
num_rows = 8
num_cols = 6

buffer = create_string_buffer (num_chars*num_rows*num_cols+num_chars)

for char in range(num_chars):
        for row in range(num_rows):
                for col in range(num_cols):
                        if char == num_chars-1 and col == num_cols-1:
                                buffer[row*num_chars*num_cols+char*num_cols+col+row] = '|'
                                buffer[row*num_chars*num_cols+char*num_cols+col+row+1] = '\n'
                        elif col == num_cols-1:
                                buffer[row*num_chars*num_cols+char*num_cols+col+row] = '|'
                        else:
                                buffer[row*num_chars*num_cols+char*num_cols+col+row] = ('.', '*')[char>row]

print repr(buffer.raw)
print buffer.value