四种数据结构:

列表list = [val1,val2,val3,val4]
字典dict = {key1:val1,key2:val2}
元组tuple = (val2,val2,val3,val4)
集合set = {val1,val2,val3,val4}

 一。列表

列表可以装入Python中所有的对象,例子

all_in_list = [
  1, #整数
  1.0 #浮点数
  'a worf' #字符串
  print(), #函数
  True, #布尔值
  [1,2] #列表中套列表
  (1,2),#元组
  {'key':'value'}#字典
]
例子2列表的增删改查
fruit = ['pineapple','pear']
fruit.insert(1,'grape')
print(fruit)
例子3位置插入
fruit[0:0] = ['Orange']
print(fruit)
例子3删除列表中元素的方法是remove():
fruit = ['pinapple','pear','grape']
fruit.remove('grape')
print(fruit)
例子4替换修改其中的元素
fruit[0] = 'Grapefruit'
删除的另外一种方法del
del fruit[0:2]
print(fruit)
 
二。字典(Dictionary)
例子
NASDAQ_code = {
  'BIDU':'Baidu',
  'SINA':'Sina',
  'YOKU':'Youku',
}

字典的增删改查

添加

NASDAQ_code = {'BIDU':'Baidu','SINA':'Sina'}
NASDAQ_code['YOKU'] = 'Youku'
print(NASDAQ_code)
列表中用来添加多个元素的方法extend(),在字典中也有对应的添加多个元素的方法update():
NASDAQ_code.update({'FB':'Facebook','TSLA':'Tesla'})

删除字典中的元素则使用del方法:

del NASDAQ_code['FB']

注意字典是不可以进行切片的

三。元组Tuple  稳固版的列表

letters = ('a','b','c','d','e','f','g')

letters[0]

四。集合Set

可以通过集合去判断数据的从属关系,有时还可以通过集合吧数据结构中重复的元素减掉。
注意集合不能被切片也不能被索引,除了做集合运算之外,集合元素可以被添加还有删除:
a_set = {1,2,3,4}
a_set.add(5)
a_set.discard(5)
五。数据结构的一些技巧
正序排列
num_list = [6,2,7,4,1,3,5]
print(sorted(num_list))
逆序排列
sorted(num_list,reverse=True)
同时需要两个列表排序,可以用Zip函数
for a,b in zip(num,str):
  print(b,'is',a)
推导式
将10个元素要装进列表中,普通写法
a = []
for i in range(1,11):
  a.append(i)
列表解析方式
b = [i for i in range(1,11)]
 列表解析式不仅非常方便,并且在执行效率上要远远胜过前者
import time
a = []
t0 time.clock()
for i in range(1,20000):
  a.append(i)
print("time.clock() - t0,seconds process time")
 
t0 = time.clock()
b = [i for i in range(1,20000)]
print("time.clock() - t0,seconds process time")
 
得到结果
8.999999999998592e-06 seconds process time
0.0012320000000000005 seconds process time
a = [i**2 for i in range(1,10)]
c = [j+1 for j in range(1,10)]
k = [n for n in range(1,10) if n %2 == 0]
z = [letter.lower() for letter in 'ABCDEFGHIGKLMN' ]
字典推导式,满足键 - 值 两个条件才能达成:
d = {i:i+1 for i in range(4)}
g = {i:j for i,j in zip(range(1,6),'abcde')}
g = {i:j.upper() for i,j in zip(range(1,6),'abcde') }
 
例子
letters = ['a','b','c','d','e','f','g']
for num,letter in enumerate(letters):
  print(letter,'is',num+1)
 
结果为a=1,b=2...

 

六。综合项目

使用split方法将字符串中的每个单词分开,得到独立的单词:

lyric = 'The night begin to shine,the night begin to shine'

words = lyric.split()

结果['The','night','begin','to','shine']

词频统计,count方法来统计重复出现的单词:

path = '/Users/Hou/.../Walden.txt'
with open(path,'r') as text:
  words = text.read().split()
  print(words)
  for word in words:
    print('{}-{} times'.format(word,words.count(word)))

结果结论

1,有些带标点符号单词被单独统计次数

2,有些单词不止一次展示了出现的次数

3,,由于Python对大小写敏感,开头大写的单词被单独统计了

调整统计方法,对单词做些预处理:

import string
path = '/Userss/Hou/.../Walden.txt'
with open(path,'r') as text:
  words = [raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]
  words_index = set(words)
for word in sorted(counts_dict,key=lambda x: counts_dict[x],reverse=True):
  print('{} --{} times'.format(word,counts_dict[word]))

第1行,引入了一个新的模块,string. 其实这个模块的用法很简单

string.punctuation打印出来,其实这里面也仅仅是包含了所有的标点符号!@#$$%%%^&*

第4行,在文字的首位去掉了连着一起的标点符号,并把首字母大写的单词转化成小写;

第5行,将列表用set函数转换成集合,自动去掉其中所有重复的元素

第6行,创建了一个以单词为键key,以出现频率为值value的字典

第7-8行,打印整理后的函数,其中key=lambda x: counts_dict[x]叫做lambda表达式,可以暂且理解为以字典中的值为排序的参数