Python—数据结构

Python有四种数据结构,分别是:列表,字典,元组,集合

列表(list)

1.列表中的每一个元素都是可变的

2.列表中的元素是有序的,也就是说每一个元素都有一个位置

3.列表可以容纳Python中的任何对象

列表的增删查改

增加

fruit = ['pineapple','pear']
fruit.insert(1,'grape')   #在指定位置加入元素,并且是在指定位置之前插入,此处是在‘pear’前插入
fruit.append('apple')     #在末尾加元素
fruit[0:0] = 'orange'     #①
fruit[0:0] = ['orange']   #②
#① ②两种都是用列表的切片性质增加元素,但效果不一样。①是迭代增加元素,②是整体增加元素
print(fruit)

输出结果:
['orange', 'o', 'r', 'a', 'n', 'g', 'e', 'pineapple', 'grape', 'pear', 'apple']

删除

#第一种
fruit = ['pineapple','pear','grape']
fruit.remove('pear') #指定删除某个元素
print(fruit)
#第二种
fruit = ['pineapple','pear','grape']
del fruit[0:1] #根据索引范围删除多个或一个
print(fruit)

修改

fruit[0] = 'Grapefruit'
print(fruit)

字典(dict)

1.字典中数据必须是以键值对的形式出现的

2.逻辑上讲,键是不能重复的,而值可以重复

3.字典中的键(key)是不可变的,也就是无法修改的;而值(value)是可变的,可修改,可以是任何对象

#相同的键只出现一次
a = {'key': 123,'key':123}
print(a)

#输出结果:
{'key': 123}

字典的增删查改

增加

#第一种
test= {'BIDU':'Baidu','SINA':'Sina'}
test['YOKU'] = 'Youku'  #以键对应值的方式增加元素
print(test)

#输出结果:
{'BIDU': 'Baidu', 'SINA': 'Sina', 'YOKU': 'Youku'}
#第二种
test.update({'FB':'Facebook','TSLA':'Tesla'})  #增加多个元素
print(test)

#输出结果:
{'BIDU': 'Baidu', 'SINA': 'Sina', 'YOKU': 'Youku', 'FB': 'Facebook', 'TSLA': 'Tesla'}

删除

del test['FB']
print(test)

#输出结果:
{'BIDU': 'Baidu', 'SINA': 'Sina', 'YOKU': 'Youku', 'TSLA': 'Tesla'}

查找

print(test['TSLA'])

#输出结果:
Tesla

因为字典是以键值对的形式出现,那么像字符串那样分片的查找是会出现错误的

test[1:4]   #Wrong

元组(tuple)

元组可以理解成一个稳固版的列表,因为元组是不可修改的,因此在列表中存在的方法均不可以使用在元组上,但是元组是可以被查看索引的,方式和列表一样

test = ('a','b','c','d','e','f','g')
print(test[0])

集合(set)

集合不能被切片也不能被索引

test = {1,2,3,4}
test.add(5)   #添加元素
test.discard(5)  #删除
print(test)

 

数据结构的一些技巧

排序

num_list = [6,2,7,4,1,3,5]
print(sorted(num_list))
print(sorted(num_list,reverse=True)) #降序

Python sort和sotred的区别

zip

str = ['b','f','d','a']
num = [2,6,4,1]
for a,b in zip(num,str):
	print(b,'is',a)

#输出结果:
b is 2
f is 6
d is 4
a is 1

列表推导式

#普通写法
a = []
for i in range(1,11):
	a.append(i)

#推导式写法
b = [i for i in range(1,11)]
print(b)

  列表推导式可以看成两部分。红色虚线后面的是我们熟系的for循环表达式,而虚线前面的可以认为是我们想要放在列表中的元素。在上面的例子里,放在列表的元素即是后面循环的元素本身

时间对比:

import time
a = []
t0 = time.perf_counter()
for i in range(1,20000):
	a.append(i)
print(time.perf_counter() - t0, "seconds process time")
t0 = time.perf_counter()
b = [i for i in range(1,20000)]
print(time.perf_counter() - t0, "seconds process time")

#输出结果:
0.0028939940000000004 seconds process time
0.000966986000000003 seconds process time

Python time模块

例子

a = [i**2 for i in range(1,10)]
print(a)
c = [j+1 for j in range(1,10)]
print(c)
k = [n for n in range(1,10) if n % 2 == 0]
print(k)
z = [letter.lower() for letter in "ABCDEFGHIJKLMN"]
print(z)

#输出结果:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 6, 8]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']

(附加)字典推导式例子

注意:字典是以键值对出现

d = {i:i+1 for i in range(4)}
print(d)
g = {i:j for i,j in zip(range(1,6),'abcdef')}
print(g)
g = {i:j.upper() for i,j in zip(range(1,6),'abcde')}
print(g)

#输出结果:
{0: 1, 1: 2, 2: 3, 3: 4}
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
{1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}

 

循环列表时获取元素索引

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

letters = ['a','b','c','d','e','f','g']
for num,letter in enumerate(letters):
	print(letter,"is",num+1)

 

综合项目

字符串分割 split()

lyric = 'The night begin to shine, the night brgin to shine'
words = lyric.split()
print(words)

#输出结果:
['The', 'night', 'begin', 'to', 'shine,', 'the', 'night', 'brgin', 'to', 'shine']

词频统计

统计文本瓦尔登湖文本

地址:https://pan.baidu.com/s/1XNWD-iGE0uM_jr1yT3XiXg

初期版:

path = 'C:/Users/ASUS/Desktop/Walden.txt'

with open(path,'r',encoding = 'UTF-8') as text:
	words = text.read().split()
	print(words)
	for word in words:
		print("{}-{} times".format(word,words.count(word)))

初期版存在的问题:

1.带标点符号的单词被单独统计次数

2.有些单词不止一次被输出

3.开头大写的单词需要转换成小写并统计

修改版:

import string
path = 'C:/Users/ASUS/Desktop/Walden.txt'

with open(path,'r',encoding = 'UTF-8') as text:
	words = [raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]
	words_set = set(words)
	count_dic = {word:words.count(word) for word in words_set}
	for word in sorted(count_dic,key = lambda x: count_dic[x],reverse = True):
		print("{}--{} times".format(word,count_dic[word]))

1.引入了string模块,string.punctuation中包含了所有的标点符号

2.将首字母大写单词转成小写

3.将列表转换成集合,去重

4.创建一个以单词为键,出现次数为值的字典,并以降序排序

5.用到了lambda表达式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值