- 博客(10)
- 收藏
- 关注
原创 寻找多个字典中的公共键
首先想到的做法,用for循环s1 = {k:randint(1,4) for k in sample('abcxyz',randint(4,6))}s2 = {k:randint(1,4) for k in sample('abcxyz',randint(4,6))}s3 = {k:randint(1,4) for k in sample('abcxyz',randint(4,6))}#s1 =
2016-12-14 15:41:20 367
原创 根据字典中值的大小,对字典的项排序
对字典排序比如学生成绩 {‘Lilei’: 63, ‘wang’: 78, ‘hanmeimei’: 90} 根据学生成绩计算排名一般情况首选sorted()函数或者sort方法第一种方法:用zip将字典转化成元组d = {k:randint(60,100) for k in ('abcxyz') }#{'a':79, 'c':78, 'b':93, 'y':95, 'x':77, 'z':9
2016-12-13 14:00:18 9737 2
原创 统计序列中元素出现的频度
第一种方法直接用for循环form random import randintdata = [randint(0,20) for _ in xrange(40)]#计算词频,预期产生如下字典#{1:2, 5:3, 6:0}#首先初始化一个字典,用data里的元素做key,value初始化为0c = dict.fromkeys(data,0)for x in data: c[x] +=
2016-12-11 17:58:39 336
原创 给元组的每个元素命名,提高程序可读性
欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦:Markdown和扩展Markdown简洁的语法代码块高亮图片链接和图片上传LaTex数学公式UML序列图和流程图离线写博客导入导出Markdown文件丰富的快捷键快捷键加粗 Ctrl + B 斜体 Ctrl + I 引用 Ctrl
2016-12-11 13:24:38 342
原创 如何在列表、字典、集合中根据条件筛选数据
在Python中筛选数据,除了可以使用 for 循环外,还可以使用函数式编程1,列表找出列表中大于零的数据第一种方法是使用 filter(function or None, sqeuence) 函数第二种方法是使用列表解析这两种方法都要比直接用 for 循环高效。并且,列表解析速度最快。2,字典找出成绩大于或等于90分的学生字典也可以使用字典
2016-12-10 13:44:24 1614
原创 cc150-8.1
题目原文:Write a method to generate the nth Fibonacci number.译文:写一个函数来产生第n个斐波那契数。解答
2014-09-29 21:58:30 929
原创 cc150,1.5
题目原文:Write a method to replace all spaces in a string with ‘%20’.译文:写一个函数,把字符串中所有的空格替换为%20 。
2014-09-15 21:38:44 641
原创 cc150,1.4
题目原文:Write a method to decide if two strings are anagrams or not.译文:写一个函数判断两个字符串是否是变位词。
2014-09-15 21:30:20 452
原创 cc-150,1.3
def string(x): y = [] for i in x: if i not in y: y.append(i) return "".join(y)2014-9-15
2014-09-15 21:25:59 386
原创 cc150-1.1
def string(x): y = [] for i in x: count = x.count(i) if count > 1 and i not in y: y.append(i) for ii in y: print ii + ' ' + 'is not the only.'2014-09
2014-09-14 11:51:11 540
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人