python矩阵创建,不使用numpy或任何东西以及行和列的最大总和

时间:2016-08-31 12:57:29

标签: python matrix

想在python

中创建如下所示的矩阵
[[y+x for y in range(4)] for x in range(4)]

但不知道该怎么做,我尝试了下面的列表理解

[[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]

提供如下输出

import openfl.net.URLVariables;
import openfl.net.URLRequest;
import openfl.net.URLLoader;
import openfl.events.IOErrorEvent;
import openfl.events.SecurityErrorEvent;
import openfl.system.Security;

public function save() {
    Security.allowDomain("www.roomrecess.com");
    Security.allowDomain("roomrecess.com");

    var sender:URLLoader = new URLLoader();
    var myVar:URLVariables = new URLVariables();
        myVar.score = score;
    var website:URLRequest = new    URLRequest("http://www.roomrecess.com/php/boomSave.php");
        website.data = myVar;           
        sender.addEventListener(Event.COMPLETE, loadComplete);
        sender.addEventListener(IOErrorEvent.IO_ERROR, IOEErrorHandler);
        sender.addEventListener(SecurityErrorEvent.SECURITY_ERROR, SecEvHandler);
        sender.load(website);
}
public function IOEErrorHandler (e:IOErrorEvent)
{
    ground[0].visible = false;
    scoreDisplay.text = e.text;
    scoreFormat.size = 10;
    scoreDisplay.setTextFormat(scoreFormat);
}
public function SecEvHandler (e:SecurityErrorEvent)
{
    ground[4].visible = false;
}
public function loadComplete(e:Event) {

    Security.allowDomain("www.roomrecess.com");
    Security.allowDomain("roomrecess.com");

    background.visible = false;

    sender.removeEventListener(Event.COMPLETE, loadComplete); //changed from loader to sender to match save()

    outputTxt.text = e.target.data; 

    bytes = outputTxt.text;
    mystring = bytes.toString();

    stats = mystring.split(" ");  //changed from int array to string array

    standings(); 
    loadCompleteVar = true;
}

并且还希望打印具有最大总和的行和具有最大总和的列。

提前致谢

4 个答案:

答案 0 :(得分:4)

[[x * 4 + y + 1 for y in range(4)] for x in range(4)]

相当于:

[[(x << 2) + y for y in range(1, 5)] for x in range(4)]

这是一个小基准:

import timeit


def f1():
    return [[x * 4 + y + 1 for y in range(4)] for x in range(4)]


def f2():
    return [[(x << 2) + y for y in range(1, 5)] for x in range(4)]


def f3():
    a = range(1, 5)
    return [[(x << 2) + y for y in a] for x in range(4)]

N = 5000000
print timeit.timeit('f1()', setup='from __main__ import f1', number=N)
print timeit.timeit('f2()', setup='from __main__ import f2', number=N)
print timeit.timeit('f3()', setup='from __main__ import f3', number=N)

# 13.683984791
# 13.4605276559
# 9.65608339037
# [Finished in 36.9s]

我们可以在f1和amp; f2方法提供几乎相同的性能。因此,像f3

一样计算内部range(1,5)只是一个不错的选择

答案 1 :(得分:3)

范围函数接受3个参数startendstep。您可以在步骤4中使用一个范围,使用外部范围创建另一个范围。

>>> [[i for i in range(i, i+4)] for i in range(1, 17, 4)]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

这是使用一个range的另一种方式:

>>> main_range = range(1, 5)
>>> [[i*j + (i-1)*(4-j) for j in main_range] for i in main_range]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

这是一种Numpythonic方法:

>>> n = 4
>>> np.split(np.arange(1, n*n + 1), np.arange(n ,n*n, n))
[array([1, 2, 3, 4]), array([5, 6, 7, 8]), array([ 9, 10, 11, 12]), array([13, 14, 15, 16])]

答案 2 :(得分:1)

试试这个,

In [1]: [range(1,17)[n:n+4] for n in range(0, len(range(1,17)), 4)]
Out[1]: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

答案 3 :(得分:0)

这可能是一个快速修复

[([x-3,x-2,x-1,x]) for x in range(1,17) if x%4 ==0]

但是你的意思是列和行的最大总和