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

Python里list的那些method学习笔记

时间:2015-07-10 15:15:46      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:python   methods   list   

__author__ = 'hualong_zhang'
# -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


init_list_1 = [1, 4, 9, 'cat', 'dog', 'dog', 'bird', ['fish']]
init_list_2 = [1, 4, 9]
print 'the origin list 1 and 2:\n', init_list_1, '\n', init_list_2

init_list_1.append(16)  # 尾加一个元素
print  init_list_1

print init_list_1.count('dog')  # 返回某元素出现个数

init_list_1.extend(init_list_2)  # 尾加一个list
print init_list_1

print init_list_1.index('cat')  # 返回某元素位置

init_list_1.insert(3, 'bird')  # 插入
print init_list_1

for i in range(4):
    init_list_1.pop()  # 从后弹出
print init_list_1


init_list_1.remove('dog')  # 移除
init_list_1.remove('dog')
print init_list_1

init_list_1.reverse()  # 可能是归类
print init_list_1

init_list_1.sort()  # 元素排序
print init_list_1

#下面这些__xxx__形式的,是所谓Python __Underscore__ Methods
print init_list_1.__add__(init_list_2)  # 连接起来,但不赋给左值
print init_list_1.__class__  # 类所有的方法,获取类型

print init_list_1.__contains__(init_list_2)  # 是否包含
print init_list_1

print init_list_1.__eq__(init_list_2)  # 相当于==
print init_list_1.__ge__(init_list_2)  # 相当于>=
print init_list_1.__ne__(init_list_2)  # 相当于!=
print init_list_1.__lt__(init_list_2)  # 相当于<
print init_list_1.__le__(init_list_2)  # 相当于<=
print init_list_1.__gt__(init_list_2)  # 相当于>
print init_list_1.__mul__(2)  # 相当于*,就是连续几个init_list_1
print init_list_1.__rmul__(2)  # 与mul效果一样,他说x.__rmul__(other) <==> other * x ,Only called if other.__mul__ is not defined or other.__mul__(x) returns NotImplemented.

init_list_1.__iadd__(init_list_2)  # x.__iadd__(other) <==> x += other,说明以i开头这些会作用于自身
print init_list_1

init_list_1.__imul__(2)  # 自身变为原来自身的重复
print init_list_1

print init_list_1.__iter__()  # 我猜是返回一个自身迭代器
print init_list_1.__sizeof__()  # 返回尺寸
print init_list_1.__len__()  # 返回元素个数
print init_list_1.__reversed__()  # Should return an iterator suitable for iterating over all the items contained in the instance in reverse order.
print init_list_1.__getitem__(2)  # 获取第几个元素
print init_list_1.__getslice__(2, 5)  # 获取i到j区间元素

init_list_1.__delitem__(2)  # 删除元素
print init_list_1

init_list_1.__delslice__(0, 2)  # 删除切片
print init_list_1

init_list_1.__setitem__(2, 'dog')  # 设置第几号元素
print init_list_1

init_list_1.__setslice__(2, 6, 'human')  # 把i到j替换为某个序列,human在里头会变成...h,u,m,a,n...
print init_list_1

print init_list_1.__str__()  # 这是object的方法,变为str
print init_list_1.__repr__()  # 他是这样说的(然后我这里直接输出了init_list_1本身) x.__repr__() <==> repr(x) <==> `x`
# Should return a string representation of the class or instance with as much information as possible, preferably something that can be passed to eval to recreate the object. Return value must be a string.
# If not present, returns something like '<class __main__.Class at 0x2e6d50>' for classes and '<__main__.Class instance at 0xbfb70>' for instances.

init_list_1.__new__(type(init_list_2))  # 大概是在参数的位置放一个list的子类型,然后类型就转过去了
print init_list_1

print init_list_1.__hash__  # 输出时none,不是哈希函数吗,暂时不理解
 
 
 
<span style="font-size:24px;color:#000099;"><strong>运行出来是这样</strong></span>
<p>D:\Python27\python.exe C:/Users/hualong_zhang/PycharmProjects/method_in_list_and_tuple/method_in_list_and_tuple.py
the origin list 1 and 2:
[1, 4, 9, 'cat', 'dog', 'dog', 'bird', ['fish']] 
[1, 4, 9]
[1, 4, 9, 'cat', 'dog', 'dog', 'bird', ['fish'], 16]
2
[1, 4, 9, 'cat', 'dog', 'dog', 'bird', ['fish'], 16, 1, 4, 9]
3
[1, 4, 9, 'bird', 'cat', 'dog', 'dog', 'bird', ['fish'], 16, 1, 4, 9]
[1, 4, 9, 'bird', 'cat', 'dog', 'dog', 'bird', ['fish']]
[1, 4, 9, 'bird', 'cat', 'bird', ['fish']]
[['fish'], 'bird', 'cat', 'bird', 9, 4, 1]
[1, 4, 9, ['fish'], 'bird', 'bird', 'cat']
[1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]
<type 'list'>
False
[1, 4, 9, ['fish'], 'bird', 'bird', 'cat']
False
True
True
False
False
True
[1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9, ['fish'], 'bird', 'bird', 'cat']
[1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9, ['fish'], 'bird', 'bird', 'cat']
[1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]
[1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]
<listiterator object at 0x019B79B0>
132
20
<listreverseiterator object at 0x019B79B0>
9
[9, ['fish'], 'bird']
[1, 4, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]
[['fish'], 'bird', 'bird', 'cat', 1, 4, 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]
[['fish'], 'bird', 'dog', 'cat', 1, 4, 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]
[['fish'], 'bird', 'h', 'u', 'm', 'a', 'n', 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]
[['fish'], 'bird', 'h', 'u', 'm', 'a', 'n', 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]
[['fish'], 'bird', 'h', 'u', 'm', 'a', 'n', 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]
[['fish'], 'bird', 'h', 'u', 'm', 'a', 'n', 9, 1, 4, 9, ['fish'], 'bird', 'bird', 'cat', 1, 4, 9]
None</p><p>Process finished with exit code 0
</p>

版权声明:本文为博主原创文章,未经博主允许不得转载。

Python里list的那些method学习笔记

标签:python   methods   list   

原文地址:http://blog.csdn.net/xiaopangxia/article/details/46830215

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