美文网首页
python基础4_字典以及字符串操作

python基础4_字典以及字符串操作

作者: 灵秋公子 | 来源:发表于2020-01-04 10:55 被阅读0次

    字典两大特点:无序,键唯一

    字典的创建

    a = list()
    print(a)
    dic = {'name': 'alex'}
    dic1 = {}
    dic2 = dict((('name', 'alex'),))
    print(dic2)
    dic3 = dict([['name', 'alex'], ])
    print(dic3)
    dic1 = {'name': 'alex'}
    dic1['age'] = 18
    print(dic1)
    

    字典的转换

    dic2=dict((('name','alex'),))#元组转换为字典dict()
    print(dic2)
    dic3=dict([['name','alex'],])#列表转换为字典dict()
    print(dic3)
    

    修改

    dic1={'name':'alex'}
    dic1['age']=18#重新对字典制定的value赋值
    print(dic1)
    #键存在,不改动,返回字典中相应的键对应的值
    ret=dic1.setdefault('age',34)
    print(ret)
    #键不存在,在字典中中增加新的键值对,并返回相应的值
    ret2=dic1.setdefault('hobby','girl')
    print(dic1)
    print(ret2)
    

    查找

    dic3={'age': 18, 'name': 'alex', 'hobby': 'girl'}
    
    print(dic3['name'])
    
    print(list(dic3.keys()))
    print(list(dic3.values()))
    print(list(dic3.items()))
    
    li=[1,2,34,4]
    li[2]=5
    dic3={'age': 18, 'name': 'alex', 'hobby': 'girl'}
    dic3['age']=55
    print(dic3)
    
    dic4={'age': 18, 'name': 'alex', 'hobby': 'girl'}
    # dic5={'1':'111','2':'222'}
    dic5={'1':'111','name':'222'}
    dic4.update(dic5)
    print(dic4)
    print(dic5)
    

    删除

    dic5 = {'name': 'alex', 'age': 18, 'class': 1}
    
    dic5.clear() # 清空字典
    print(dic5)
    del dic5['name'] #删除字典中指定键值对
    print(dic5)
    print(dic5.pop('age')) #删除字典中指定键值对,并返回该键值对的值
    ret=dic5.pop('age')
    print(ret)
    print(dic5)
    a = dic5.popitem() #随机删除某组键值对,并以元组方式返回值
    print(a, dic5)
    del dic5        #删除整个字典
    print(dic5)
    

    其他操作

    dic6=dict.fromkeys(['host1','host2','host3'],'test')
    print(dic6)#{'host3': 'test', 'host1': 'test', 'host2': 'test'}
    
    dic6['host2']='abc'
    print(dic6)
    
    dic6=dict.fromkeys(['host1','host2','host3'],['test1','tets2'])
    print(dic6)#{'host2': ['test1', 'tets2'], 'host3': ['test1', 'tets2'], 'host1': ['test1', 'tets2']}
    
    dic6['host2'][1]='test3'
    print(dic6)#{'host3': ['test1', 'test3'], 'host2': ['test1', 'test3'], 'host1': ['test1', 'test3']}
    
    

    字符串操作

    String的内置方法

    st='hello kitty {name} is {age}'
    
    print(st.count('l'))       #  统计元素个数
    print(st.capitalize())     #  首字母大写
    print(st.center(50,'#'))   #  居中
    print(st.endswith('tty3')) #  判断是否以某个内容结尾
    print(st.startswith('he')) #  判断是否以某个内容开头
    print(st.expandtabs(tabsize=20))
    print(st.find('t'))        #  查找到第一个元素,并将索引值返回
    print(st.format(name='alex',age=37))  # 格式化输出的另一种方式   待定:?:{}
    print(st.format_map({'name':'alex','age':22}))#以字典键值对替换参数
    print(st.index('t')) #查找字符串是否包含“t”,不包含报错
    print('asd'.isalnum())#包含字母或者数值--返回Ture
    print('12632178'.isdecimal())#十进制返回Ture
    print('1269999.uuuu'.isnumeric())#判断是否为数字,返回Ture或False
    print('abc'.isidentifier())#检测是否为非法变量
    print('Abc'.islower())#全为小写返回Ture
    print('ABC'.isupper())#全为大写返回Ture
    print('  e'.isspace())#是否是个空格,返回Ture
    print('My title'.istitle())#是否为title格式(首字母大写)否则返回Talse
    print('My tLtle'.lower())#全部转为小写
    print('My tLtle'.upper())#全部转为大写
    print('My tLtle'.swapcase())#翻转,大写变小写,小写变大写
    print('My tLtle'.ljust(50,'*'))#左边加入“*”靠左补充
    print('My tLtle'.rjust(50,'*'))#右边加入“*”靠右补充
    print('\tMy tLtle\n'.strip())#去掉左右两边的换行符及空格
    print('\tMy tLtle\n'.lstrip())#去掉左边的换行符及空格
    print('\tMy tLtle\n'.rstrip())#去掉右边的换行符及空格
    print('ok')
    print('My title title'.replace('itle','lesson',1))#替换字符串内容()第一个参数为字符串中需要进行替换的内容,第二个为替换成的内容,第三个为替换的次数
    print('My title title'.rfind('t'))#查找右侧开始,第一个“t”的位置下标
    print('My title title'.split('i',1))#分割字符串为一个列表
    a = '123'
    b = 'qwe'
    c = 'ert'
    d = ' '.join(a,,b,c)
    print(d)#转换拼接为字符串
    print('My title title'.title())#转换为首字母大写的字符串
    

    格式化字符串

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

    关键字 in

    print(123 in [23,45,123])
    print('e2l' in 'hello')
    
    a="Let's go "
     print(a)
    1   * 重复输出字符串
    print('hello'*20)
    2 [] ,[:] 通过索引获取字符串中字符,这里和列表的切片操作是相同的,具体内容见列表
    print('helloworld'[2:])
    

    相关文章

      网友评论

          本文标题:python基础4_字典以及字符串操作

          本文链接:https://www.haomeiwen.com/subject/rjyxactx.html