• Python元组部分源码分析,元组和列表是可以相互转换的


    1 def count(self, value): # real signature unknown; restored from __doc__
    2         """ T.count(value) -> integer -- return number of occurrences of value """
    3         return 0
    1 练习1、统计元组中元素出现次数
    2 aa = ("rhel",34,"fdsf","rhel","dsss",456,"rhel")
    3 k = aa.count("rhel")      #计算元组中指定元素出现的个数
    4 print(k)
    5 #执行结果:
    6 3
    1 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
    2         """
    3         T.index(value, [start, [stop]]) -> integer -- return first index of value.
    4         Raises ValueError if the value is not present.
    5         """
    6         return 0
    1 #练习2、找出指定元素的索引位置
    2 aa = ("rhel",34,"fdsf","rhel","dsss",456,"rhel")
    3 k = aa.index("rhel")      #寻找指定元组元素的索引位置,有重复则输出最前面的索引位置
    4 print(k)
    5 #执行结果:
    6 0
     1 #练习3、元组与列表的转换
     2 li = [11,22,33,"wer","are",]
     3 aa = ("rhel",34,"fdsf","rhel","dsss",456,"rhel")
     4 print(type(li))      #列表的类是list,列表的所有功能都在类里面
     5 print(type(aa))      #元组的类是tuple
     6 print(tuple(li))     #元组和列表是可以相互转换的
     7 print(list(aa))
     8 #执行结果:
     9 <class 'list'>
    10 <class 'tuple'>
    11 (11, 22, 33, 'wer', 'are')
    12 ['rhel', 34, 'fdsf', 'rhel', 'dsss', 456, 'rhel']
  • 相关阅读:
    HDU
    HDU
    西电OJ
    西电OJ
    西电OJ
    USACO 2.1-Healthy Holsteins
    USACO 2.1-Sorting a Three-Valued Sequence
    HDU
    UVA
    codeforces 811A Vladik and Courtesy
  • 原文地址:https://www.cnblogs.com/repo/p/5414772.html
Copyright © 2020-2023  润新知