Python 生成一个从0到n个数字的列表4种方法小结

yipeiwu_com7年前Python基础

我就废话不多说了,直接上代码吧!

第一种

def test1():
  l = []
  for i in range(1000):
    l = l + [i]

第二种(append )

def test2():
  l = []
  for i in range(1000):
    l.append(i)

第三种(列表推导式)

def test3():
  l = [i for i in range(1000)]

第四种(list )

def test4(): 
  l = list(range(1000))

以上这篇Python 生成一个从0到n个数字的列表4种方法小结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python3使用requests发闪存的方法

requests是一个python 轻量的http客户端库,相比python的标准库要优雅很多。接下来通过本文给大家介绍Python3使用requests发闪存的方法,一起学习吧。 使...

numpy向空的二维数组中添加元素的方法

直接上代码了 x = np.empty(shape=[0, 4], int) x = np.append(x, [[1,2,3,4]], axis = 0) x = np.appen...

基于python实现名片管理系统

基于python实现名片管理系统

本文实例为大家分享了python实现名片管理系统的具体代码,供大家参考,具体内容如下 主程序: import cards_tools # 无限循环,由用户主动决定什么时候退出 whi...

python 用下标截取字符串的实例

运行环境: win7 64位 python 2.7 pycharm python 源码如下 # -*- coding: utf-8 -*- str = '0123456789'...

Django中Middleware中的函数详解

一个middleware的例子 import time from django.urls import reverse from django.utils.deprecation...