https://blog.csdn.net/questionfish/article/details/46739207
https://blog.csdn.net/qq_19707521/article/details/79174533
.split()
.split()为什么没有空格,因为分隔符默认为空格,但是不能为空('')。若字符串中没有分隔符,则把整个字符串作为列表的一个元素。
Python split()方法
Python中的split()和os.path.split()
list,tuple,dict,set
Python中list,tuple,dict,set的区别和用法
python-基础语法-list、array、set、tuple
1、list、tuple是有序列表;dict、set是无序列表
2、list元素可变、tuple元素不可变
3、dict和set的key值不可变,唯一性
4、set只有key没有value
5、set的用途:去重、并集、交集等
6、list、tuple:+、*、索引、切片、检查成员等
7、dict查询效率高,但是消耗内存多;list、tuple查询效率低、但是消耗内存少
list[]
tuple()
set{}
dict{}
pprint
from pprint import pprint
pprint就是pretty-printer,打印出来更pretty.
列表生成式
[word for word in document.lower().split() if word not in stoplist]
这样可以快速生成列表
列表生成式
texts = [[word for word in document.lower().split() if word not in stoplist]
for document in documents]
这样嵌套的list生成式怎么看?先执行外循环,再执行内循环,和普通的循环是一个执行顺序。
collections模块的defaultdict类
内建模块collections
使用dict时,如果引用的Key不存在,就会抛出KeyError。如果希望key不存在时,返回一个默认值,就可以用defaultdict。
注意默认值是调用函数返回的,而函数在创建defaultdict对象时传入。这里frequency = defaultdict(int)
传入int,key不存在时会返回0.
>>> from collections import defaultdict
>>> dd = defaultdict(int)
>>> dd['key1'] = 'abc'
>>> dd['key1'] # key1存在
'abc'
>>> dd['key2'] # key2不存在,返回默认值
0
文件读写
https://www.liaoxuefeng.com/wiki/897692888725344/923030555456160
read()方法可以一次读取文件的全部内容,Python把内容读到内存,用一个str对象表示。
网友评论