2013年4月18日
摘要: # 实现的功能如下:提示用户输入一个句子(英文,并且按enter键结尾),# 该程序将句子中的字母按ASCII码编码顺序重新排列,排列后的单词的长度要与原始句子中的长度相同,# 例如: 输入:who is your daddy,输出:add dh ioor suwyy s = input('please input a string:\n')# 先排序,去空格tmp_s = list(filter(lambda c : not c.isspace(), sorted(s, reverse = True)))print(tmp_s)# 再在原始串空格位置上插入空格,其他位置为排序 阅读全文
posted @ 2013-04-18 19:11 101010 阅读(413) 评论(0) 推荐(0) 编辑
摘要: Python offers two different primitive operations based on regular expressions:re.match() checks for a match only at the beginning of the string,while re.search() checks for a match anywhere in the string (this is what Perl does by default).For example:>>> re.match('a','abc') 阅读全文
posted @ 2013-04-18 17:23 101010 阅读(279) 评论(0) 推荐(0) 编辑
摘要: 直接上代码:片段1:>>> def func(x): print(','.join(str(i) for i in range(1,x+1))) >>> func(5)1,2,3,4,5>>> func(10)1,2,3,4,5,6,7,8,9,10片段2:>>> def func(x): for i in range(1,x+1): print(','.join(str([j,'password'][j==i]) for j in range(1,x+1))) >&g 阅读全文
posted @ 2013-04-18 15:08 101010 阅读(226) 评论(0) 推荐(0) 编辑
摘要: 2个list:a =[1,2,3,4,5]b =['a','b','c','d','e']形成一个字典:c = {}构造规则:a中的元素用作key,b中的元素用作value,字典c={1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}。>>> a = [1,2,3,4,5]>>> b = ['a','b','c','d 阅读全文
posted @ 2013-04-18 11:28 101010 阅读(183) 评论(0) 推荐(0) 编辑
摘要: 初步想法:>>> a = [1,2,4,1,2,3,5,2,6,8,7,8,8]>>> a[1, 2, 4, 1, 2, 3, 5, 2, 6, 8, 7, 8, 8]>>> for i in a: print(i,a.count(i)) 1 22 34 11 22 33 15 12 36 18 37 18 38 3但是,结果出现了重复的值。有以下三种解决方案:1.使用集合Set>>> for i in set(a): print(i,a.count(i)) 1 22 33 14 15 16 17 18 32.使用字典Di 阅读全文
posted @ 2013-04-18 11:18 101010 阅读(11232) 评论(0) 推荐(0) 编辑
摘要: >>> l = [1,2,3,1,2,4,5]1.列表推导式:>>> [x for x in l if x > min(l)][2, 3, 2, 4, 5]2.filter()函数:>>> list(filter(lambda x: x>min(l), l))[2, 3, 2, 4, 5] 阅读全文
posted @ 2013-04-18 10:33 101010 阅读(863) 评论(0) 推荐(0) 编辑