`
newand
  • 浏览: 36667 次
  • 性别: Icon_minigender_1
  • 来自: nj
最近访客 更多访客>>
社区版块
存档分类
最新评论

list in python

阅读更多

1.插入元素

>>> li = ['a', 'b', 'z', 'example']
>>> li.insert(2,'new')#在指定的位置插入
>>> li
['a', 'b', 'new', 'z', 'example']
>>> li.append('app')#插入到末尾
>>> li
['a', 'b', 'new', 'z', 'example', 'app']
 

2.extend和append

 

>>> li= [1,2,3]
>>> li.append([4,5])#将[]做为一个整体加进list里去
>>> li
[1, 2, 3, [4, 5]]
>>> li.extend([4,5])#将[]里的元素分别加进list里去
>>> li
[1, 2, 3, [4, 5], 4, 5]

 

3. slice

(start:end:step)默认值(0:list的长度:1)

>>> li = [11,22,33,44]
>>> li[:1]#从0到1
[11]
>>> li[1:]#从0到3
[22, 33, 44]
>>> li[1:3]#从1到3
[22, 33]
>>> li[::1]#从0到4,以1自加
[33, 22, 33, 44]
>>> li[::2]#从0到4,以2自加
[33, 33]

 4.list 的'+'操作

>>> li = [1,2]
>>> li = li+[3]#可以用这种方式来添加到list,不过慢了点
>>> li
[1, 2, 3]
>>> li = li+[5,6]#这样的方式和extend效果差不多,不过也稍微慢一些
>>> li
[1, 2, 3, 5, 6]
>>> li2 = ['a','b']
>>> li2+li#可以直接利用加操作将两个list连接起来
['a', 'b', 1, 2, 3, 5, 6]
>>> 
 

5.

row = [0] * 5
multi = [row] * 3
#row  是5个对0 的引用
#multi 则是3个对row的引用
#multi[0][0] = 'some'这个修改是对row这个引用的第一个元素的修改,因此会全部变化。
>>> multi[0][0] = 'some'
>>> 
>>> multi
[['some', 0, 0, 0, 0], ['some', 0, 0, 0, 0], ['some', 0, 0, 0, 0]]

#如果不想创建某个list 的引用,则应用下面这样的方式
multilist = [[0 for col in range(5)] for row in range(10)]
 

 

参考:

   1.《dive into python》

   2.《python cookbook》

   3.http://www.java2s.com/Code/Python/

3
3
分享到:
评论

相关推荐

    Complex Network Analysis in Python

    background in computer programming—namely, in Python programming. It expects from you no more than common sense knowledge of complex networks. The intention is to build up your CNA programming skills...

    Data Structures and Algorithms in Python

    derpinnings of Python’s built-in list, tuple, and str classes. New Appendix A serves as an additional reference regarding the functionality of the str class. • Over 450 illustrations have been ...

    使用python list 查找所有匹配元素的位置实例

    如下所示: import re word = test s = test abcdas test 1234 testcase testsuite ... 您可能感兴趣的文章:Python 查找list中的某个元素的所有的下标方法python 获取list特定元素下标的实例讲解python

    Python 查看list中是否含有某元素的方法

    用关键字 in 和not in 来 ... 您可能感兴趣的文章:python list是否包含另一个list所有元素的实例Python判断两个list是否是父子集关系的实例python对list中的每个元素进行某种操作的方法Python之list对应元素求和的方法

    Python中List.index()方法的使用教程

    index()方法返回obj出现在列表中最低位索引。.../usr/bin/python aList = [123, 'xyz', 'zara', 'abc']; print Index for xyz : , aList.index( 'xyz' ) ; print Index for zara : , aList.index( 'zara

    Python处理CSV与List的转换方法

    1.读取CSV文件到List def readCSV2List(filePath): try: file=open(filePath,'r',encoding=gbk)# 读取以utf-8 context = file.read() # 读取成... for i in range(length): list_result[i]=list_result[i].split

    Modern Python Standard Library Cookbook

    It contains an exhaustive list of libraries, and this book will help you choose the best one to address specific programming problems in Python. The Modern Python Standard Library Cookbook begins ...

    Python 查找list中的某个元素的所有的下标方法

    /usr/bin/env python #_*_ coding:utf-8 _*_ name = ['hello', 'world', 'a', 'b', 'c', 1, 2, 3, 'hello', 'world', 'a', 'b', 'c', 1, 2, 3] first_pos = 0 for i in range(name.count(2)): new_list = name...

    python中in在list和dict中查找效率的对比分析

    今天小编就为大家分享一篇python中in在list和dict中查找效率的对比分析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    python七种方法判断字符串是否包含子串

    in 和 not in 在 Python 中是很常用的关键字,我们将它们归类为 成员运算符。 使用这两个成员运算符,可以很让我们很直观清晰的判断一个对象是否在另一个对象中,示例如下: >>> llo in hello, python True >>> >>>...

    Python-Python数据科学开发库汇总列表

    Curated list of data science software in Python

    Python判断一个list中是否包含另一个list全部元素的方法分析

    本文实例讲述了Python判断一个list中是否包含另一个list全部元素的方法。分享给大家供大家参考,具体如下: 你可以用for in循环+in来判断 #!/usr/bin/env python # coding: utf-8 a = [1, 2, 3, 4, 5] b = [3, 4, 5...

    python numpy 一维数组转变为多维数组的实例

    new_list = [i for i in range(9)] numpy.array(new_list).reshape(3,3) 借助numpy库; 以上这篇python numpy 一维数组转变为多维数组的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多...

    Modern.Python.Cookbook.epub

    Built-in Data Structures – list, set, dict Chapter 5. User Inputs and Outputs Chapter 6. Basics of Classes and Objects Chapter 7. More Advanced Class Design Chapter 8. Functional and Reactive ...

    python基础教程:Python中在for循环中嵌套使用if和else语句的技巧.pdf

    下⾯举⼏个简单的例⼦进⾏说明 >>> a=[12, 3, 4, 6, 7, 13, 21] >>> newList = [x for x in a] >>> newList [12, 3, 4, 6, 7, 13, 21] >>> newList2 = [x for x in a if x%2==0] >>> newList2 [12, 4, 6] 省略if后...

    Python实现输出某区间范围内全部素数的方法

    本文实例讲述了Python实现输出某区间范围内全部素数的方法。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- # 简述:区间范围101-200 # 要求:判断这个区间内有多少个素数,并逐一输出。 def prime(m,n...

    Python安装包version 3.1.5

    that this installs the Python executable in a place that is not normally on your PATH, you may want to set up a symlink in /usr/local/bin. On Windows, see PCbuild/readme.txt. If you wish, you can ...

    python 基础-数据结构列表list.docx

     常用方法:+, *, in, x[i], x[i:j], len, del, append insert, pop, count, reverse, index, sort  运行list_test.py  Print语句末尾加逗号,可消除自动换行  Sort方法影响列表本身  体会各种方法,可变...

    Python中list循环遍历删除数据的正确方法

    初学Python,遇到过这样的问题,在遍历list的时候,删除符合条件的数据,可是总是报异常,代码如下: num_list = [1, 2, 3, 4, 5] print(num_list) for i in range(len(num_list)): if num_list[i] == 2: num_...

Global site tag (gtag.js) - Google Analytics