`

Python Initialise a list to a specific length

阅读更多

Below show some tricks that you can play to create a list of certain size

 

 

 

 

 

def main():
    check(5, '''/path/to/CommandLine/CommandLine.exe''', ">null 2>&1")
#    for i, v in enumerate(initializeList(5)):
#        print "i: {0}, v: {1}" % (i, v)
    for i, v in enumerate(initializeList(5, value = None)):
        print "{0}:{1}".format(i, v)
    for i, v in enumerate(initializeList2(5, value = None)):
        print "{0}:{1}".format(i, v)


def initializeList(size, value=None):
#    l = [None] * size
    l = []
    for i in range(size):
        l.append(value)
    return l
    
def initializeList2(size, value=None):
    return [value] * size; 

 

 

So in a nutshell, they are two ways that you can create and initalize a list with a fixed size, 

 

 

  • with the * operator on list (which is to create duplicate the list n times)
  • create an empty list and stack things onto it

 

 

 

 

分享到:
评论

相关推荐

    commons-logging-1.2

    When writing a library it is very useful to log information.... in this case an application may be able to avoid containing any code that is specific to the logging implementation used.

    flutter_local_notifications..zip

    《Flutter本地通知插件详解与应用实践》 Flutter作为Google推出的跨平台开发框架,以其高效、灵活和丰富的UI库赢得了开发者们的青睐。在移动应用开发中,本地通知是一种不可或缺的功能,它可以在用户不直接与应用...

    A Text File Logging system for use in VC++ projects (3KB)

    在VC++项目中,日志记录系统是一种必不可少的工具,用于跟踪程序运行时的状态和错误。这个3KB的文本文件日志系统专为VC++项目设计,提供了简单但实用的功能,便于开发人员记录和分析程序行为。下面我们将深入探讨这...

    DIBSection Class VB

    A DIB (Device Independent Bitmap) Section is a GDI object like a standard DIB but which additionally provides a pointer to the memory used to store the bitmap bits to which the creating ...

    Python-game-code:使用pygame在python中进行平台游戏

    Python游戏代码导入pygame #Initialise pygame pygame.init() #创建游戏窗口Width = 800 Length = 600 Window = pygame.display.set_mode((Width,Length)) #字符的位置,大小和速度变化char_Width = 40 ...

    c语言或者c++ :Bit manipulations 位操作

    Write a function to print a long integer on the console in hex and binary form. Now perform the following operations on the pattern and print out the results of each operation in hex and binary format...

    Using DIB Sections in VB

    Using DIB Sections in VB An Easy to Use Class for manipulating DIBSections plus a blindingly quick technique for updating the bits <br> <br> <br> Updated! 17 February 1999 The...

    1302和计算器 org 0000h

    Routine to output character in A, preserving all but A. sndchr: push R0B0 push R1B0 call getmode mov digitcode,A call storedigit pop R1B0 pop R0B0 ret ;Routine to print error message ...

    NX二次开发UF-CLONE-initialise 函数介绍

    NX二次开发UF_CLONE_initialise 函数介绍,Ufun提供了一系列丰富的 API 函数,可以帮助用户实现自动化、定制化和扩展 NX 软件的功能。无论您是从事机械设计、制造、模具设计、逆向工程、CAE 分析等领域的专业人士,...

    80c51 Caculator

    Initialise the constant buffer to 100. Primarily used for % ops. mov digitcode,#031h call storedigit mov digitcode,#030h call storedigit mov digitcode,#030h call storedigit。。。。。。。...

    一个win32下的ARM开源编译器

    look at a disassembly listing and match it to the original source. Example: What you coded | What is assembled ;reason -------------------+--------------------------------------------------- ...

    modellogger.github.io:Model-Logger是一个Python库,用于存储模型的配置文件和模型之间的快速比较

    模型记录器 model-logger是一个Python库,用于存储模型的配置... modellogger import ModelLogger#initialise a modelloger instancepath = "c/path/to/db/databasename.db"mlog = ModelLogger ( path ) #setup compl

    pingy-cli:简单的前端构建工具。 没有配置,没有插件

    export Export website to a folder for distribution -h, --help output usage information -V, --version output the version number init Usage: pingy init [options] Initialise a new or existing website...

    requests-ip-rotator:一个 Python 库,利用 AWS API Gateway 的大型 IP 池作为代理来生成用于网络抓取和暴力破解的伪无限 IP

    请求-ip-旋转器 一个 Python 库,利用 AWS API Gateway 的大型 IP 池作为代理来生成用于...# Create gateway object and initialise in AWS gateway = ApiGateway ( "https://site.com" ) gateway . start () # Assig

    timetester:用于测试功能运行时的Python包,其中包含一些额外的功能

    pip install timetester 或者python3 -m pip install timetester 用法 时间测试器对象 import timetester def foo ( arg ): print ( arg ) pass k = timetester . timeTester ( foo ) # NOT NECESSARY. Only use ...

    question2.rar_The First

    Q1 Create a program that will initialise Port2 bits 0 - 7 to outputs. Then in an endless loop perform the sequence of bit manipulations shown below. Your program should use bit masking operations. ...

    tesseract.net 4.0 配中英文识别库 验证码识别

    tesseract.net 4.0 配英文识别库 eng.traineddata osd.traineddata 实现内存图像bitmap的直接识别,无需保存成图片再识别 增加 中值法去噪点 颜色反转 去杂点 二值化 灰度处理 等简单的图像处理 ...

    biostructmap:用于将序列比对数据映射到蛋白质结构的Python软件包

    生物结构图 Biostructmap是一个Python工具,用于将序列比对的数据(例如多态...# Initialise structure object structure = biostructmap.Structure('1zrl.pdb', 'test_pdb_name') # The location of known polymorphi

    C# winform tesseract-ocr演示代码

    测试环境: vs2019 netframework4.7.2 博客地址: blog.csdn.net/FL1623863129/article/details/135525157 视频演示: https://www.bilibili.com/video/BV1uT4y1n7SK/

    Android代码-Once

    Once A small Android library to manage one-off operations for API 9 and higher. Some things should happen...First things first, you'll need to initialise Once on start up. In your Application class's

Global site tag (gtag.js) - Google Analytics