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循环遍历 ...
随机推荐
- 判断App是否在后台运行
在一些场景中,经常会需要判断App是否在后台运行,比如是否显示解锁界面,收到新消息是否显示Notification等.需求可能是多样化的,但所依仗的原理是相通的,今天Stay打算说说这些需求的最优解. ...
- POJ 3349 HASH
题目链接:http://poj.org/problem?id=3349 题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过 ...
- 《DSP using MATLAB》示例Example4.9
收敛域在圆外,对应原始时间序列为右边序列. 上代码: b = 1; a = poly([0.9, 0.9, -0.9]); % compute the polynomials coefficients ...
- 在IOS手机safari浏览器的无痕模式下,localStorage不起作用
无痕模式是黑色风格,正常模式是白色风格.在无痕模式中,使用localStorage.setItem()会报错,但在window对象下确实有localStorage.setItem方法. if (typ ...
- BZOJ 3282 Tree ——KD-Tree
[题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...
- js小例子(简单模糊匹配输入信息)
该例子实现的是用户输入信息或者字母时可以搜索出来,鼠标点击选择 <!DOCTYPE html> <html> <style> p{ width:200px; hei ...
- HDU5863 cjj's string game(DP + 矩阵快速幂)
题目 Source http://acm.split.hdu.edu.cn/showproblem.php?pid=5863 Description cjj has k kinds of charac ...
- Visual Studio: 暂时?绕过 fatal error C1083: Cannot open precompiled header file
可以使用右键点击项目工程中的该cpp文件,选择setting,在c/c++栏,选择PreCompiled headers,然后设置第一选项,选择不使用预编译头.
- iOS学习17之OC内存管理
1.内存管理的方式 1> iOS应用程序出现Crash(闪退),90%的原因是因为内存问题. 2> 内存问题 野指针异常:访问没有所有权的内存,如果想要安全的访问,必须确保空间还在 内存泄 ...
- 【BZOJ】3676: [Apio2014]回文串
http://www.lydsy.com/JudgeOnline/problem.php?id=3676 题意:给一个串求回文串×出现次数的最大值.(|S|<=300000) #include ...