python中四种数据类型_python中几种常用的数据类型

1.字典

字典的创建:

dict1=dict((('name','PIG'),)),其中第一层()代表里面的内容是dict函数的输入参数。

第二层和第三层代表字典中的各元素,也就是key和value组合的列表,只不过这种列表不

是一种线性列表list,而是hash列表。在dict()和list()的函数调用中,里面用list标识符[]或者()

元组tuple效果一样。也可以像int()一样,将其看成强转换。dict2={'name':'PIG'}

字典的修改:

dict1={'name':'dog'}

dict1['age']=18dict1.setdefault('name','cat')

dict1.setdefault('age',34)

这两种效果一致

字典的查找:dict2={'age': 23, 'name': 'pis','career': 'student'}

print(dict2['name'])

print(list(dict2.keys()))

print(list(dict2.values()))

print(list(dict2.items()))

print出来的顺序与输入无关,而是根据hash算法自动调整。list()是一个强制类型转换。

将keys/values/items类型转换为list类型。 dict3={'age': 23, 'name': 'pis','career': 'student'}

dict4={'birthday':'94-09-01'}

dict3.update(dic4)

print(dict3)

updata()将key值相同的替换,其他的添加。

key值只能为数值,tuple,字符串等不可变类型,不能为list和dict。删除元素

dict4.clear() # 清空字典

print(dict4)

del dict4['name'] #删除字典中指定键值对

print(dict4)

print(dict4.pop('age')) #删除字典中指定键值对,并返回该键值对的值

ret=dict4.pop('age')

print(ret)

print(dict4)

pop操作可返回被pop的value值。

a = dict4.popitem() #随机删除某组键值对,并以元组方式返回值

print(a, dict4)

del dict4 #删除整个字典

print(dict4)

其他操作以及涉及到的方法

dict5=dict.fromkeys(['host1','host2','host3'],'test')

dict6=dict.fromkeys(['host1','host2','host3'],['test1','tets2'])

#print(dic6)

#dic6['host2']='abc'

#print(dic6)

这两个的区别是改变某一个key值时,第一个不影响其他的,第二个一起改变。因为字符串是不可变类型,list是可变类型。

dict可嵌套dict。

sorted(dict6.items())可对无序的hash顺序按照key值排序。对dict和list的for运算

for i in dic5:

print(i,dic5[i])

for i,v in dic5.items():

print(i,v)

2 string操作a='dad'

b='mom'

c=a+b 加操作

d=a*5 乘操作print('helloworld'[2:])切片操作

关键字 in

# print(123 in [23,45,123])

# print('e2l' in 'hello')输出True和False格式化输出

print('%s is a good teacher'%'alex')

msg='''

----------info of %s----------

Name: %s

Age : %d

Job : %s

Salary: %f

You will be retired in %s years

----------end---------

''' % (name, name, age, job, salary, 50-age)

c=# print(c)连接join方法

''.join([a,b,d])

#摘一些重要的字符串方法

#1 print(st.count('l'))

# print(st.center(50,'#')) # 居中

# print(st.startswith('he')) # 判断是否以某个内容开头

# print(st.find('t'))

# print(st.format(name='alex',age=37)) # 格式化输出的另一种方式 待定:?:{}

# print('My tLtle'.lower())

# print('My tLtle'.upper())

# print('\tMy tLtle\n'.strip())

# print('My title title'.replace('itle','lesson',1))

# print('My title title'.split('i',1))

具体可看string类源码

3.list操作

list的切片操作

#a=['xiaohong', 'xiaoming', 'xiaoliang', 'xiaofang', 'xiaoli']

# print(a[2:-1])

# print(a[1:])

# print(a[1:-1])

# print(a[1:-1:1])

# print(a[1::2])

# print(a[3::-2])增

# a.append('xiaoliu')

# print(a)

# a.insert(1,'xiaoxiong')

# print(a)

a[1] = 'haidilao'

# print(a)

# a[1:3] = ['da','ads']

# print(a)

# a.remove('xiaoming')

# print(a)

# b = a.pop(1)

#

# print(a)

# print(b)

# del(a[0])

# print(a)

# del(a)

# print(a)

# a.pop(1)

# print(a)其他操作

#t = ['to', 'on', 'be', 'to', 'to', 'to'].count('to')

# print(t)

#

# a = [1, 2, 3]

# b = [4, 5, 6]

# a.extend(b)

# print(a)

# print(b)

# print(a.index(1))

# a.reverse()

# print(a)

# x = [4, 6, 2, 1, 7, 9, ]

# x.sort()

# print(x)

#t = ['to', 'on', 'be', 'to', 'to', 'to']

# t.sort(reverse=True)

# print(t)

# print(t.count('dad'))

# print("haide"in t)

# b = t.copy()

# print(b)

主要关注三种重要的数据类型list tuple dictionary的常用操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值