zoukankan      html  css  js  c++  java
  • 元组与序列

      元组(tuple)是与列表类似的线性数据结构,与列表结构所不同的是,元组定义中的内容不允许被修改及容量的动态扩充。

    将列表转换为元组:

    #coding:utf-8
    numbers =[1,3,5,7,9,11,13,15,]
    tuples = tuple(numbers)
    print("numbers变量的数据类型:%s" % type(numbers)) #获取列表类型
    print("tuples变量的数据类型:%s" % type(tuple)) #获取类型
    序列统计函数:
    #coding:utf-8                                    #序列统计函数
    numbers =[1,3,5,7,9,] #定义列表信息
    print("元素个数:%d" % len(numbers)) #统计序列中个数
    print("元素最大值:%d" % max(numbers)) #统计序列中最大值
    print("元素最小值:%d" % min(numbers)) #统计序列中最小值
    print("元素总和:%d" % sum(numbers)) #统计序列中总和
    print(any((True,1,"helsll"))) #判断元组内的结果
    print(all((True,None))) #判断元组中的结果

     字符串  

    字符串(str)是python中最为常用的一种数据类型,一个字符串可以理解为由若干个字符组成的序列结构,它可以使用所有操作比如:字符串分片、数据统计操作等

    字符串切片获取:

    #coding:utf-8
    title = "明天会更好:www.teday.com"
    sub_url = title[6:] #字符串切片获取
    sub_name = title[:5] #字符串切片
    print(sub_url)
    print(sub_name)

    字符串信息统计:

    #coding:utf-8
    title = "www.baidu.com"
    print("字符串长度:%d" % len(title))
    print("最大字符:%c" % max(title))
    print("最小字符:%c" % min(title))
    使用in和not in去判断一个字符串是否存在字符串中:
    #coding:utf-8
    title = "www.baidu.com"
    if "w" in title:
    print(title)

    学而不思则罔,思而不学则殆
  • 相关阅读:
    第八章 多线程编程
    Linked List Cycle II
    Swap Nodes in Pairs
    Container With Most Water
    Best Time to Buy and Sell Stock III
    Best Time to Buy and Sell Stock II
    Linked List Cycle
    4Sum
    3Sum
    Integer to Roman
  • 原文地址:https://www.cnblogs.com/linyu51/p/13699329.html
Copyright © 2011-2022 走看看