#元组(tuple) -------------------tuple_create.py---------------------- #!/usr/bin/python # -*- coding: UTF-8 -*- tuple = ("apple", "banana", "grape", "orange") #tuple[0] = "a" t = ("apple",) t = () print tuple[-1] print tuple[-2] #print t[0] -------------------tuple_use.py---------------------- #!/usr/bin/python # -*- coding: UTF-8 -*- tuple = ("apple", "banana", "grape", "orange") print tuple[-1] print tuple[-2] tuple2 = tuple[1:3] tuple3 = tuple[0:-2] tuple4 = tuple[2:-2] print tuple2 print tuple3 print tuple4 fruit1 = ("apple", "banana") fruit2 = ("grape", "orange") tuple = (fruit1, fruit2) print tuple print "tuple[0][1] =",tuple[0][1] print "tuple[1][1] =",tuple[1][1] #print tuple[1][2] #打包 tuple = ("apple", "banana", "grape", "orange") #解包 a, b, c, d = tuple print a, b, c, d -------------------tuple_loop.py---------------------- #!/usr/bin/python # -*- coding: UTF-8 -*- #使用range()循环遍历 tuple = (("apple", "banana"),("grape", "orange"),("watermelon",),("grapefruit",)) for i in range(len(tuple)): print "tuple[%d] :" % i, "" , for j in range(len(tuple[i])): print tuple[i][j], "" , print #使用map()循环遍历 k = 0 for a in map(None,tuple): print "tuple[%d] :" % k, "" , for x in a: print x, "" , print k += 1
讯享网
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/99693.html