使用zip的矩阵转置?

时间:2016-08-27 05:38:24

标签: python matrix transpose

着迷于拉链,我发现可以用它进行矩阵转置。
这是python代码:

 public int countWeekendDays() {
    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.DAY_OF_WEEK, Calendar.DATE+1);
    int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    int count = 0;
    for (int day = 1; day <= daysInMonth; day++) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.DATE+1, day);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY) {
            if(dayOfWeek==Calendar.SUNDAY)
                calendar.add(Calendar.DATE, Calendar.SUNDAY-1);
            else if(dayOfWeek==Calendar.SATURDAY)
                calendar.add(Calendar.DATE, Calendar.SATURDAY);

            disable.add(calendar.getTime());
        }

    }
    for(int i=0;i<disable.size();i++)
    {
        Log.e("***&***",disable.get(i).toString());
    }
    return count;
}

但我对我的代码看起来不太满意..我该如何改进?

谢谢!

编辑:

import sys
n = raw_input("enter the number of rows of the matrix:")
x = []
for i in range(int(n)):
    y = map(int,raw_input().split())
    x.append(y)
mat = zip(*x)

print ""

for i in mat:
    for j in i:
        sys.stdout.write(str(j))
        sys.stdout.write(" ")
    sys.stdout.write("\n")

1 个答案:

答案 0 :(得分:2)

带zip的矩阵转置:

a = [[1,2],[3,4]]

a_t =list(map(list, zip(*a)))