对python中list的操作,大家可以参考:

Python list 操作

以下是我个人的笔记:

============================================

Add by Hongten 2013-08-14

============================================

 #python list
'''
创建list有很多方法: 1.使用一对方括号创建一个空的list:[]
2.使用一对方括号,用','隔开里面的元素:[a, b, c], [a]
3.Using a list comprehension:[x for x in iterable]
4.Using the type constructor:list() or list(iterable) ''' def create_empty_list():
'''Using a pair of square brackets to denote the empty list: [].'''
return [] def create_common_list():
'''Using square brackets, separating items with commas: [a], [a, b, c].'''
return ['a', 'b', 'c', 1, 3, 5] def create_common_list2():
'''Using a list comprehension: [x for x in iterable].'''
return [x for x in range(1, 10)] def str_to_list(s):
'''Using a string to convert list'''
if s != None:
return list(s)
else:
return [] def main():
test_listA = create_empty_list()
print(test_listA)
print('#' * 50)
test_listB = create_common_list()
print(test_listB)
print('#' * 50)
test_listC = create_common_list2()
print(test_listC)
print('#' * 50)
test_str = 'i want to talk about this problem!'
test_listD = str_to_list(test_str)
print(test_listD) if __name__ == '__main__':
main()

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
[]
##################################################
['a', 'b', 'c', 1, 3, 5]
##################################################
[1, 2, 3, 4, 5, 6, 7, 8, 9]
##################################################
['i', ' ', 'w', 'a', 'n', 't', ' ', 't', 'o', ' ', 't', 'a', 'l', 'k', ' ', 'a', 'b', 'o', 'u', 't', ' ', 't', 'h', 'i', 's', ' ', 'p', 'r', 'o', 'b', 'l', 'e', 'm', '!']
>>>

下面有更多的demo:

 Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> counter = 100
