zoukankan      html  css  js  c++  java
  • python3练习:涉及序列

    #序列的sum函数,列表后面的数不是列表中的数,而是自定义的一个加数。
    a = [3,4,77,88,54]
    print (sum(a))          #226
    print (sum(a,10000))    #10226
    
    #eval函数就是执行一个字符串表达式,并返回表达式的值,实现list、dict、tuple与str之间的转化。
    a = '[3,4,77,88,54]'
    print('字符串a使用eval前的类型:',type(a))        #字符串a使用eval前的类型: <class 'str'>
    
    print('字符串a使用eval后的类型:',type(eval(a)))  #字符串a使用eval后的类型: <class 'list'>
    
    b = "{'q':3,'w':4,'t':8}"
    print('字符串a使用eval前的类型:',type(b))        #字符串a使用eval前的类型: <class 'str'>
    
    print('字符串a使用eval后的类型:',type(eval(b)))  #字符串a使用eval后的类型: <class 'dict'>
    
    
    
    
    #将一个字符串用字典输出显示
    c = "xyzacg"
    strc = str(c)
    dict1 = {}
    for i,item in enumerate(c):
        dict1[i] = item
    print (dict1)                      #{0: 'x', 1: 'y', 2: 'z', 3: 'a', 4: 'c', 5: 'g'}
    
    #输出字符串c中第n~m个字符,c[n-1,m],因为结束的时候不算第m个。
    print('c的第3~7个字符为:',c[2:7])  #c的第3~7个字符为: zabce
    
    
    #将两个字符串打包成字典
    d = ("aa","bb","cc")
    e = [12,13,14]
    f =dict(zip(d,e))
    print ("元组和列表打包为字典:",f)   #元组和列表打包为字典: {'aa': 12, 'bb': 13, 'cc': 14}
    
    d = "xyzufs"
    e = "123hvn"
    f2 =dict(zip(d,e))
    print ("两个字符串打包为字典:",f2)  #两个字符串打包为字典: {'x': '1', 'y': '2', 'z': '3', 'u': 'h', 'f': 'v', 's': 'n'}

    结果:

    226
    10226
    字符串a使用eval前的类型: <class 'str'>
    字符串a使用eval后的类型: <class 'list'>
    字符串a使用eval前的类型: <class 'str'>
    字符串a使用eval后的类型: <class 'dict'>
    {0: 'x', 1: 'y', 2: 'z', 3: 'a', 4: 'c', 5: 'g'}
    c的第3~7个字符为: zacg
    元组和列表打包为字典: {'aa': 12, 'bb': 13, 'cc': 14}
    两个字符串打包为字典: {'x': '1', 'y': '2', 'z': '3', 'u': 'h', 'f': 'v', 's': 'n'}
  • 相关阅读:
    NETCore中RabbitMQ的使用
    net core 中间件
    表达式和表达式树
    NET
    解决Net内存泄露原因
    Spring的IoC容器-Spring BeanFactory容器
    Spring的Hello World工程
    Linux下使用curl进行http请求(转)
    Java教程收集
    使用wget进行整站下载(转)
  • 原文地址:https://www.cnblogs.com/jxba/p/11908726.html
Copyright © 2011-2022 走看看