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循环遍历 ...
随机推荐
- Linux查看可执行程序所在路径
首先通过命令获得进程PID:如4285,然后执行下述命令 cd /proc/4285 ls -l 或直接ls -l /proc/4285 其中exe所在行即为可执行文件的全路经.如下图所示:
- no-jquery 01Elements
Select Elements id // IE 5.5+ document.getElementById('myElement'); // IE 8+ document.querySelector( ...
- Swift3.0语言教程查找字符集和子字符串
Swift3.0语言教程查找字符集和子字符串 Swift3.0语言教程查找字符集和子字符串,在字符串中当字符内容很多时,我们就需要使用到查找字符集或者子字符串的方法.以下我们将讲解3种查找字符集和子字 ...
- pycharm设置主题/默认格式/字体
1.步骤为:File-->Settings-->Appearance & Behavior-->Appearance-->Theme中就可以选择喜欢的主题 2.一般将文 ...
- 【转】linux network namespace 学习
原文地址:https://segmentfault.com/a/1190000004059167 介绍 在专业的网络世界中,经常使用到Virtual Routing and Forwarding(VR ...
- MFC 设置控件事件对应的函数
在项目中,右击你想设置的控件,打开属性. 然后找到:控件事件,如果在Visual Studio 2015中操作,显示如图: 然后,以设定单击事件为例: 点击右边的三角,选择 <Edit Code ...
- 使用expdp时遇到ORA-39002、ORA-39070错误
使用expdp时,遇到”ORA-39002.ORA-39070......”连续报错. 1. 遇到的问题 expdp yguo/dbimp@botnet schemas=yguo dumpfile= ...
- ural 1341. Device
1341. Device Time limit: 1.0 secondMemory limit: 64 MB Major (M): You claimed that your device would ...
- 【Cocos2d-x游戏开发】浅谈游戏中的坐标系
无论是开发2D还是开发3D游戏,首先必须弄清楚坐标系的概念.在Cocos2d-x中,需要了解的有OpenGL坐标系.世界坐标系和节点坐标系. 1.UI坐标系 IOS/Android/Windows ...
- Android 实用开源控件
图片放大缩小: PinchImageView 体验最好的图片手势控件,不同分辨率无缝切换,可与ViewPager结合使用. GestureViews 带有手势控制的ImageView和FrameLay ...