Python 小练习(排序,不换行,元组字典传参等)

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'


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值