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字符串,元组、列表、字典的更多相关文章

  1. python字符串/元组/列表/字典互转

    #-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...

  2. 转:python字符串/元组/列表/字典互转

    #-*-coding:utf-8-*-  #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ...

  3. python 小白(无编程基础,无计算机基础)的开发之路,辅助知识6 python字符串/元组/列表/字典互转

    神奇的相互转换,小白同学可以看看,很有帮助 #1.字典dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ...

  4. 【转】python字符串/元组/列表/字典互转

    #-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...

  5. Python之路 day2 字符串/元组/列表/字典互转

    #-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...

  6. python基础之数据类型/字符串/元组/列表/字典

    Python 数据类型 数字类型: int整型,long 长整型(在python3.0里不区分整型和长整型).float浮点型:complex复数(python中存在小数字池:-5--257):布尔值 ...

  7. python数据类型:序列(字符串,元组,列表,字典)

    序列通常有2个特点: 1,可以根据索引取值 2,可以切片操作 字符串,元组,列表,字典,都可以看做是序列类型 我的操作环境:Ubuntu16.04+python2.7 一.字符串类型 >按索引获 ...

  8. python中元组/列表/字典/集合

    转自:https://blog.csdn.net/lobo_seeworld/article/details/79404566

  9. python字符串、列表和字典的说明

    python字符串.列表和字典的说明 字符串.列表.字典 字符串的作用存储一段数据信息.例如 info = '我爱北京天安门' ,在调取的时候可以直接调取,灵活方便,print(info) 就可以把刚 ...

  10. python字符串、列表和文件对象总结

    1.字符串是字符序列.字符串文字可以用单引号或者双引号分隔. 2.可以用内置的序列操作来处理字符串和列表:连接(+).重复(*).索引([]),切片([:])和长度(len()).可以用for循环遍历 ...

随机推荐

  1. ffmpeg处理RTMP流媒体的命令 发送流媒体的命令(UDP,RTP,RTMP)

    将文件当做直播送至live ffmpeg -re -i localFile.mp4 -c copy -f flv rtmp://server/live/streamName   re限制输出速率,按照 ...

  2. Android 编程下的自定义 xmlns

    什么是 xmlns xmlns是 XML Namespaces 的缩写,中文名称是 XML命名空间. xmlns 使用规则 xmlns:namespace-prefix="namespace ...

  3. TestNg线程池配置、执行次数配置、超时配置

    使用注解的方式对TestNg线程池配置.执行次数配置.超时配置 注:使用注解来控制测试方法运行的次数和超时时间,timeOut在单线程或者多线程模式下都可用,threadPoolSize设置了线程池的 ...

  4. oracle sql别名

    为sql的字段起一个别名,常以为是可有可无的,但是有时候是必要的. 例如在ibatis中 <!-- 获取已发或待发送的彩信记录列表 --> <resultMap id="m ...

  5. 【Cocos2d-x游戏开发】浅谈游戏中的坐标系

    无论是开发2D还是开发3D游戏,首先必须弄清楚坐标系的概念.在Cocos2d-x中,需要了解的有OpenGL坐标系.世界坐标系和节点坐标系.  1.UI坐标系 IOS/Android/Windows ...

  6. ccc 旋转

    cc.Class({ extends: cc.Component, properties: { moveSpeed: 100, rotationSpeed: 90 }, // use this for ...

  7. Arima拟合函数,se出现NaN问题

    R语言Arima函数拟合模型,se(标准误)出现NaN的问题. 参考了网上的资料,虽然不太明白是什么意思,但是这样的模型不能用. 参考:http://stackoverflow.com/questio ...

  8. view not attached to windows manager与This Toast was not created with Toast.makeText()

      http://blog.sina.com.cn/s/blog_474928c90100x871.html     public class Ex04_1Activity extends Activ ...

  9. 【BZOJ】2795: [Poi2012]A Horrible Poem

    题意 一个长度为\(n(n \le 500000)\)的字符串\(s\),给\(q(q \le 2000000)\)个询问,每个询问给一个区间\([l, r]\),求这个区间内最短的循环节. 分析 分 ...

  10. 【BZOJ1003】1003: [ZJOI2006]物流运输trans SPFA+DP

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...