list使用示例

1、list的简单使用

示例代码如下(listTest1.py):

#! /usr/bin/env python
#-*- coding:utf-8 -*-

t1 = [1,2,3]
print t1

t1.append(4)
print t1

print t1[:]
print t1[::-1]
print t1[:2:]
print t1[1:-2:]

运行效果如下:

$ python listTest1.py
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4]
[4, 3, 2, 1]
[1, 2]
[2]

该代码的github地址:

https://github.com/mike-zhang/pyExamples/blob/master/dataStructure/list_examples/listTest1.py

2、合并两个list

示例代码如下(listMerge1.py):

#! /usr/bin/env python
#-*- coding:utf-8 -*-

t1 = [1,2,3]
t2 = [3,4]

print t1 + t2
print t1,t2

print sum([t1,t2],[])
print t1,t2

t1.extend(t2)
print t1,t2

运行效果如下:

$ python listMerge1.py
[1, 2, 3, 3, 4]
[1, 2, 3] [3, 4]
[1, 2, 3, 3, 4]
[1, 2, 3] [3, 4]
[1, 2, 3, 3, 4] [3, 4]

该代码的github地址:

https://github.com/mike-zhang/pyExamples/blob/master/dataStructure/list_examples/listMerge1.py

3、list去重

移除list中的重复数据

3.1、不关注顺序(可能改变原来数据的顺序)

示例代码如下(listRemoveDups1.py):

#! /usr/bin/env python
#-*- coding : utf-8 -*-

t1 = [1, 1000, 8, 9, 11, 50, 99, 1, 45, 32, 7, 8, 10]
print t1
print list(set(t1))

运行效果如下:

$ python listRemoveDups1.py
[1, 1000, 8, 9, 11, 50, 99, 1, 45, 32, 7, 8, 10]
[32, 1, 99, 7, 1000, 9, 10, 11, 45, 8, 50]

该代码的github地址:

https://github.com/mike-zhang/pyExamples/blob/master/dataStructure/list_examples/listRemoveDups1.py

3.2、关注顺序,不改变原来的顺序

示例代码如下(listRemoveDups2.py):

#! /usr/bin/env python
#-*- coding : utf-8 -*-

def rmDups(items):
    tmpSet = set()
    for item in items :
        if not item in tmpSet :
            yield item
            tmpSet.add(item)

t1 = [1, 1000, 8, 9, 11, 50, 99, 1, 45, 32, 7, 8, 10]
print t1
print list(rmDups(t1))

运行效果如下:

$ python listRemoveDups2.py
[1, 1000, 8, 9, 11, 50, 99, 1, 45, 32, 7, 8, 10]
[1, 1000, 8, 9, 11, 50, 99, 45, 32, 7, 10]

该代码的github地址:

https://github.com/mike-zhang/pyExamples/blob/master/dataStructure/list_examples/listRemoveDups2.py

4、list排序

4.1 python基本数据类型排序

这里以int为例

示例代码如下(listSort_test1.py):

#! /usr/bin/env python
#-*- coding : utf-8 -*-

t1 = [5,6,2,3,1]

print sorted(t1)
print sorted(t1,reverse=True

运行效果如下:

$ python listSort_test1.py
[1, 2, 3, 5, 6]
[6, 5, 3, 2, 1]

该代码的github地址:

https://github.com/mike-zhang/pyExamples/blob/master/dataStructure/list_examples/listSort_test1.py

4.2 python结构数据类型排序

这里以dict为例

示例代码如下(listSort_test2.py):

#! /usr/bin/env python
#-*- coding : utf-8 -*-

rows = [
    {"id" : 1001,'name': "user1","score" : 80},
    {"id" : 1002,'name': "user8","score" : 60},
    {"id" : 1003,'name': "user7","score" : 70},
    {"id" : 1004,'name': "user6","score" : 90},
    {"id" : 1005,'name': "user3","score" : 99},
    {"id" : 1006,'name': "user5","score" : 66},
]

def show(rows):
    for item in rows :
        print item
    print("-" * 50)

show(rows)

rows_byname2 = sorted(rows, key=lambda x:x["score"])
show(rows_byname2)

运行效果如下:

$ python listSort_test2.py
{'score': 80, 'id': 1001, 'name': 'user1'}
{'score': 60, 'id': 1002, 'name': 'user8'}
{'score': 70, 'id': 1003, 'name': 'user7'}
{'score': 90, 'id': 1004, 'name': 'user6'}
{'score': 99, 'id': 1005, 'name': 'user3'}
{'score': 66, 'id': 1006, 'name': 'user5'}
--------------------------------------------------
{'score': 60, 'id': 1002, 'name': 'user8'}
{'score': 66, 'id': 1006, 'name': 'user5'}
{'score': 70, 'id': 1003, 'name': 'user7'}
{'score': 80, 'id': 1001, 'name': 'user1'}
{'score': 90, 'id': 1004, 'name': 'user6'}
{'score': 99, 'id': 1005, 'name': 'user3'}
--------------------------------------------------

该代码的github地址:

https://github.com/mike-zhang/pyExamples/blob/master/dataStructure/list_examples/listSort_test2.py