Python数据结构——序列、字典
来源:互联网 发布:程序设计用什么软件 编辑:程序博客网 时间:2024/06/05 21:22
一、定义函数countchar()按字母表顺序统计字符串中所有出现的字母的个数(允许输入大写字符,并且计数时不区分大小写)。形如:
def countchar(str): ... ... return a list if __name__ == "__main__": str = input() print(countchar(str))
- 输入格式: 字符串
- 输出格式: 列表
- 输入样例: Hello, World!
- 输出样例: [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 3, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0]
源代码:
def countchar(str): #创建列表ans,用来存放答案 ans=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] str=str.lower() #将输入字符串转化为小写字母,统一处理 #遍历字符串,分别处理字符串中每一个元素 for i in str: if(ord('a')<=ord(i)<=ord('z')): #筛选出a-z,不处理其他元素 ans[ord(i)-ord('a')] = ans[ord(i)-ord('a')]+1 return ans if __name__ == "__main__": str = input() #输入字符串 print(countchar(str))
运行结果:
二、编写一个函数:oddTuples(aTup),返回一个元组,其中元素是原元组的偶数位置的元素。例如:
>>>aTup=('I', 'am', 'a', 'test', 'tuple')>>> oddTuples(aTup)('I', 'a', 'tuple')
源代码:
def oddTuples(aTup): #元组一旦创建不可修改,所以将元素暂存在列表中 ans=[] #存偶数元素的列表 for i in range(0,len(aTup),2): ans.append(aTup[i]) return tuple(and) #将列表转换为元组并返回aTup=('I','am','a','test','tuple')print(oddTuples(aTup))
运行结果:
三、假设已经执行了下列语句序列:
animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']} #值为列表animals['d'] = ['donkey']animals['d'].append('dog')animals['d'].append('dingo')
编写过程howMany(Dict),返回结果为字典中值的总个数(包含每个列表中的元素个数)。如:
>>> print howMany(animals)6
又如:
>>>print(howMany({'a': [8, 11, 4, 18], 'H': [19, 19, 16], 'L': [12], 'O': [12, 2, 14, 2], 'p': [7, 15, 7, 15, 10], 's': [], 't': [], 'y': [11, 19, 12, 0]}) )21
源代码:
def howMany(Dict):#基本思路是:根据k值,遍历字典的每一个value值。字典的每个value都是一个列表,使用len()方法,累加列表长度 k=Dict.keys() #temp_ans=[] ans=0 for i in k: ans+=len(Dict[i]) return(ans)#示例一的实例animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']} #值为列表animals['d'] = ['donkey']animals['d'].append('dog')animals['d'].append('dingo')#print(howMany(animals))#示例二的实例test1 = {'a': [8, 11, 4, 18], 'H': [19, 19, 16], 'L': [12], 'O': [12, 2, 14, 2], \ 'p': [7, 15, 7, 15, 10], 's': [], 't': [], 'y': [11, 19, 12, 0]}print(howMany(test1))
运行结果:
(1)示例一:
(2)示例二:
四、假定已经执行了以下语句序列:
animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']} #值都是列表animals['d'] = ['donkey']animals['d'].append('dog')animals['d'].append('dingo')
编写函数:bigest(Dict),返回其值包含元素个数最多的键值。
例如:
>>> biggest(animals)'d'
又如:
>>>biggest({'a': [3, 3, 18], 'c': [3, 15, 12, 10, 0], 'b': [10, 19, 14, 5, 16, 20, 11, 6], 'd': [5, 16, 8, 16, 6, 1]}) 'b'
源代码:
#思路跟上题类似def biggest(Dict): max=-1 max_key='' k = Dict.keys() for i in k: if(len(Dict[i])>max): max=len(Dict[i]) max_key=i return max_keyanimals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']} #值都是列表animals['d'] = ['donkey']animals['d'].append('dog')animals['d'].append('dingo')print(biggest(animals))test={'a': [3, 3, 18], 'c': [3, 15, 12, 10, 0], 'b': [10, 19, 14, 5, 16, 20, 11, 6], 'd': [5, 16, 8, 16, 6, 1]}print(biggest(test))
运行结果:
阅读全文
0 0
- Python数据结构——序列、字典
- python 数据结构 元组、列表、字典、序列
- 4. Python中的序列——字典
- python基础知识——内置数据结构(字典)
- Python数据结构之——字典
- Python dict dictionaries Python 数据结构——字典
- Python dict dictionaries Python 数据结构——字典
- Python中数据结构 列表 元组 字典 序列
- python-数据结构(列表,元组,字典,序列,引用)
- Python中数据结构 列表 元组 字典 序列
- Python数据结构:序列——字符串
- Python数据结构:映射(map)——字典
- Python数据结构-字典
- Python数据结构--字典
- python数据结构之字典
- python数据结构之字典
- Python内存数据结构--字典
- python数据结构之字典
- JS常见算法
- 【技术贴收集】微信视频播放全屏问题
- 树状数组模板
- 【linux】复制文件部分内容到另外一个文件
- 矩阵最小二乘与Tikhonov正则化
- Python数据结构——序列、字典
- java 反射的原理
- C#计算一段程序运行时间的三种方法
- XML文档相关介绍
- smaps文件的解释
- Class装载系统
- 状压DP 分裂
- POJ3076-Sudoku
- jmeter 关联