python 列表推导式

示例选自 《Expert Python Programming》

未使用列表推导式:

>>> numbers = range(10)
>>> size = len(numbers)
>>> evens = []
>>> i = 0
>>> while i >> evens
[0, 2, 4, 6, 8]

使用列表推导式:

>>> [i for i in range(10) if i % 2 == 0]
[0, 2, 4, 6, 8]

python 内建函数 range()

代码说明一切

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10,3)
[1, 4, 7]
>>> range(10,1,-3)
[10, 7, 4]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(10,)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

最近终于下定决心在硬盘上安装了 Ubuntu

最近参考 http://opensource.csdn.net/bbs/thread/8343 这篇帖子, 在 Windows 7 中用硬盘安装的方法安装了 Ubuntu 10.10
整个过程的感受是:安装方法简单。对 Ubuntu 的直观感受是:安装系统时间短,运行速度快,占有资源少(开到最高特效),开关机速度快(尤其是关机速度)。

Bytes 智能转换为 KB, MB,GB

本文参考了 http://otnv.pixnet.net/blog/post/29073136

代码:

function readablizeBytes(bytes) {
    var s = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
    var e = Math.floor(Math.log(bytes)/Math.log(1024));
    return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
}

用法:

readablizeBytes(8589312); // return 8.19 MB

实例:
Firefox 扩展 DiB 的功能如下:
DiB1
可以看出此扩展是以 Bytes 为单位的,不够友好。现在用本文的代码对源代码进行改动,达到文件大小智能显示的效果:
DiB2
只须改动文件 扩展\chrome\content\overlay.js 即可
改动前:

var showDialogAddonDiB =
{
  init: function()
  {
    if ("contentLength" in dialog.mLauncher)
    {
      var bytes = dialog.mLauncher.contentLength;
      var type = dialog.dialogElement("type");
      if (bytes != -1)
      {
        type.value += " (" + bytes.toLocaleString() + " Bytes)";
        type.setAttribute("tooltiptext", type.value);
      }
    }
  }
};
dialog.mDialog.addEventListener("load", function() { showDialogAddonDiB.init(); }, false);

改动后:

var showDialogAddonDiB =
{
  init: function()
  {
    if ("contentLength" in dialog.mLauncher)
    {
      var bytes = dialog.mLauncher.contentLength;
      var type = dialog.dialogElement("type");
      if (bytes != -1)
      {
        type.value += " ( " + readablizeBytes(bytes).toLocaleString() + " )";
        type.setAttribute("tooltiptext", type.value);
      }
    }

    function readablizeBytes(bytes) {
        var s = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
        var e = Math.floor(Math.log(bytes)/Math.log(1024));
        return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
    }
  }

};
dialog.mDialog.addEventListener("load", function() { showDialogAddonDiB.init(); }, false);

[JavaScript]Bytes 智能转换为 KB, MB,GB

本文参考了 http://otnv.pixnet.net/blog/post/29073136

代码:

function readablizeBytes(bytes) {
    var s = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
    var e = Math.floor(Math.log(bytes)/Math.log(1024));
    return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
}

用法:

readablizeBytes(8589312); // return 8.19 MB

实例:
Firefox 扩展 DiB 的功能如下:
DiB1
可以看出此扩展是以 Bytes 为单位的,不够友好。现在用本文的代码对源代码进行改动,达到文件大小智能显示的效果:
DiB2
只须改动文件 扩展\chrome\content\overlay.js 即可
改动前:

var showDialogAddonDiB =
{
  init: function()
  {
    if ("contentLength" in dialog.mLauncher)
    {
      var bytes = dialog.mLauncher.contentLength;
      var type = dialog.dialogElement("type");
      if (bytes != -1)
      {
        type.value += " (" + bytes.toLocaleString() + " Bytes)";
        type.setAttribute("tooltiptext", type.value);
      }
    }
  }
};
dialog.mDialog.addEventListener("load", function() { showDialogAddonDiB.init(); }, false);

改动后:

var showDialogAddonDiB =
{
  init: function()
  {
    if ("contentLength" in dialog.mLauncher)
    {
      var bytes = dialog.mLauncher.contentLength;
      var type = dialog.dialogElement("type");
      if (bytes != -1)
      {
        type.value += " ( " + readablizeBytes(bytes).toLocaleString() + " )";
        type.setAttribute("tooltiptext", type.value);
      }
    }

    function readablizeBytes(bytes) {
        var s = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
        var e = Math.floor(Math.log(bytes)/Math.log(1024));
        return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
    }
  }

};
dialog.mDialog.addEventListener("load", function() { showDialogAddonDiB.init(); }, false);