zoukankan      html  css  js  c++  java
  • python之变量操作

    变量类型

    Number(数字)       包括int,long,float,double,complex
    String(字符串)     例如:hello,"hello",hello
    List(列表)         例如:[1,2,3],[1,2,3,[1,2,3],4]
    Dictionary(字典)   例如:{1:"nihao",2:"hello"}
    Tuple(元组)        例如:(1,2,3,abc)
    File(文件)         例如:f = open(a.txt,rw)

    关于数字和字符串的操作这里略过,注意的是字符串是不允许跟数字相加的(java允许)

    一、元组

     元组是一组不可修改的元素的集合

    #定义一个4个元素的元组对象 类型可以不一样
    tuple=("basketball","football","pingpong",1)
    print tuple[0]
    print tuple[-1]
    #元组的切片操作 从0开始算 到目标的前一位
    tuple2=tuple[1:3]
    print tuple2
    tuple3=tuple[0:-2]
    print tuple3
    #定义一个2微元组
    tuple4=(tuple2,tuple3)
    print tuple4
    print tuple4[0][1]
    #tuple[0]="edit" #元组不支持修改 只元素读取
    t=(1,)
    print t
    print t.__len__()
    print tuple4[0].__len__()
    #遍历元组 range
    for i in range(len(tuple4)):
        print "tuple[%d]:"%i,"",
        for j in range(len(tuple4[i])):
            print tuple4[i][j],"",
            
    print "\n"        
    #使用map()循环遍历
    
    ''' '''   
    k=0
    for a in map(None,tuple4):
        print "tuple[%d]:" %k,"",
        for x in a:
            print x,"",
        print  
        k+=1

    结果:

    basketball
    1
    ('football', 'pingpong')
    ('basketball', 'football')
    (('football', 'pingpong'), ('basketball', 'football'))
    pingpong
    (1,)
    1
    2
    tuple[0]:  football  pingpong  tuple[1]:  basketball  football  
    
    tuple[0]:  football  pingpong 
    tuple[1]:  basketball  football 

    任何至少包含一个上元素的元组为真值。元素的值无关紧要。为创建单元素元组,需要在值之后加上一个逗号。没有逗号,Python 会假定这只是一对额外的圆括号,虽然没有害处,但并不创建元组

    二、列表

     列表也是一组元素的集合,它不同与于元组的是,列表中的元素是可以修改的。

    #*******列表的操作********
    #定义一个列表
    list=["android","ios","wp8","others"]
    print list
    #输出第三个列表元素
    print list[2]
    #追加一个元素
    list.append("others2")
    print list
    #指定位置插入一个元素
    list.insert(1, "insert")
    print list
    list.remove("insert")
    print list
    #反转列表
    list.reverse()
    #弹出最后一个元素
    print list.pop()
    list.pop(1)
    print list
    #切片操作
    print list[1:2]
    print list[1:-3]
    #定义一个二元列表
    list=[["apple","banana"],["orange","watermelon"]]
    #循环列表
    for i in range(len(list)):
        print "list[%d]:"%i,"",
        for j in range(len(list[i])):
            print list[i][j],"",
        print
    #获取列表的索引
    list=["android","ios","wp8","others"]
    print list.index("android" )    
    #判断某对象是否是列表元素成员(跟struts2的投影一样 犀利)
    print "android" in list
    list2=["sugarcane"]
    #append 追加的是一个对象列表
    list.append(list2)
    print list
    #extends和+都是追加一个元素
    list.extend(list2)
    print list
    list+=["other"]
    print list
    list.sort()
    print list
    print list.__len__()
    print list[0][0]
    #不删除 无法排序
    list.remove(["sugarcane"])
    print list
    list+=["other"]
    print list
    #过滤掉重复的元素
    for i in sorted(set(list)):
        print i,"",
    print
    
    #复制
    list=[1,2]
    b=list#如果是b=list 则会互相影响
    print list
    b.remove(1)
    print list
    list=[1,2]
    b=list[:]#纯复制 不受互相影响
    b.remove(1)
    print list

    查看结果

    View Code
    ['android', 'ios', 'wp8', 'others']
    wp8
    ['android', 'ios', 'wp8', 'others', 'others2']
    ['android', 'insert', 'ios', 'wp8', 'others', 'others2']
    ['android', 'ios', 'wp8', 'others', 'others2']
    android
    ['others2', 'wp8', 'ios']
    ['wp8']
    []
    list[0]:  apple  banana 
    list[1]:  orange  watermelon 
    0
    True
    ['android', 'ios', 'wp8', 'others', ['sugarcane']]
    ['android', 'ios', 'wp8', 'others', ['sugarcane'], 'sugarcane']
    ['android', 'ios', 'wp8', 'others', ['sugarcane'], 'sugarcane', 'other']
    [['sugarcane'], 'android', 'ios', 'other', 'others', 'sugarcane', 'wp8']
    7
    sugarcane
    ['android', 'ios', 'other', 'others', 'sugarcane', 'wp8']
    ['android', 'ios', 'other', 'others', 'sugarcane', 'wp8', 'other']
    android  ios  other  others  sugarcane  wp8 
    [1, 2]
    [2]
    [1, 2]

    可用的方法 tuple()可将列表转化为元组

    同样list() 可将元组转化为列表

    元组不可修改 也就没有 pop() remove() del()等方法

    #元组的特点
    >>> v = ('a', 2, True)
    
    >>> (x, y, z) = v 
    
    >>> x
    
    'a' 
    
    >>> y 
    
    2 
    
    >>> z
    
    True

     三、集合

    集合的特点
    1.无序
    2.唯一一个不重复值的容器
    3.元素不允许是List或则dict 如果tuple中包含了List或则dict
    也是不允许加入到集合中的

    #****1.创建一个集合 *****
    set_one={2,3,True,2}
    print set_one
    print len(set_one)#获取集合的个数
    #****2.添加元素 *****
    set_one.add(5)#添加元素
    print set_one
    #****3.判断某元素是否是在结合中 *****
    print 5 in set_one 
    set_two={1,2,6}#定义第二个集合
    #****4.判断某集合是否是另外一个集合的子集 *****
    print set_two.issubset(set_one)
    #****5.获取第二个集合没有的集合元素 *****
    one_mil_two=set_one-set_two
    print one_mil_two
    empty_dict={}#这种方式定义的不是集合 而是字典
    #****6.定义一个空的集合 *****
    empty_set=set()
    print empty_set#打印记过 set([])
    #****7.求两个集合的交集*****
    union_set=set_two.intersection(set_one)#
    print union_set
    #****8.求两个集合的并集*****
    all_set=set_two.union(set_one)#
    print all_set
    a=set()#定义一个空的集合
    #****9.给集合对象赋值*****
    a={1,2}
    print a
    a=set([1,2,"aa"])#
    print a
    a=set((1,2,"bb"))
    print a
    #****10.将集合转成字符 *****
    c=[str(x) for x in a]#无比强大的功能
    s="".join(c)
    print(s)
    #****11.将集合转成列表*****
    list=list(a)
    list.sort()
    print list #由于在python中 都是由ascii码组成 而数字的ascii在字母前面
    #****12.集合的运算符*****
    print {1,2}|{2,3}#并集
    print {1,2}&{2,3}#交集
    print {1,2}-{2,3}#差集
    print {1,2}^{2,3}#13 ==并集-交集
    #****13.集合的方法(重新复习一次)*****
    
    a=set([1,2])
    b={2,3}
    print "集合元素的个数:%d"%len(a) 
    print "1是否是集合中的一元:",1 in a
    print "a,b集合合并",a.union(b)#a|b
    print "a,b集合交集",a.intersection(b)#a&b
    print "a,b集合中只在a出现不在b出现的元素",a.difference(b)#a-b
    print a.symmetric_difference(b)#a^b
    print "a是否是b的子集",a.issubset(b)
    print a.issuperset({1})
    print "a是否是b的超集",a.issuperset(b)#
    a.difference_update(b)#删除a中含有b元素的元素
    print a
    b.remove(2)#删除2
    print b
    a.discard(1)#如果a中含有1 则删除
    print a
    a=set((1,2))
    a.pop()
    print a
    a.clear()#删除所有元素
    print a

    查看结果

    View Code
    set([True, 2, 3])
    3
    set([True, 2, 3, 5])
    True
    False
    set([3, 5])
    set([])
    set([1, 2])
    set([1, 2, 3, 5, 6])
    set([1, 2])
    set(['aa', 1, 2])
    set([1, 2, 'bb'])
    12bb
    [1, 2, 'bb']
    set([1, 2, 3])
    set([2])
    set([1])
    set([1, 3])
    集合元素的个数:2
    1是否是集合中的一元: True
    a,b集合合并 set([1, 2, 3])
    a,b集合交集 set([2])
    a,b集合中只在a出现不在b出现的元素 set([1])
    set([1, 3])
    a是否是b的子集 False
    True
    a是否是b的超集 False
    set([1])
    set([3])
    set([])
    set([2])
    set([])

    请注意:非运算符版本的 update(), intersection_update(), difference_update()和symmetric_difference_update()将会接受任意 iterable 作为参数。从 2.3.1 版本做的更改:以前所有参数都必须是 sets。

    还请注意:这个模块还包含一个 union_update() 方法,它是 update() 方法的一个别名。包含这个方法是为了向后兼容。程序员们应该多使用 update() 方法,因为这个方法也被内置的 set() 和 frozenset() 类型支持。

     四、字典

    a_dict={"name":"lwx","pwd":"123"}#创建一个字典
    print(a_dict)
    print "您好:",a_dict["name"]#获取字典的一个元素
    a_dict["pwd2"]="456"#为字典新增一个元素
    print "字典对象的元素数量:",len(a_dict)
    tuple=[1,2]
    a_dict["yz"]=tuple#存放元组对象的元素
    print a_dict
    a_dict.pop("yz")#删除某个元素
    print a_dict
    del a_dict['pwd2']#删除某个元素
    print a_dict
    #键值交换
    b_dict={value:key for key,value in a_dict.items()}
    print b_dict

    结果

    {'pwd': '123', 'name': 'lwx'}
    您好: lwx
    字典对象的元素数量: 3
    {'pwd': '123', 'yz': [1, 2], 'name': 'lwx', 'pwd2': '456'}
    {'pwd': '123', 'name': 'lwx', 'pwd2': '456'}
    {'pwd': '123', 'name': 'lwx'}
    {'lwx': 'name', '123': 'pwd'}

     

     

  • 相关阅读:
    删除难以删除的文件
    DLL创建与使用
    Springboot多文件上传
    解决javaweb项目启动端口号被占用
    pl/sql 导出数据库表dmp文件并导入数据库过程
    Spring Boot 静态资源处理
    Consider defining a bean of type错误
    SpringBoot+layUI上传图片功能
    jQuery改变html页面样式
    Springboot启动后默认访问页面修改
  • 原文地址:https://www.cnblogs.com/draem0507/p/2794875.html
Copyright © 2011-2022 走看看