Python和矩阵,移动列和&行

时间:2018-05-23 22:04:35

标签: python matrix

我的python脚本有点问题。

实际上,我正在研究真正的led矩阵8x8,我想绘制一个带动画的文本(从右到左,从左到右,从上到下,从下到上)。

例如,我想写E字母。所以我创建了E矩阵:

matrix =[
"1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1",
"1","1","0","0","0","0","0","0",
"1","1","1","1","1","0","0","0",
"1","1","1","1","1","0","0","0",
"1","1","0","0","0","0","0","0",
"1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1"]

Like this

但是现在,想象一下我想移动我的E来制作动画。如果我向左移动我的E,第八列将是空的,并且在1秒后,第七列也是等等... 当我的矩阵完全空了,我的E字母将再次从正确的位置开始......

你知道我怎么能用数组,矩阵或其他什么来做?

谢谢

编辑,使用https://www.jdoodle.com/python3-programming-online进行代码测试:

matrix = [  [0, 0, 0, 1, 0], 
            [0, 0, 0, 1, 0],
            [0, 0, 0, 1, 0], 
            [0, 0, 0, 1, 0], 
            [0, 0, 0, 1, 0]]

print(matrix)

def shift(my2dArray):
    for _ in range(5): # shift to the left 8 times - brute force
        for row in my2dArray:
            row.pop(0) # removes first element of each row
            row.append("0") # add "0" to the end of the row
        # code to update display here and sleep for a bit

shift(matrix)

print(matrix)

2 个答案:

答案 0 :(得分:1)

(正如s3n0所建议的)首先,你想要声明一个二维数组

 my2dArray = [["1","1","1","1","1","1","1","1"],
    ["1","1","1","1","1","1","1","1"],
    ["1","1","0","0","0","0","0","0"],
    ["1","1","1","1","1","0","0","0"],
    ["1","1","1","1","1","0","0","0"],
    ["1","1","0","0","0","0","0","0"],
    ["1","1","1","1","1","1","1","1"],
    ["1","1","1","1","1","1","1","1"]]

然后,为了简单的算法,您可以执行以下操作...(假设8x8网格):

def shift_left(my2dArray):
    for _ in range(8): # shift to the left 8 times - brute force
        for row in my2dArray:
            row.pop(0) # removes first element of each row
            row.append("0") # add "0" to the end of the row
        # code to update display here and sleep for a bit

这是将显示器向左移动的一个示例,我将把剩下的(其他方向)留给你去弄清楚,因为它们与此算法非常相似。

要上下移动,只需在算法中向上调用列表函数:

def shift_up(my2dArray):
    for _ in range(8): # shift to the left 8 times - brute force
        my2dArray.pop(0) # removes first element of each column
        my2dArray.append(["0","0","0","0","0","0","0","0"]) # add "0" to the end of the columns
        # code to update display here and sleep for a bit

答案 1 :(得分:0)

另一种方式是numpy。哪个也可以扩展。无论你的数组的维度。你只需改变形状。我运行的代码在下面,工作正常。只需将for循环中的数字8替换为数组的形状即可。

// google map geocode api url
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&sensor=false&key=".$google_api_key;

$resp_json = get_web_page($url);

// method
function get_web_page( $url, $cookiesIn = '' ){
        $options = array(
            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => true,     //return headers in addition to content
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
            CURLOPT_TIMEOUT        => 120,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
            CURLINFO_HEADER_OUT    => true,
            CURLOPT_SSL_VERIFYPEER => false,     // Validate SSL Cert
            CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
            CURLOPT_COOKIE         => $cookiesIn
        );

        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $rough_content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );
        curl_close( $ch );

        $header_content = substr($rough_content, 0, $header['header_size']);
        $body_content = trim(str_replace($header_content, '', $rough_content));
        $pattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m"; 
        preg_match_all($pattern, $header_content, $matches); 
        $cookiesOut = implode("; ", $matches['cookie']);

        $header['errno']   = $err;
        $header['errmsg']  = $errmsg;
        $header['headers']  = $header_content;
        $header['content'] = $body_content;
        $header['cookies'] = $cookiesOut;

        if($errmsg != ''){
            echo json_encode(" ERROR ". $errmsg);
        }

    return $body_content;
}