python_5(list_列表)

#list(列表)
----------------list_create.py----------------------
#!/usr/bin/python
# -*- coding: UTF-8 -*-

list = ["apple", "banana", "grape", "orange"]
print list
print list[2]
list.append("watermelon")
list.insert(1, "grapefruit")
print list
list.remove("grape")
print list
#list.remove("a")
print list.pop()
print list

list = ["apple", "banana", "grape", "orange"]
print list[-2]
print list[1:3]
print list[-3:-1]
list = [["apple", "banana"],["grape", "orange"],["watermelon"],["grapefruit"]]
for i in range(len(list)):
    print "list[%d] :" % i, "" ,
    for j in range(len(list[i])):
        print list[i][j], "" ,
    print
----------------list_use.py----------------------
#!/usr/bin/python
# -*- coding: UTF-8 -*-

list = ["grape", "grape"]
list2 = ["apple", list, "orange"]             
print list
print list2

list3=[i for i in list1 if i not in list2]

----------------list_index_sort.py----------------------
#!/usr/bin/python
# -*- coding: UTF-8 -*-

list = ["apple", "grape", "grape", "orange"]
list.remove("grape")
print list

list = ["apple", "banana", "grape", "orange"]
print list.index("grape")
print list.index("orange")
print "orange" in list

list1 = ["apple", "banana"]
list2 = ["grape", "orange"]
list1.extend(list2)
print list1
list3 = ["watermelon"]
list1 = list1 + list3
print list1
list1 += ["grapefruit"]
print list1
list1 = ["apple", "banana"] * 2
print list1

#使用列表的sort方法排序
list = ["banana", "apple", "orange", "grape"]
list.sort()
print "Sorted list:", list
list.reverse()
print "Reversed list:", list

#使用函数sorted排序,返回一个新的列表
list = ["banana", "apple", "orange", "grape"]
for li in sorted(set(list)):
    print li, "" ,

-------------list_stack_sep.py(堆栈、队列)-----------------
#!/usr/bin/python
# -*- coding: UTF-8 -*-

#堆栈的实现
list = ["apple", "banana", "grape"]
list.append("orange")
print list
print "弹出的元素:", list.pop()
print list

#队列的实现
list = ["apple", "banana", "grape"]
list.append("orange")
print list
print "弹出的元素:", list.pop(0)
print list
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值