python下 list/dictionary
2015-11-26 22:57
405 查看
一、list
实例 1:
实例 2
二、dictionary
实例 1:
-------------------------------shop_list.py---------------------------------- #!/usr/bin/python #coding=utf-8 import tab products = ['car','iphone','coffee','mac','mouse','bicyle'] prices = [25000, 4999, 35, 9688, 332, 1500] shop_list = [] salary = int(raw_input('please input your salary:')) while True: print "things have in the shop,please choose one to buy:" for p in products: print p,'\t',prices[products.index(p)] choice = raw_input('please input one item to buy:') f_choice = choice.strip() if f_choice in products: product_price_index = products.index(f_choice) product_price = prices[product_price_index] print f_choice,product_price if salary > product_price: shop_list.append(f_choice) print "added %s into your shop list" % f_choice salary = salary - product_price print "Salary left:",salary else: if salary < min(prices): print "sorry,rest of your salary cannot buy anything!" print "you have bought these things:%s" % shop_list else: print "you cannot afford this product,try other ones!"
实例 2
-------------------------------shops.txt------------------------------------- car 25000 iphone 4999 coffee 35 mac 9688 bicyle 432 -------------------------------shop2_list.py--------------------------------- #!/usr/bin/python #coding=utf-8 import tab products = [] prices = [] shop_list = [] #----------------------把产品、价格加入到系统中 f = file('shops.txt') for line in f.readlines(): new_line = line.split() products.append(new_line[0]) prices.append(int(new_line[1])) #print products #print prices salary = int(raw_input("please input your salary:")) #此脚本是让用户不停的买东西,直到木有钱为止,因此用while循环 while True: print "welcome,things you can buy as below" #----------------------打印产品、价格菜单 for p in products: p_index = products.index(p) p_price = prices[p_index] print p,'\t',p_price choice = raw_input("please input what you want to buy:") f_choice = choice.strip() if f_choice in products: print "\033[31;1myes,it is in the list\033[0m" f_index = products.index(f_choice) f_price = prices[f_index] if salary >= f_price: print "\033[34;1mCongratulations,added %s to shop list\033[0m" %f_choice shop_list.append(f_choice) salary = salary - f_price print "Now you have %d left! keeping buying" %salary else: if salary < min(prices): print "\033[31;1mYou can't buy anything! Bye!\033[0m" print "you have bought:",shop_list break else: print "\033[31;1msorry,money is not enough,try other one!\033[0m" else: print "\033[31;1m%s not in the list,try other one\033[0m" %f_choice
二、dictionary
-------------------------------contact_list.txt------------------------------ 1 zhangsan IT 18212356434 2 lisi HR 17482382344 3 wangwu QA 12384722344 4 sunliu JN 14234234235 5 tom IT 23423343523 -------------------------------contact_dic.py-------------------------------- #!/usr/bin/python #coding=utf-8 import tab contact_dic = {} contact_file='contact_list.txt' f = file(contact_file) for line in f.readlines(): name = line.split()[1] contact_dic[name] = line #print contact_dic #--------格式化输出------------------------------------------------- for n,v in contact_dic.items(): print n,'\t',v, while True: input = raw_input("please input the staff name:").strip() if len(input) == 0:continue if contact_dic.has_key(input): print "\033[31;1m%s \033[0m" %contact_dic[input] else: print "sorry,no staff name found"
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法