概念 双核苷酸由任意2个碱基组成 测试1 dna = "AATGATGAACGAC" #一一列举 dinucleotides = ['AA','AT','AG','AC', 'TA','TT','TG','TC', 'GA','GT','GG','GC', 'CA','CT','CG','CT'] all_counts = {} for dinucleotide in dinucleotides: count = dna.count(dinucleotide) if count >…
Counter类:计算序列中出现次数最多的元素 from collections import Counter c = Counter('abcdefaddffccef') print('完整的Counter对象:', c) a_times = c['a'] print('元素a出现的次数:', a_times) c_most = c.most_common(3) print('出现次数最多的三个元素:', c_most) times_dict = c.values() print('各元素出现…
如何统计序列中元素的频度 问题举例 如何找出随机序列[1, 5, 6, 5, 3, 2, 1, 0, 6, 1, 6]中出现频度最高的3个元素? 如何统计某篇英文文章中词频最高的5个单词? 将序列转换成字典(元素:频度),根据字典的值进行排序 列表 from random import randint list1 = [randint(0, 10) for _ in range(30)] print(list1) dict1 = dict.fromkeys(list1, 0) for item…
摘要:  使用Python在给定整数序列中找到和为100的所有数字组合.可以学习贪婪算法及递归技巧. 难度:  初级 问题 给定一个整数序列,要求将这些整数的和尽可能拼成 100. 比如 [17, 17, 4, 20, 1, 20, 17, 6, 18, 17, 12, 11, 10, 7, 19, 6, 16, 5, 6, 21, 22, 21, 10, 1, 10, 12, 5, 10, 6, 18] , 其中一个解是 [ [17, 17, 4, 20, 1, 17, 6, 18] [20,…
问题:提取出序列中的值或者根据某些标准对序列做删减 解决方案:列表推导式.生成器表达式.使用内建的filter()函数 1.列表推导式方法:存在一个潜在的缺点,如果输入数据非常大可能会产生一个庞大的结果,考虑到该问题,建议选择生成器表达式 # Examples of different ways to filter data mylist = [1, 4, -5, 10, -7, 2, 3, -1] print('mylist=',mylist) # 使用列表推导式 pos = [n for n…
问题:找出一个元素序列中出现次数最多的元素是什么 解决方案:collections模块中的Counter类正是为此类问题所设计的.它的一个非常方便的most_common()方法直接告诉你答案. # Determine the most common words in a list words = [ 'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 'the', 'eyes', 'the', 'eyes', 'the', '…
问题:从序列中移除重复的元素,但仍然保持剩下的元素顺序不变 解决方案: 1.如果序列中的值时可哈希(hashable)的,可以通过使用集合和生成器解决.…
enumerate 函数用于遍历序列中的元素以及它们的下标 for i,v in enumerate(['tic','tac','toe']): print i,v #0 tic #1 tac #2 toe for i,j in enumerate(('a','b','c')): print i,j #0 a #1 b #2 c for i,j in enumerate({'a':1,'b':2}): print i,j #0 a #1 b 遍历字典的key和value knights={'ga…
list.count(obj)  统计某个元素在列表中出现的次数例子: aList = [123, 'xyz', 'zara', 'abc', 123]; print "Count for 123 : ", aList.count(123); print "Count for zara : ", aList.count('zara'); 以上实例输出结果如下: Count for 123 : 2 Count for zara : 1…
enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')): print i,j 0 a1 b2 c>>> for i,j in enumerate([1,2,3]): print i,j 0 11 22 3>>> for i,j in enumerate({'a':1,'b':2}):    #注意字典,只返回KEY值!! print i,j 0 a1 b >&g…