• 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']
  • 相关阅读:
    HDU1720 A+B Coming
    HDU1390 ZOJ1383 Binary Numbers
    HDU1390 ZOJ1383 Binary Numbers
    HDU2504 又见GCD
    HDU2504 又见GCD
    HDU1335 POJ1546 UVA389 UVALive5306 ZOJ1334 Basically Speaking
    HDU1335 POJ1546 UVA389 UVALive5306 ZOJ1334 Basically Speaking
    HDU1020 ZOJ2478 Encoding
    HDU1020 ZOJ2478 Encoding
    HDU2097 Sky数
  • 原文地址:https://www.cnblogs.com/repo/p/5414772.html
Copyright © 2020-2023  润新知