列表、元组、字典

列表
def append(self, p_object):

    '''在列表末尾添加一个元素'''
l2 = [20,40,70,100]
l2.append(1)
l2.append(2)
l2.append(3)
print(l2)

[20, 40, 70, 100, 1, 2, 3]

def count(self, value):

    '''总计一个元素在列表中出现的次数'''
l2 = [20,40,70,100,20,70,80,20]
print(l2.count(20))
3

def extend(self, iterable):

    '''向列表里添加多个元素或另一个列表'''
#1
l1 = [2,4,7,10]
l2 = [20,40,70,100,20,70,80,20]
l2.extend(l1)
print(l2)
[20, 40, 70, 100, 20, 70, 80, 20, 2, 4, 7, 10]
#2
l2 = [20,40,70,100,20,70,80,20]
l2.extend('qwer')
print(l2)

[20, 40, 70, 100, 20, 70, 80, 20, 'q', 'w', 'e', 'r']

def index(self, value, start=None, stop=None):

      '''返回列表中某个元素第一次出现时的索引值'''
l2 = [20,40,70,100,20,70,80,20]
print(l2.index(100))

3

def insert(self, index, p_object):

      '''向列表中插入一个元素,可以指定插入位置'''
l2 = [20,40,70,100,20,70,80,20]
l2.insert(0,100)
print(l2)

[100, 20, 40, 70, 100, 20, 70, 80, 20]

def pop(self, index=None):

      '''删除列表中最后一个元素'''
l2 = [20,40,70,100,20,70,80,20]
print(l2.pop())
print(l2)
20
[20, 40, 70, 100, 20, 70, 80]

def remove(self, value):

      '''从开头开始查找删除匹配到的第一个元素'''
l2 = [20,40,70,100,20,70,80,20]
l2.remove(20)
print(l2)

[40, 70, 100, 20, 70, 80, 20]

def reverse(self):

      '''把列表倒过来'''
l2 = [20,40,70,100,20,70,80,20]
l2.reverse()
print(l2)

[20, 80, 70, 20, 100, 70, 40, 20]

def sort(self, cmp=None, key=None, reverse=False):
      '''列表排序,默认按从小到大排序'''
l2 = [20,40,70,100,20,70,80,20]
l2.sort()
print(l2)
[20, 20, 20, 40, 70, 70, 80, 100]

元组

def count(self, value):
    '''统计元组中元素出现的次数'''
tu = (1,2,3,4,5,11,22,33,44,55,66,1,2,3,4,5,6,1)
print(tu.count(1))

3

def index(self, value, start=None, stop=None):
    '''显示元组中指定元素第一次出现的索引'''
tu = (1,2,3,4,5,11,22,33,44,55,66,1,2,3,4,5,6,1)
print(tu.index(5))
4

字典

def clear(self):
    '''清除字典里所有元素'''
dic = {
1:'大唐',
2:'st',
3:'fc'
}
print(dic)
dic.clear()
print(dic)
{1: '大唐', 2: 'st', 3: 'fc'}
{}

def copy(self):

    '''复制字典'''
dic = {
1:'大唐',
2:'st',
3:'fc'
}
print(dic)
dic2 = dic.copy()
print(dic2)
{1: '大唐', 2: 'st', 3: 'fc'}
{1: '大唐', 2: 'st', 3: 'fc'}

def get(self, k, d=None):
    '''根据key得到对应的值'''
dic = {
1:'大唐',
2:'st',
3:'fc'
}
print(dic)
dic2 = dic.get(2)
print(dic2)


{1: '大唐', 2: 'st', 3: 'fc'}
st

def items(self):
    """ 所有项的列表形式 """
dic = {
1:'大唐',
2:'st',
3:'fc'
}
print(dic)
dic2 = dic.items()
print(dic2)

{1: '大唐', 2: 'st', 3: 'fc'}
dict_items([(1, '大唐'), (2, 'st'), (3, 'fc')])

def keys(self):
      """ 字典所有的key列表 """
dic = {
1:'大唐',
2:'st',
3:'fc'
}
print(dic)
dic2 = dic.keys()
print(dic2)

{1: '大唐', 2: 'st', 3: 'fc'}
dict_keys([1, 2, 3])

def pop(self, k, d=None):

      '''获取并移除字典中一对元素'''
dic = {
1:'大唐',
2:'st',
3:'fc'
}
print(dic)
dic2 = dic.pop(2)
print(dic2)
print(dic)

{1: '大唐', 2: 'st', 3: 'fc'}
st
{1: '大唐', 3: 'fc'}

def popitem(self):

      '''获取并一处字典中第一对元素'''
dic = {
0:'长安',
1:'大唐',
2:'st',
3:'fc'
}
print(dic)
dic2 = dic.popitem()
print(dic2)
print(dic)
{0: '长安', 1: '大唐', 2: 'st', 3: 'fc'}
(0, '长安')
{1: '大唐', 2: 'st', 3: 'fc'}

def setdefault(self, k, d=None):

        '''如果key不存在,则创建,如果存在,则返回已存在的值且不修改'''
dic = {
0:'长安',
1:'大唐',
2:'st',
3:'fc'
}
print(dic)
dic2 = dic.setdefault(4)
print(dic2)
print(dic)

{0: '长安', 1: '大唐', 2: 'st', 3: 'fc'}
None
{0: '长安', 1: '大唐', 2: 'st', 3: 'fc', 4: None}

def update(self, E=None, **F):

        '''更新,可以将一个字典追加到另一个字典里面'''  
dic = {
0:'长安',
1:'大唐',
2:'st',
3:'fc'
}
dic1 = {
'A':'飞燕女',
'B':'逍遥生',
'C':'玄彩娥'
}
print(dic)
dic2 = dic.update(dic1)
print(dic2)
print(dic)
{0: '长安', 1: '大唐', 2: 'st', 3: 'fc'}
None
{0: '长安', 1: '大唐', 2: 'st', 3: 'fc', 'B': '逍遥生', 'C': '玄彩娥', 'A': '飞燕女'}

def values(self):

     '''列出字典中所有值'''
dic = {
0:'长安',
1:'大唐',
2:'st',
3:'fc'
}
print(dic)
dic2 = dic.values()
print(dic2)
print(dic
{0: '长安', 1: '大唐', 2: 'st', 3: 'fc'}
dict_values(['长安', '大唐', 'st', 'fc'])
{0: '长安', 1: '大唐', 2: 'st', 3: 'fc'}

      

    
 


 


 
 


  
 
 
 
 
 

  

      
 














 
 
posted @ 2016-07-05 22:25  仙小音  阅读(141)  评论(0编辑  收藏  举报