Python 小练习(排序,不换行,元组字典传参等)
2012-01-02 23:36
555 查看
python 实现排序算法
http://www.cnblogs.com/chineking/archive/2011/05/24/implement-sort-algorithm-with-python.html
python的内置排序
http://apps.hi.baidu.com/share/detail/19550749
python 的不换行输出
http://blog.fuqcool.com/2011/04/10/python-cancel-changeline.html
http://hi.baidu.com/netspider_2007/blog/item/50fc9b13705a21c7c2fd78f9.html
python 元组字典传参等
http://www.cnblogs.com/chineking/archive/2011/05/24/implement-sort-algorithm-with-python.html
python的内置排序
http://apps.hi.baidu.com/share/detail/19550749
python 的不换行输出
http://blog.fuqcool.com/2011/04/10/python-cancel-changeline.html
http://hi.baidu.com/netspider_2007/blog/item/50fc9b13705a21c7c2fd78f9.html
python 元组字典传参等
test_list = TestList(*(list_test)) test_list2 = TestList(1,2) test_map= TestMap(a='1',b='2') test_map2= TestMap(**(map_test))
1、定义类Ctest 至少包含如下公有成员。 int *list(存储动态数据) int Max(数据最大长度) int NUM(当前数据个数) 2、具备增加减少数据的功能。 3、编写适当的构造函数和析构函数(鼓励通过构造函数初始化数据)。 4、定义成员函数实现将list内数据用选择法由小到大排序。 5、定义类的派生类,在派生类中增加成员函数,将list中的数据倒序排列。 6、以上功能需要用主函数加以验证。
# coding=utf8 from sys import exit import os import sys class Base: '''hello class''' # 全局 just_for_test = 0 def __init__(self, max_value): self.max_value = max_value self.list_data = [] def print_data(self): '''hello print_data''' print "==========begin========" for i in range(0, len(self.list_data)): print self.list_data[i] print "==========end==========" def __del__(self): print "unconstructor" def add(self, data): if len(self.list_data) < self.max_value: self.list_data.append(data) else: print 'full' def remove(self, index): if index >= 0 and index < len(self.list_data): del self.list_data[index] else: print 'illegal index' def sort(self): self.list_data.sort() class Extern(Base): def __init__(self, max_value, introduction): Base.__init__(self, max_value) self.introduction = introduction def __del__(self): Base.__del__(self) def sort(self): self.list_data.sort(reverse=True) class TooLongException: def __init__(self, at_most_length, length): self.at_most_length = at_most_length self.length = length class TestList: def __init__(self, *args): self.arg = args def print_data(self): for data in self.arg: print data, print class TestMap: def __init__(self, **args): self.arg = args def print_data(self): for key in self.arg: print "key:%s => value:%s" % (key, self.arg[key]) for key,value in self.arg.items(): print "key:%s => value:%s" % (key, value) def main(): print Base.just_for_test print Base.__doc__ print Base.print_data.__doc__ base = Base(10) # externs Class Base # extern = Extern(20,'x') base.print_data() while(True): choice = int(raw_input('input you choice:1.add, 2.remove, 3.sort, 4.exit\t')) if choice == 1: data = raw_input('input the data:') base.add(data) elif choice == 2: index = raw_input('input the index:') base.remove(int(index)) elif choice == 3: base.sort() else: break base.print_data() list_test = [1,2] map_test = { 'huangkq' : '2008', 'Mis' : '2011', 'all' : '2001' } if __name__=="__main__": main() test_list = TestList(*(list_test)) test_list.print_data() test_map= TestMap(a='1') test_map2= TestMap(**(map_test)) test_map.print_data() test_map2.print_data() print map_test # 1 map_test_return = sorted(map_test.items(), key=lambda map_test: map_test[1]) #不改变原来的 # lambda x,y:cmp(x.a,y.a) print map_test_return # 排了序的输出 print map_test # 和 1 的 相同 if False: # 相当于多行注释 print os.getcwd() print os.getenv('JAVA_HOME') print os.listdir('./') #os.system('gvim') print os.name print os.linesep print ''' weclom here !''' try: choice = raw_input("Are you sure you want to start this paragram?yN:\t") if len(choice) > 1: raise TooLongException(1,len(choice)) except EOFError: print 'Eof' exit() except TooLongException, x: print 'Too long %d over %d' % (x.length, x.at_most_length) except: print 'Some exception occurs' finally: print 'Will excute this'
相关文章推荐
- Python排序总结:列表、字典、包含字典的列表、包含元组的列表排序
- Python练习代码 -- 元组,列表,字典
- Python中数组 链表 元组 字典实现类Sql多字段排序和动态函数实现
- python序列(列表,元组,字典)的常用排序
- Python - 列表、字典和元组的排序
- python sorted list 包含元组 字典 的多列排序
- 复习1次课(11月28日) 1.3 python的文件类型 1.4 python的变量 1.5 python的数值和字符串 1.6 元组 1.7 列表 1.8 字典 1.9 字典练习
- 【Python】列表、字典和元组的排序
- Python数据结构之列表、元组及字典
- python数据结构之列表、元组和字典用法
- Python 列表 元组 字典 集合 区别(一)
- Python学习笔记3-Python元组、数组、字典集合的操作
- 简明python教程 --C++程序员的视角(四):容器类型(字符串、元组、列表、字典)和参考
- python基础练习--对10个数进行排序
- 无限遍历,python实现在多维嵌套字典、列表、元组的JSON中获取数据
- Python进阶 03 元组/列表/字典 互转
- Python:元组、列表、字典
- Python学习笔记---元组(tuple)、列表(list)、字典(dict)
- python列表,元组,字典,集合对比
- python中列表,元组,字典知识点整理