首页 技术 正文
技术 2022年11月21日
0 收藏 507 点赞 4,935 浏览 1412 个字
texts=[[word for word in document.lower().split() if word not in stoplist] for document in documents]

  这段代码等同于:

 texts=[]
for document in documents:
words=[]
for word in document.lower().split():
if word not in stoplist():
words.append(word)
texts.append(words)

  【python】笔记

【python】笔记

python 中 counter用法

—————————

>>>from collections import Counter 
>>>c = Counter()
>>>for ch in ‘programing’:
…    c[ch]=c[ch]+1
    
>>>c
Counter({‘g’: 2, ‘r’: 2, ‘a’: 1, ‘i’: 1, ‘m’: 1, ‘o’: 1, ‘n’: 1, ‘p’: 1})

counter 是一个简单的计数器,比如统计字符出现的个数
——————— —–

python 中 json使用方法

一、概念理解

1、json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串)
  (1)json.dumps()函数是将一个Python数据类型列表进行json格式的编码(可以这么理解,json.dumps()函数是将字典转化为字符串)
  (2)json.loads()函数是将json格式数据转换为字典(可以这么理解,json.loads()函数是将字符串转化为字典)

2、json.dump()和json.load()主要用来读写json文件函数

二、代码测试

1.py

import json
# json.dumps()函数的使用,将字典转化为字符串
dict1 = {"age": "12"}
json_info = json.dumps(dict1)
print("dict1的类型:"+str(type(dict1)))
print("通过json.dumps()函数处理:")
print("json_info的类型:"+str(type(json_info)))

运行截图:

【python】笔记

2.py

import json
# json.loads函数的使用,将字符串转化为字典
json_info = '{"age": "12"}'
dict1 = json.loads(json_info)
print("json_info的类型:"+str(type(json_info)))
print("通过json.dumps()函数处理:")
print("dict1的类型:"+str(type(dict1)))

运行截图:

【python】笔记

3.py

import json
# json.dump()函数的使用,将json信息写进文件
json_info = "{'age': '12'}"
file = open('1.json','w',encoding='utf-8')
json.dump(json_info,file)

运行截图(1.json文件):

【python】笔记

4.py

import json
# json.load()函数的使用,将读取json信息
file = open('1.json','r',encoding='utf-8')
info = json.load(file)
print(info)

运行截图:

【python】笔记

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,960
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,484
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,330
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,113
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,745
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,779