当前位置: 移动技术网 > IT编程>脚本编程>Python > python基础知识(列表、字典、二维数组)

python基础知识(列表、字典、二维数组)

2020年07月07日  | 移动技术网IT编程  | 我要评论
记得改参数!!!(1)简述列表(list)结构的特点。List(列表)List的元素以线性方式存储,可以存放重复对象,List主要有以下两个实现类:ArrayList : 长度可变的数组,可以对元素进行随机的访问,向ArrayList中插入与删除元素的速度慢。 JDK8 中ArrayList扩容的实现是通过grow()方法里使用语句newCapacity = oldCapacity + (oldCapacity >> 1)(即1.5倍扩容)计算容量,然后调用Arrays.copyof.

记得改参数!!!


(1)简述列表(list)结构的特点。

List(列表)

List的元素以线性方式存储,可以存放重复对象,List主要有以下两个实现类:

ArrayList : 长度可变的数组,可以对元素进行随机的访问,向ArrayList中插入与删除元素的速度慢。 JDK8 中ArrayList扩容的实现是通过grow()方法里使用语句newCapacity = oldCapacity + (oldCapacity >> 1)(即1.5倍扩容)计算容量,然后调用Arrays.copyof()方法进行对原数组进行复制。

LinkedList: 采用链表数据结构,插入和删除速度快,但访问速度慢。

(2)简述字典(dict)结构的特点。

序列是以连续的整数为索引,字典是以关键字为索引,关键字是任意不可变类型,通常是字符串或数值,如果元组中只包含字符串和数字,也可以作为关键字。

列表不可以做关键字,因为列表可以用索引、切割或者append()和extend()等方法改变。

字典的结构为:用{ }来包含所有的元素如:{‘苹果’:‘apple’,‘香蕉’:‘banana’},key-value之间用 : 分割,键值对和键值对之间用 , 分隔。

  1. 编码题

(1)使用二维列表保存学生信息, 如表4-5所示

list1 = [[‘张璇’,‘17岁’,‘8年级3班’,‘成绩4. 2’],[‘李敏’,‘15岁’,’ 男性’,‘7年级2班’,‘成绩3. 4’],[‘赵四’,‘16岁’,’ 男性’,‘8年级1班’,‘成绩4.0’],[‘李艳’,‘15岁’,‘女性’,’ 8年级1班’,‘成绩3. 3’]]

output = open(‘data.xls’,‘w’,encoding=‘gbk’)

output.write(‘姓名\年龄\性别\年级\成绩\n’)

for i in range(len(list1)):

for j in range(len(list1[i])):

	output.write(str(list1[i][j]))  

	output.write('\t')  

output.write('\n')       

output.close()

效果图

(3)统计英文儿歌《twinkle twinkle little star》中使用到的单词及其出现的次数。要求恕略单词大小写的影响, 不统计标点符号的个数。在控制台上输出的结果

效果图

message=‘Twinkle, twinkle, little star,How I wonder what you are!Up above the world so high,Like a diamond in the sky.Twinkle, twinkle, little star,How I wonder what you are!When the blazing sun is gone,When he nothing shines upon,Then you show your little light,Twinkle, twinkle, all the night.Twinkle, twinkle, little star,How I wonder what you are!Then the travler in the dark Thanks you for your tiny spark;How could he see where to go, If you did not twinkle so?Twinkle, twinkle, little star,How I wonder what you are!In the dark blue sky you keep, andThrough my curtains often peep. ,For you never shut your eyes,Till the morning sun does rise.Twinkle, twinkle, little star,How I wonder what you are!As your bright and tiny sparkLights the travler in the dark,Though I know not what you are,Twinkle on, please, little star.Twinkle, twinkle, little star,How I wonder what you are!’

count={}

for character in message:

count.setdefault(character,0)

count[character]=count[character]+1

print(count)

(4)在第(3)题的基础上, 对英文儿歌中出现的单词按照词频数从大到小进行降序排列, 并在控制台上输出

本文地址:https://blog.csdn.net/R_Heng/article/details/107165822

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网