将2D列表分成4个部分然后转置它

时间:2016-01-19 18:36:07

标签: python python-3.x

我有一个可以转置列表的函数,但我不知道如何将该列表分成4个相等的块,然后使用该函数单独转置每个列表。以下是我如何转置这些列表:

def rotation(list,m) :
    newlist= [[0] * m for _ in range(m)]
    for i in range(m) :
        for j in range(m):
            newlist[j][m-1-i] = list[i][j]
    for i in range(m) :
        for j in range(m):
            list[i][j] = newlist[i][j]

1 个答案:

答案 0 :(得分:1)

最好使用Numpy进行矩阵运算

  var w = 300,
      h = 200;
  var barHeight = 20;

  var data = 

  championsleague = [{
    "name": "Hart",
    "saves": "9",
    "total": "15",
    "image": "https://upload.wikimedia.org/wikipedia/commons/4/41/Joe_Hart_69775.jpg"
  }, {
    "name": "Subasic",
    "saves": "6",
    "total": "10",
    "image": "https://upload.wikimedia.org/wikipedia/commons/4/41/Joe_Hart_69775.jpg"
  },];

  premierleague = [{
    "name": "Neuer",
    "saves": "12",
    "total": "27",
    "image": "https://upload.wikimedia.org/wikipedia/commons/4/41/Joe_Hart_69775.jpg"
  }, {
    "name": "Forster",
    "saves": "13",
    "total": "22",
    "image": "https://upload.wikimedia.org/wikipedia/commons/4/41/Joe_Hart_69775.jpg"
   }];


// config, add svg
  var canvas = d3.select('#chart')
    .append('svg')
    .attr('width', w)
    .attr('height', h)
    .append('g')
    .attr("transform", "translate(0,70)");

// config, add groups

  var name_g = canvas.append('g')
      .attr("transform", "translate(80,0)");

  var image_g = canvas.append('g');

  var clip = image_g.append("clipPath");

  var Scale = d3.scale.linear().domain([0, 39]).range([0, w]);

 // function that wraps around the d3 pattern (bind, add, update, remove)
  function updateLegend(data) {

 // bind data

  var clip = image_g
      .selectAll('ellipse')
      .data(data);

  var image = image_g
      .selectAll('image')
      .data(data);

    var name = name_g
        .selectAll('text')
        .data(data);

    // add new elements

    name.enter().append('text');
    clip.enter().append('ellipse').attr("id", "ellipse-clip");
    image.enter().append('image');

    // update existing elements

    name.transition()
    .duration(200)
    .text(function(d) {return d.name;
    })
    .attr('x', 0)
     .attr('y', function(d, i) {return i * (h / data.length) -10
     });

    clip.transition()
    .duration(200) 
    .attr("cx", 25)        
    .attr("cy", function(d, i) {return i * (h / data.length) - 25
    })        
    .attr("rx", 30)        
    .attr("ry", 30);  

    image.transition()
    .duration(200)
    .attr('xlink:href', function(d) {return d.image;
         })
    .attr('x', 0)
    .attr('y', function(d, i) {return i * (h / data.length) -50
    })
    .attr('width', 80)
    .attr('height', 80)
    .attr("clip-path", "url(#ellipse-clip)"); 

    // remove old elements

    name.exit().remove();
    clip.exit().remove();
    image.exit().remove();

    };

    // generate initial legend
    updateLegend(data);

    // handle on click event
    d3.selectAll('.opts')
     .on('click', function() {
     var data = eval(d3.select(this).property('value'));
        updateLegend(data);
     })