Python字符串,元组、列表、字典
1.字符串
<string>.strip() 去掉两边空格及去指定字符
<string>.split() 按指定字符分隔字符串为数组
<string>.isdigit()判断是否为数字类型
字符串操作集:http://www.cnpythoner.com/wiki/string.html
tt=322
tem='%d' %tt
tem即为tt转换成的字符串
字符串长度len(tt)
import string
f2 = string.atof(a1)
列表转字符串:
dst = '.'.join(list1)
2.元组--不可更改的数据序列
被用圆括号包围()
3.列表--
[]
>>> living_room=("rug","chair")
>>> living_room
('rug', 'chair')
>>> apartment=[]
>>> apartment.append(living_room)
>>> apartment
[('rug', 'chair')]
>>> apartment.extend(living_room)
>>> apartment
[('rug', 'chair'), 'rug', 'chair']
apartment.extend(living_room)将living_room中的每个元素都插入到调用它的列表中
stop = [line.strip().decode('utf-8', 'ignore') for line in open('/home/xdj/chstop.txt').readlines()]
outStr = ''
for word in wordList:#
if not word in stop: #不在列表中
outStr += word
outStr += ' '
fout.write(outStr.strip().encode('utf-8')) #将分词好的结果写入到输出文件
二维列表/数组
2d_list = [[0 for col in range(cols)] for row in range(rows)]
其中cols, rows变量替换为你需要的数值即可
4.字典--
{}
字典排序:http://www.cnblogs.com/kaituorensheng/archive/2012/08/07/2627386.html
sorted(dic,value,reverse)
- dic为比较函数,value 为排序的对象(这里指键或键值),
- reverse:注明升序还是降序,True--降序,False--升序(默认)
>>> a = {1:0.12, 2:0.012, 0:0.22}
>>> a
{0: 0.22, 1: 0.12, 2: 0.012}
>>> a = sorted(a.iteritems(), key= lambda asd:asd[1],reverse = False)
>>> a
[(2, 0.012), (1, 0.12), (0, 0.22)] #变成了list , 数据结构转变
>>> a = sorted(a.iteritems(), key= lambda asd:asd[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'iteritems'
>>> a[1]
(1, 0.12)
>>> a[0]
(2, 0.012)
>>> a[2]
(0, 0.22)
>>> a[0][0]
2
增加元素
>>> a = {1:0.123}
>>> a[2]=0.325 #方式一
>>> a
{1: 0.123, 2: 0.325}
>>> a.setdefault(3,0.25) #方式二
0.25
>>> a
{1: 0.123, 2: 0.325, 3: 0.25}
>>> a[2]=0.999 区别:在原来基础上修改
>>> a
{1: 0.123, 2: 0.999, 3: 0.25}
>>> a.setdefault(2,0.111) 保持原来键-值对
0.999
>>> a
{1: 0.123, 2: 0.999, 3: 0.25}
>>>
5.集合set--互不相同的值
Python字符串,元组、列表、字典的更多相关文章
- python字符串/元组/列表/字典互转
#-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...
- 转:python字符串/元组/列表/字典互转
#-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ...
- python 小白(无编程基础,无计算机基础)的开发之路,辅助知识6 python字符串/元组/列表/字典互转
神奇的相互转换,小白同学可以看看,很有帮助 #1.字典dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ...
- 【转】python字符串/元组/列表/字典互转
#-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...
- Python之路 day2 字符串/元组/列表/字典互转
#-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...
- python基础之数据类型/字符串/元组/列表/字典
Python 数据类型 数字类型: int整型,long 长整型(在python3.0里不区分整型和长整型).float浮点型:complex复数(python中存在小数字池:-5--257):布尔值 ...
- python数据类型:序列(字符串,元组,列表,字典)
序列通常有2个特点: 1,可以根据索引取值 2,可以切片操作 字符串,元组,列表,字典,都可以看做是序列类型 我的操作环境:Ubuntu16.04+python2.7 一.字符串类型 >按索引获 ...
- python中元组/列表/字典/集合
转自:https://blog.csdn.net/lobo_seeworld/article/details/79404566
- python字符串、列表和字典的说明
python字符串.列表和字典的说明 字符串.列表.字典 字符串的作用存储一段数据信息.例如 info = '我爱北京天安门' ,在调取的时候可以直接调取,灵活方便,print(info) 就可以把刚 ...
- python字符串、列表和文件对象总结
1.字符串是字符序列.字符串文字可以用单引号或者双引号分隔. 2.可以用内置的序列操作来处理字符串和列表:连接(+).重复(*).索引([]),切片([:])和长度(len()).可以用for循环遍历 ...
随机推荐
- POJ 2418 字典树
题目链接:http://poj.org/problem?id=2418 题意:给定一堆树的名字,现在问你每一棵树[无重复]的出现的百分比,并按树名的字典序输出 思路:最简单的就是用map来写,关于字典 ...
- MVVM datatemplate 下button.contextmenu的command 失效解决方案
<Button CommandParameter="{Binding}" Tag="{Binding RelativeSource={RelativeSource ...
- memcache/redis 缓存学习笔记
0.redis和memcache的区别 a.redis可以存储除了string之外的对象,如list,hash等 b.服务器宕机以后,redis会把内存的数据持久化到磁盘上,而memcache则不会 ...
- ural 1255. Graveyard of the Cosa Nostra
1255. Graveyard of the Cosa Nostra Time limit: 1.0 secondMemory limit: 64 MB There is a custom among ...
- CF#335 Lazy Student
Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Excel: 公式
单元格内输入 = xx公式 e.g =EXACT(C2,D2) //比较两个单元格内的文本是否相同
- Codeforces Round #246 (Div. 2) B. Football Kit
题目的意思是求出每个队穿主场衣服和客场衣服的次数 每个队作为主场的次数是n-1,作为客场的次数是n-1 当每个队打主场的时候肯定穿的主场衣服 当每个队打客场时,如果客场与主场的衣服不同,则穿客场衣服 ...
- ACM BUYING FEED
BUYING FEED 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 Farmer John needs to travel to town to pick up ...
- CF 22B. Bargaining Table
水题.好久没有写过优化搜索题了. #include <cstdio> #include <cstring> #include <iostream> #include ...
- 【CodeVS】p1299 切水果
题目描述 Description 简单的说,一共N个水果排成一排,切M次,每次切[L,R]区间的所有水果(可能有的水果被重复切),每切完一次输出剩下水果数量 数据已重新装配,不会出现OLE错误 时限和 ...