|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
第5道问题里出现一个
>>> list1.sort()
>>> list1
[1, 2, 3, 7, 8, 9]
提问:
sort() 是什么意思啊?
还有这节课里讲到 pop()
为什么括号里面只能打数字,不能打文字?
>>> mumber = ['albb','baidu','tenxun','wanyi','xinlang']
>>> mumber.pop()
'xinlang'
>>> mumber.pop('baidu')
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
mumber.pop('baidu')
TypeError: 'str' object cannot be interpreted as an integer
描述
pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
语法
pop()方法语法:
list.pop(obj=list[-1])
参数
obj -- 可选参数,要移除列表元素的对象。
返回值
该方法返回从列表中移除的元素对象。
| pop(...)
| L.pop([index]) -> item -- remove and return item at index (default last).
| Raises IndexError if list is empty or index is out of range.
描述
sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。
语法
sort()方法语法:
list.sort([func])
参数
func -- 可选参数, 如果指定了该参数会使用该参数的方法进行排序。
返回值
该方法没有返回值,但是会对列表的对象进行排序。
实例
以下实例展示了 sort()函数的使用方法:
#!/usr/bin/python
aList = [123, 'xyz', 'zara', 'abc', 'xyz'];
aList.sort();
print "List : ", aList;
以上实例输出结果如下:
List : [123, 'abc', 'xyz', 'xyz', 'zara']
sort(...)
| L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
|
|