>>> miles = 1000.0
>>> name = "hongten"
>>> numberA,numberB,nameC = 1,2,"Hongten"
>>> list = [counter,miles,name,numberA,numberB,nameC]
>>> print(list)
[100, 1000.0, 'hongten', 1, 2, 'Hongten']
>>> #这是注释部分,注释用"#"开始
>>> for element in list:
print(element) 100
1000.0
hongten
1
2
Hongten
>>> #上面是遍历列表list
>>> print(list[0]) #获取列表list里面的第一个元素值
100
>>> print(list[-1]) #获取列表list里面的最后一个元素值
Hongten
>>> print(len(list)) #用len(list)获取list列表的长度
6
>>> num_inc_list = range(10) #产生一个数值递增的列表
>>> print(num_inc_list)
range(0, 10)
>>> for inc_list in num_inc_list:
print(inc_list) 0
1
2
3
4
5
6
7
8
9
>>> #从这里我们可以看到range(10)是产生了一个从0开始到9的一个数值递增列表
>>> initial_value = 10
>>> list_length = 5
>>> myList = [initial_value for i in range(10)]
>>> print(myList)
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
>>> list_length = 2
>>> myList = myList * list_length
>>> print(myList)
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
>>> print(len(myList))
20
>>> #上面是用一个固定值initial_value去初始化一个列表myList
>>> #同时用myList = myList * list_length去复制myList
>>> #下面再看看复制的效果
>>> copyList = [1,2,3,"hongten"]
>>> copyList = copyList * list_length
>>> print(len(copyList))
8
>>> for cl in copyList:
print(cl) 1
2
3
hongten
1
2
3
hongten
>>> #下面我们来仔细研究一下python里面的list
>>> #在一个list中可以包含不同类型的元素,这个和ActionScript 3.0(AS3.0)中的数组类似
>>> test_list = ["hello",1,2,"world",4,5,"hongten"]
>>> print(len(test_list))
7
>>> print(test_list[0]) # 打印test_list
hello
>>> #打印test_list中的第一元素
>>> print(test_list[-1]) #打印test_list中最后一个元素
hongten
>>> print(test_list[-len]) #打印第一个元素
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
print(test_list[-len]) #打印第一个元素
TypeError: bad operand type for unary -: 'builtin_function_or_method'
>>> print(test_list[-len(test_list)]) #打印第一个元素
hello
>>> print(test_list[len(test_list) - 1]) #打印最后一个元素
hongten
>>> test_list.append(6) #向列表中追加一个元素
>>> print(test_list[-1])
6
>>> test_list.insert(1,0)
>>> print(test_list)
['hello', 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> #上面的操作是向列表test_list的小标为1的地方插入元素0
>>> test_list.insert(1,0)
>>> print(test_list)
['hello', 0, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.insert(2,1)
>>> print(test_list)
['hello', 0, 1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> print(test_list.pop(0)) #返回最后一个元素,并从test_list中删除之
hello
>>> print(test_list)
[0, 1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> print(test_list.pop(2)) #上面的注释有错误,pop(index)的操作是返回数组下标为index的元素,并从列表中删除之
0
>>> print(test_list)
[0, 1, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.remove(1)
>>> print(test_list)
[0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> #remove(1)表示的是删除第一次出现的元素1
>>> test_list.insert(0,1)
>>> print(test_list)
[1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.remove(1)
>>> print(test_list)
[0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.insert(2,"hongten")
>>> print(test_list)
[0, 1, 'hongten', 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.count("hongten")
2
>>> #count(var)是统计var元素在列表中出现的个数
>>> test_list.count("foo")
0
>>> test_list_extend = ["a","b","c"]
>>> test_list.extend(test_list_extend)
>>> print(test_list)
[0, 1, 'hongten', 2, 'world', 4, 5, 'hongten', 6, 'a', 'b', 'c']
>>> #使用extend(list)作用是追加一个list到源list上面
>>> print(test_list.sort())
Traceback (most recent call last):
File "<pyshell#76>", line 1, in <module>
print(test_list.sort())
TypeError: unorderable types: str() < int()
>>> test_list_extend.append("h")
>>> test_lsit_extend.append("e")
Traceback (most recent call last):
File "<pyshell#78>", line 1, in <module>
test_lsit_extend.append("e")
NameError: name 'test_lsit_extend' is not defined
>>> list_a = ["e","z","o","r"]
>>> list_a.extend(test_list_extend)
>>> print(list_a)
['e', 'z', 'o', 'r', 'a', 'b', 'c', 'h']
>>> print(list_a.sort()) #对list_a列表进行排序
None
>>> #不知道为什么以上排序都有报错......
>>> list_b = [1,3,5,2,6,4]
>>> print(list_b.sort())
None
>>> print(sort(list_b))
Traceback (most recent call last):
File "<pyshell#86>", line 1, in <module>
print(sort(list_b))
NameError: name 'sort' is not defined
>>> #不去管排序问题了,先看看删除操作吧!!!!!
>>> print(list_b)
[1, 2, 3, 4, 5, 6]
>>> print(del list_b[1])
SyntaxError: invalid syntax
>>> del list_b[1]
>>> print(list_b)
[1, 3, 4, 5, 6]
>>> del list_b[0,2]
Traceback (most recent call last):
File "<pyshell#92>", line 1, in <module>
del list_b[0,2]
TypeError: list indices must be integers, not tuple
>>> del list_b[0:2]
>>> print(list_b)
[4, 5, 6] >>> #del list[index]删除下标为index的元素,del list[start:end]删除从start下标开始到end下标结束的元素
>>> del list_b[10]
Traceback (most recent call last):
File "<pyshell#96>", line 1, in <module>
del list_b[10]
IndexError: list assignment index out of range
>>> #如果我们删除的下标超出了列表的长度范围,就会报错啦!!!!!
>>> ##########################################################################
>>> list_c = range(5);
>>> for c in list_c:
print(c) 0
1
2
3
4
>>> list_d = list_c
>>> for d in list_d:
print(d) 0
1
2
3
4
>>> #上面是列表的复制
>>> list_d[2] = 23
Traceback (most recent call last):
File "<pyshell#108>", line 1, in <module>
list_d[2] = 23
TypeError: 'range' object does not support item assignment
>>> list_e = [1,2,3,4,5]
>>> list_f = list_e
>>> list_f[2] = 234
>>> print(list_e)
[1, 2, 234, 4, 5]
>>> #从这里我们可以知道,list_f复制了list_e,list_f是对list_e的一个引用,
>>> #他们共同指向一个对象:[1,2,3,4,5],当我们视图修改list_f[2]的值的时候,
>>> #list_f所指向的对象的行为发生了变化,即元素值发生了变化,但是他们的引用是没有
>>> #发生变化的。所以list_e[2] = 234也是在情理之中。
>>> #######################################################################
>>> list_i = list_e[:]
>>> print(list_i)
[1, 2, 234, 4, 5]
>>> print(list_e)
[1, 2, 234, 4, 5]
>>> list_i[2] = 3
>>> print(list_e)
[1, 2, 234, 4, 5]
>>> print(list_i)
[1, 2, 3, 4, 5]
>>> #上面是进行了列表的克隆操作,即拷贝了另一个列表,这样的操作,会创造出新的一个列表对象
>>> #使得list_i和list_e指向不同的对象,就有着不同的引用,所以当list_i[2] = 3的时候,
>>> #list_e[2]还是等于234,即不变
>>>

python开发_python中的list操作的更多相关文章

  1. python开发_python中字符串string操作

    在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号' ...

  2. python开发_python中的Boolean运算和真假值

    python中的真假值: Truth Value Testing Any object can be tested for truth value, for use in an if or while ...

  3. python开发_python中str.format()

    格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看python中的字符串格式函数str.format(): 1 #使用str.format()函数 2 3 #使用 ...

  4. python开发_python中的range()函数

    python中的range()函数的功能hen强大,所以我觉得很有必要和大家分享一下 就好像其API中所描述的: If you do need to iterate over a sequence o ...

  5. python开发_python中的module

    在python中,我们可以把一些功能模块化,就有一点类似于java中,把一些功能相关或者相同的代码放到一起,这样我们需要用的时候,就可以直接调用了 这样做的好处: 1,只要写好了一个功能模块,就可以在 ...

  6. python开发_python中的函数定义

    下面是我做的几个用列: #python中的函数定义,使用和传参 def_str = '''\ python中的函数以如下形式声明: def 函数名称([参数1,参数2,参数3......]): 执行语 ...

  7. python开发_python中的变量:全局变量和局部变量

    如果你在为python中的变量:全局变量和局部变量头疼,我想这篇blog会给你帮助 运行效果: 代码部分: #Python中的变量:全局变量和局部变量 #在很多语言中,在声明全局变量的时候,都喜欢把全 ...

  8. python开发_python中for循环操作

    如果你对python中的for循环不是很清楚,请看看这篇文章:”for循环控制语句——菜鸟的Python笔记“ 下面是我做的一些学习记录供大家参考: #基本的for循环语句 test_list = [ ...

  9. python开发_python关键字

    python3.3.2中的关键字如下: The following identifiers are used as reserved words, or keywords of the languag ...

随机推荐

  1. iOS-----获取当前app的名称和版本号

    iOS获取当前App的名称和版本号 第一步 如图中Info.plist中鼠标点击右键,出现选项框,选着" Show Raw Keys/Values " 第二步 用下面代码就可以获取 ...

  2. 【排序】插入排序,C++实现

    # 基本思想 每一步将一个待排序的记录,插入到前面已经排好序的有序序列中去,直到插完所有元素为止. # C++代码 #include<iostream> #include<vecto ...

  3. lx 与cd 的计算方法

    Candela to lux calculation with distance in meters The illuminance Ev in lux (lx) is equal to the lu ...

  4. NET MVC 升级到5.1后,View视图中代码报错

    使用nuget将项目中MVC4 升级到MVC5,之后项目还可以正常编译运行, 但View视图中相关的很多代码都报错,比如: 1.@model找不到 2.@Html找不到,本该是System.Web.M ...

  5. Roslyn 入门:使用 .NET Core 版本的 Roslyn 编译并执行跨平台的静态的源码

    Roslyn 是微软为 C# 设计的一套分析器,它具有很强的扩展性.以至于我们只需要编写很少量的代码便能够编译并执行我们的代码. 作为 Roslyn 入门篇文章之一,你将可以通过本文学习如何开始编写一 ...

  6. VSCode插件开发

    VSCode插件开发全攻略(十)打包.发布.升级 发布方式 插件开发完了,如何发布出去分享给他人呢?主要有3种方法: 方法一:直接把文件夹发给别人,让别人找到vscode的插件存放目录并放进去,然后重 ...

  7. CALayer4-自定义层

    一.自定义层的方法1 方法描述:创建一个CALayer的子类,然后覆盖drawInContext:方法,使用Quartz2D API进行绘图 1.创建一个CALayer的子类 2.在.m文件中覆盖dr ...

  8. Windows10安装MySQL8.0

    1.到MySQL官网下载安装包:https://dev.mysql.com/downloads/mysql/:选择8.0版本: 2.将下载好的安装包(mysql-8.0.12-winx64 .zip) ...

  9. LG1343 地震逃生

    题意 汶川地震发生时,四川**中学正在上课,一看地震发生,老师们立刻带领x名学生逃跑,整个学校可以抽象地看成一个有向图,图中有n个点,m条边.1号点为教室,n号点为安全地带,每条边都只能容纳一定量的学 ...

  10. tomcat  nginx  证书切换

    1. 导出公钥 keytool -export -alias tomcat -keystore <you jks>wsriakey.keystore -file <outputfil ...