标签:val 删除 拷贝 ice import mes input one 复制
1、列表
列表定义:
空列表:names = []
非空:>>> names = ["zhangyang","guyun","xiangpeng","xuliangyun"]
列表取值:
names[0]
"zhangyang"
names[0:2]#切片取法:顾头不顾尾;
"zhangyang","guyun"
names[0::2]
"zhangyang","xiangpeng"
names[0:-1]
"zhangyang","guyun",xiangpeng"
列表添加:
names.append("xiaoxiao")
names.insert(1,"kaka")
names.extend([1,2,3])
列表修改:
names[0] = ‘bibi‘
列表删除:
names.remove("chengronghua")
del names[0]
names.pop()
names.pop(2)
列表排序
names.sort()
names.reverse()
列表索引:
names.index("xiedi")
列表统计:
names.count("xiedi")
列表清空:
names.clear()
列表复制:
import copy
>>> x = ["a","b","z",["d","e","f"]]
>>> y = copy.copy(x)
>>> z = copy.deepcopy(x)
浅拷贝只会拷贝第一层,深拷贝会完全拷贝.
2、元组
元组和列表很象,一个区别:不能增、删、改,所以把元组又叫只读列表(它只有2个方法,一个count,一个index)
3、购物车例子:
product_list = [
(‘iphone‘,3000),
(‘Mac pic‘,1000),
(‘Bike‘,200),
(‘book‘,100)
]
shopping_list = []
salary = input("enter your salary:")
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print (index,item)
user_choice = input("选择要买嘛>>>")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice<len(product_list) and user_choice>=0:
p_item = product_list[user_choice]
if p_item[1]<=salary:
shopping_list.append(p_item)
salary-=p_item[1]
print("Added %s into shopping card!your current balance is \033[31;1m%s\033[0m" %(p_item,salary))
else:
print("\033[32;1m你的余额只剩:%s\033[0m" % salary)
else:
print("输入的商品编号不存在,请重新输入!")
elif user_choice == ‘q‘:
print(‘exit....‘)
for p in shopping_list:
print (p)
exit("购物结束,你还剩余:%s" % salary)
else:
print(‘invalid option!‘)
标签:val 删除 拷贝 ice import mes input one 复制
原文地址:http://www.cnblogs.com/wulafuer/p/7667237.html