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设计】。

相关文章

python创建与遍历List二维列表的方法

python 创建List二维列表 lists = [[] for i in range(3)] # 创建的是多行三列的二维列表 for i in range(3): lists...

vscode 远程调试python的方法

vscode 远程调试python的方法

本文介绍了vscode 远程调试python的方法,分享给大家,具有如下: 实验环境 远程服务器:京东云,1核2G,centos7.3 64bit 本地环境配置 安装vscode...

Python操作MongoDB数据库PyMongo库使用方法

引用PyMongo 复制代码 代码如下: >>> import pymongo 创建连接Connection 复制代码 代码如下: >>> impo...

Python向excel中写入数据的方法

Python向excel中写入数据的方法

最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中。 数据导入之前需要安装 xlwt依赖包,安装的方法就很...

python如何读取bin文件并下发串口

下面是实现代码 # coding:utf-8 import time, serial from struct import * import binascii file = ope...