Python笔记(6)----列表、元组、字符串、字典等的相关用法
2017-05-24 19:59
639 查看
列表(可变的)
list函数
示例:
print list('hello')
结果:
['h', 'e', 'l', 'l', 'o']
基本列表操作
1、改变列表:元素赋值
示例;
a = [2,2,2]
a[1]=1
print a
结果:
[2, 1, 2]
2、删除元素
示例:
a = [1,2,3]
del a[2]
print a
结果:
[1, 2]
3、分片赋值
示例:
name = list('pell')
name[1:] = list ('ython')
print name
numbers = [1,5]
numbers[1:1] = [2,3,4]
print numbers
b = [1,2,3,4,5]
b[1:4] = []
print b
结果:
['p', 'y', 't', 'h', 'o', 'n']
[1, 2, 3, 4, 5]
[1, 5]
列表方法
方法是一个与某些对象有紧密联系的函数,对象可能是列表、数字,也可能是字符串或者其他类型的对象,一般来说可以这样调用:
对象.方法(参数)
1、append
append方法用于在列表末尾追加新的对象,
示例:
list = [1,2,3]
list.append(4)
print list
结果:
[1, 2, 3, 4]
2、count
count方法统计某个元素在列表中出现的次数,
示例:
a = [1,[1,2],1,2,[1,3],1]
print a.count(1)
结果:
3
3、extend
extend方法可以在列表的末尾一次性添加另一个序列中的多个值,
示例:
a = [1,2,3]
b = [4,5,6]
a.extend(b)
print a
结果:
[1, 2, 3, 4, 5, 6]
extend和连接的区别:extend修改了北扩展的序列,二I连接则是返回一个群新的序列,
4、index
index方法用于从列表中找出值第一个匹配项的索引位置,
示例:
x = ['what','is','your','name']
print x.index('your')
结果:
2
5、insert
insert方法用于将列表对象插入到列表中,
示例:
numbers = [1,2,3,4,5]
numbers.insert(3,'four')
结果:
[1, 2, 3, 'four', 4, 5]
6、pop
pop方法用于移除列表中的一个元素,并返回钙元素的值
示例:
x = [1,2,3]
x.pop(1)
print x
结果:
[1, 3]
默认情况下移除最后一个,
7、remove
remove用于移除列表中某个值的第一个匹配项,
示例:
x =[1,2,3,4,5,4,6,7,4]
x.remove(4)
print x
结果:
[1, 2, 3, 5, 4, 6, 7, 4]
8、reverse
reverse方法用于将列表中的元素反向存放,
示例:
x = [1,2,3,4]
x.reverse()
print x
print numbers
结果:
[4, 3, 2, 1]
9、sort
sort用于在原始位置对列表进行排序,
示例:
x = [4,6,1,7,9]
x.sort()
print x
结果:
[1, 4, 6, 7, 9]
10、高级排序
compare(x,y)函数
示例:
numbers = [5,2,7,9]
numbers.sort(cmp)
print numbers
结果:
[2, 5, 7, 9]
元组(不可变序列)
即使只有一个值也必须加一个逗号,
tuple函数
示例:
print tuple([1,2,3])
结果:
(1, 2, 3)
字符串
字符串格式化,操作符:%,%左边放格式化字符串,右边放希望格式化的值,
示例:
format = "hello.%s.%s enough for ya?"
values = ('world','hot')
print format % values
结果:
hello.world.hot enough for ya?
示例:
format = "pi with three decimals; %.3f"
from math import pi
print format % pi
结果:
pi with three decimals; 3.142
示例:
print '%s plus %s equals %s' % (1,1,2)
结果:
1 plus 1 equals 2
字段宽度和精度
示例:
from math import pi
print '%10f' % pi
print'%10.2f' % pi
print '%.2f' % pi
print'%.5s' % 'Guido van Rossum'
结果:
3.141593
3.14
3.14
Guido
符号对齐和填充
示例:
from math import pi
print '%010.2f' % pi
结果:
0000003.14
示例:
from math import pi
print '%-10.2f' % pi
结果:
3.14
字符串方法
1、find
find可以在一个较长的字符串中查找子字符串,它返回子串所在位置的最左端索引,
2、join
join方法是split方法的逆方法,用来在队列中添加元素
3、lower
lower方法返回字符串的小写字母版,
4、replace
replace方法返回某字符串的所有匹配项均被替换后得到的字符串,
5、split
将字符串分割成序列,
6、strip
strip方法返回去除两侧空格的字符串,
7、translate
translate方法和replace方法一样,可以替换字符串中的某个部分,但是它只处理耽搁的字符,
字典
字典由多个键及对应的值构成的对组成,键是唯一的,值把并不唯一
dict函数
示例:
items = [('name','Gumby'),('age',42)]
d = dict(items)
print d
结果:
{'age': 42, 'name': 'Gumby'}
字典的方法
1、clear
清楚字典中的所有项,
2、copy
返回一个具有相同键值对的新字典,
3、fromkeys
是给定的键建立新的字典,每个键默认的值为none,
4、get
get方法是个更宽松的访问字典项的方法,
5、has_key
检查字典中是否有给出的键
6、items和iteritems
将所有紫貂项一列表的形式返回,无顺序,后者会返回一个迭代器,
7、keys和iterkeys
将字典中的键一列表的形式返回,后者返回针对键的迭代器
8、pop
获得相应给定键的值,然后将这个键值对从字典中移除,
9、poitem
弹出随机项
10、setdfault
与list。pop()类似能获得与给定键相关联的值,若没有可以设定相应的键值,
11、update
用一个字典项更新另一个字典
list函数
示例:
print list('hello')
结果:
['h', 'e', 'l', 'l', 'o']
基本列表操作
1、改变列表:元素赋值
示例;
a = [2,2,2]
a[1]=1
print a
结果:
[2, 1, 2]
2、删除元素
示例:
a = [1,2,3]
del a[2]
print a
结果:
[1, 2]
3、分片赋值
示例:
name = list('pell')
name[1:] = list ('ython')
print name
numbers = [1,5]
numbers[1:1] = [2,3,4]
print numbers
b = [1,2,3,4,5]
b[1:4] = []
print b
结果:
['p', 'y', 't', 'h', 'o', 'n']
[1, 2, 3, 4, 5]
[1, 5]
列表方法
方法是一个与某些对象有紧密联系的函数,对象可能是列表、数字,也可能是字符串或者其他类型的对象,一般来说可以这样调用:
对象.方法(参数)
1、append
append方法用于在列表末尾追加新的对象,
示例:
list = [1,2,3]
list.append(4)
print list
结果:
[1, 2, 3, 4]
2、count
count方法统计某个元素在列表中出现的次数,
示例:
a = [1,[1,2],1,2,[1,3],1]
print a.count(1)
结果:
3
3、extend
extend方法可以在列表的末尾一次性添加另一个序列中的多个值,
示例:
a = [1,2,3]
b = [4,5,6]
a.extend(b)
print a
结果:
[1, 2, 3, 4, 5, 6]
extend和连接的区别:extend修改了北扩展的序列,二I连接则是返回一个群新的序列,
4、index
index方法用于从列表中找出值第一个匹配项的索引位置,
示例:
x = ['what','is','your','name']
print x.index('your')
结果:
2
5、insert
insert方法用于将列表对象插入到列表中,
示例:
numbers = [1,2,3,4,5]
numbers.insert(3,'four')
结果:
[1, 2, 3, 'four', 4, 5]
6、pop
pop方法用于移除列表中的一个元素,并返回钙元素的值
示例:
x = [1,2,3]
x.pop(1)
print x
结果:
[1, 3]
默认情况下移除最后一个,
7、remove
remove用于移除列表中某个值的第一个匹配项,
示例:
x =[1,2,3,4,5,4,6,7,4]
x.remove(4)
print x
结果:
[1, 2, 3, 5, 4, 6, 7, 4]
8、reverse
reverse方法用于将列表中的元素反向存放,
示例:
x = [1,2,3,4]
x.reverse()
print x
print numbers
结果:
[4, 3, 2, 1]
9、sort
sort用于在原始位置对列表进行排序,
示例:
x = [4,6,1,7,9]
x.sort()
print x
结果:
[1, 4, 6, 7, 9]
10、高级排序
compare(x,y)函数
示例:
numbers = [5,2,7,9]
numbers.sort(cmp)
print numbers
结果:
[2, 5, 7, 9]
元组(不可变序列)
即使只有一个值也必须加一个逗号,
tuple函数
示例:
print tuple([1,2,3])
结果:
(1, 2, 3)
字符串
字符串格式化,操作符:%,%左边放格式化字符串,右边放希望格式化的值,
示例:
format = "hello.%s.%s enough for ya?"
values = ('world','hot')
print format % values
结果:
hello.world.hot enough for ya?
示例:
format = "pi with three decimals; %.3f"
from math import pi
print format % pi
结果:
pi with three decimals; 3.142
示例:
print '%s plus %s equals %s' % (1,1,2)
结果:
1 plus 1 equals 2
字段宽度和精度
示例:
from math import pi
print '%10f' % pi
print'%10.2f' % pi
print '%.2f' % pi
print'%.5s' % 'Guido van Rossum'
结果:
3.141593
3.14
3.14
Guido
符号对齐和填充
示例:
from math import pi
print '%010.2f' % pi
结果:
0000003.14
示例:
from math import pi
print '%-10.2f' % pi
结果:
3.14
字符串方法
1、find
find可以在一个较长的字符串中查找子字符串,它返回子串所在位置的最左端索引,
2、join
join方法是split方法的逆方法,用来在队列中添加元素
3、lower
lower方法返回字符串的小写字母版,
4、replace
replace方法返回某字符串的所有匹配项均被替换后得到的字符串,
5、split
将字符串分割成序列,
6、strip
strip方法返回去除两侧空格的字符串,
7、translate
translate方法和replace方法一样,可以替换字符串中的某个部分,但是它只处理耽搁的字符,
字典
字典由多个键及对应的值构成的对组成,键是唯一的,值把并不唯一
dict函数
示例:
items = [('name','Gumby'),('age',42)]
d = dict(items)
print d
结果:
{'age': 42, 'name': 'Gumby'}
字典的方法
1、clear
清楚字典中的所有项,
2、copy
返回一个具有相同键值对的新字典,
3、fromkeys
是给定的键建立新的字典,每个键默认的值为none,
4、get
get方法是个更宽松的访问字典项的方法,
5、has_key
检查字典中是否有给出的键
6、items和iteritems
将所有紫貂项一列表的形式返回,无顺序,后者会返回一个迭代器,
7、keys和iterkeys
将字典中的键一列表的形式返回,后者返回针对键的迭代器
8、pop
获得相应给定键的值,然后将这个键值对从字典中移除,
9、poitem
弹出随机项
10、setdfault
与list。pop()类似能获得与给定键相关联的值,若没有可以设定相应的键值,
11、update
用一个字典项更新另一个字典
相关文章推荐
- Python之列表、字符串、元组和字典的基本用法
- Python学习笔记ucas(lecture2)数据结构(字符串、列表、元组、字典、集合)
- Python笔记第2章,文件,字符串,列表,元组,字典,集合的使用
- python学习笔记2—python文件类型、变量、数值、字符串、元组、列表、字典
- Python 学习笔记 (1)—— 字符串、元组、列表、字典
- Python 核心编程笔记_Chapter_2 Note_3_赋值与变量_字符串_列表list_元组tuple_字典dict
- Python探索记(14)——字符串、列表、元组、字典与运算符相关的操作
- Python3中的列表,元组,字典,字符串相关知识小结
- Python笔记——基本数据结构:列表、元组及字典
- python - 字符串,元组,列表,字典
- python基础教程__列表、元组、字符串和字典
- python笔记之元组、列表、字典和文件
- Python笔记——基本数据结构:列表、元组及字典
- python字符串/元组/列表/字典互转
- python字符串/元组/列表/字典互转
- Python字符串、元组、列表、字典互相转换的方法
- python列表,元组,字典和字符串之间的相互转换
- 复习1次课(11月28日) 1.3 python的文件类型 1.4 python的变量 1.5 python的数值和字符串 1.6 元组 1.7 列表 1.8 字典 1.9 字典练习
- 简明python教程 --C++程序员的视角(四):容器类型(字符串、元组、列表、字典)和参考
- python 基础1 --列表 元组 字符串 字典