码迷,mamicode.com
首页 > 编程语言 > 详细

python列表

时间:2016-09-29 01:38:01      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> dir(list)
[__add__, __class__, __contains__, __delattr__, __delitem__, __delslice__, __doc__, __eq__, __format__, __ge__, __getattribute__, __getitem__, __getslice__, __gt__, __hash__, __iadd__, __imul__, __init__, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __reversed__, __rmul__, __setattr__, __setitem__, __setslice__, __sizeof__, __str__, __subclasshook__, append, count, extend, index, insert, pop, remove, reverse, sort]
>>> list1=[1,2,3]
>>> list2=[4,5,6]
>>> list3=list1+list2
>>> list3
[1, 2, 3, 4, 5, 6]
>>> list3*=2
>>> list3
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
>>> 3 in list3
True
>>> 7 in list3
False
>>> 7 not in list3
True
>>> list4["niha",[2,3],q]

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    list4["niha",[2,3],q]
NameError: name list4 is not defined
>>> list4=["niha",[2,3],q]
>>> list4
[niha, [2, 3], q]
>>> 2 in list4
False
>>> 2 in list[1]

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    2 in list[1]
TypeError: type object has no attribute __getitem__
>>> 2 in list4[1]
True
>>> list3.count(1)
2
>>> list3.index(2)
1
>>> list3.index(4,1,10)
3
>>> list3.index(4,2,10)
3
>>> list3.reverse()
>>> list3
[6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1]
>>> 
>>> 
>>> list5=[3,7,4,9,1,8]
>>> list5.sort()
>>> list5
[1, 3, 4, 7, 8, 9]
>>> list5.so

Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    list5.so
AttributeError: list object has no attribute so
>>> list5.sort(reverse=True)
>>> list5
[9, 8, 7, 4, 3, 1]
>>> 

python自带的sort函数其实有3个参数,都有默认的值,第一个是func,代表解决的算法,python的sort是由归并排序解决的,第二格式key和func想对应。第三个是reverse就是反转,默认的值为false,如果希望、

sort从大到小排序,就将reverse赋值为True,

 

 

 

list链表的复制拷贝

必须要用slice切片赋值,那样cachi开出新的空间创建一个数组,否之直接赋值就相当于c语言中的引用起了一个别名,实际指向的是同一快空间,操作一个都会对另一个造成影响

>>> 
>>> list_1=[1,2,6,3,9,2]
>>> list_2=list_1[:]
>>> list_3=list_1
>>> list_2
[1, 2, 6, 3, 9, 2]
>>> list_3
[1, 2, 6, 3, 9, 2]
>>> list_1.sort()
>>> list_1
[1, 2, 2, 3, 6, 9]
>>> list_2
[1, 2, 6, 3, 9, 2]
>>> list_3
[1, 2, 2, 3, 6, 9]
>>> 

 

 

 

Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2Type "copyright", "credits" or "license()" for more information.>>> dir(list)[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__delslice__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getslice__‘, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__setslice__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]>>> list1=[1,2,3]>>> list2=[4,5,6]>>> list3=list1+list2>>> list3[1, 2, 3, 4, 5, 6]>>> list3*=2>>> list3[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]>>> 3 in list3True>>> 7 in list3False>>> 7 not in list3True>>> list4["niha",[2,3],‘q‘]
Traceback (most recent call last):  File "<pyshell#10>", line 1, in <module>    list4["niha",[2,3],‘q‘]NameError: name ‘list4‘ is not defined>>> list4=["niha",[2,3],‘q‘]>>> list4[‘niha‘, [2, 3], ‘q‘]>>> 2 in list4False>>> 2 in list[1]
Traceback (most recent call last):  File "<pyshell#14>", line 1, in <module>    2 in list[1]TypeError: ‘type‘ object has no attribute ‘__getitem__‘>>> 2 in list4[1]True>>> list3.count(1)2>>> list3.index(2)1>>> list3.index(4,1,10)3>>> list3.index(4,2,10)3>>> list3.reverse()>>> list3[6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1]>>> >>> >>> list5=[3,7,4,9,1,8]>>> list5.sort()>>> list5[1, 3, 4, 7, 8, 9]>>> list5.so
Traceback (most recent call last):  File "<pyshell#27>", line 1, in <module>    list5.soAttributeError: ‘list‘ object has no attribute ‘so‘>>> list5.sort(reverse=True)>>> list5[9, 8, 7, 4, 3, 1]>>> 

python列表

标签:

原文地址:http://www.cnblogs.com/13224ACMer/p/5918392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!