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 tkinter模块安装过程

引言: 在Python3下运行Matplotlib之时,碰到了”No module named _tkinter“的问题,花费数小时进行研究解决,这里讲整个过程记录下来,并尝试分析过程中...

pygame播放音乐的方法

本文实例讲述了pygame播放音乐的方法。分享给大家供大家参考。具体如下: 方法一: import pygame filename='/home/A.mp3' pygame.mixe...

python脚本内运行linux命令的方法

本文实例讲述了python脚本内运行linux命令的方法。分享给大家供大家参考。具体实现方法如下: #/usr/bin/env python import subprocess cl...

python字符串过滤性能比较5种方法

python字符串过滤性能比较5种方法比较 总共比较5种方法。直接看代码: import random import time import os import string ba...

Python3中urlencode和urldecode的用法详解

在Python3中,将中文进行urlencode编码使用函数 urllib.parse.quote(string, safe='/', encoding=None, errors=N...