1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      python-列表list-元組-集合-字典(dict)-實例代碼

      ?

      成都創(chuàng)新互聯(lián)公司主營張北網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,App定制開發(fā),張北h5微信小程序搭建,張北網(wǎng)站營銷推廣歡迎張北等地區(qū)企業(yè)咨詢

      python 經(jīng)常用的數(shù)據(jù)類型還有列表list-元組(tuple)-集合(set)-字典(dict),以下是這四種類型用法的簡單示例。

      ?

      1.列表list? ? ? ? [? ]元素可變 , 有序

      ?

      2. 元組(tuple)? ( )?元素不可變,可訪問?

      ?

      3.集合set? { }?元素不可重復(fù),沒有索引和位置的概念,?無序

      ?

      4.?字典dict? ?{? }? key:value? ?無序

      ?

      Python 中組合數(shù)據(jù)類型-列表list-元組(tuple)-字典(di)。

      ?

      1.list [ ]

      ?

      list 有序元素集合 元素類型可不同

      1.訪問 單個訪問l=list[0]? l=[1,3,5,7,9,11,13]

      ?????? 多個訪問l[2:5]

      2.列表操作

      ?? list1+list2? 合并連接兩個列表

      ?? list1*n? ? ? ?重復(fù)n次列表內(nèi)容

      ?? len(list1)? ? 返回列表長度(元素個數(shù))

      ?? x in list1? ? 檢查元素是否在列表中

      ?? l1*l2? ? ? ? ?錯

      3.長度len(list1) len(l1)=6

      4. 檢查元素是否在列表中x in l1

      """

      def main():

      ?????# m_list=[1,3,5,'abc',9,'def',13] ??????#賦值為什么就是什么數(shù)據(jù)類型 也可以寫成 ?m_list=[]

      ??????m_list=[1,3,5,7,9,11,13] ??????#賦值為什么就是什么數(shù)據(jù)類型 也可以寫成 ?m_list=[]

      ??????print(m_list) ?????????????????#輸出[1,3,5,7,9,11,13]

      ??????print('訪問第一個元素:{}'.format(m_list[0])) ????????????????#輸出1

      ??????print('訪問最后一個元素:{}'.format(m_list[-1:])) ????????????#輸出[13]返回的是list

      ??????print('訪問最后一個元素:{}'.format(m_list[-1])) ?????????????#輸出13 ??返回的是元素

      ??????print('訪問倒數(shù)第二個元素:{}'.format(m_list[-2])) ???????????#輸出def

      ??????print('訪問2-5元素:{}'.format(m_list[2:5])) ?????????????????#輸出3=(5-2)個元素,序號從0開始 不包含5,輸出[5,'abc',9]

      ??????print('不訪問最后兩個元素:{}'.format(m_list[:-2])) ??????????# [1, 3, 5, 7, 9]

      """

      """

      ??????l1=[1,2,3]

      ??????l2=[4,5,6]

      ??????print('加相當于合并l1+l2={}'.format(l1+l2)) ?????????????#輸出[1,2,3,4,5]

      ??????print('乘相當于重復(fù)l1*2={}'.format(l1*2)) ???????????????#輸出[1,2,3,1,2,3]

      ??????print('得到列表中的元素個數(shù)len(l1)={}'.format(len(l1))) ?#輸出3

      ??????print('元素是否在列表中{}'.format(10 in l1)) ????????????#輸出False

      ??????print('元素是否在列表中{}'.format('abc' in l1)) ????????#輸出False

      ??????print('元素是否在列表中{}'.format(5 in l2)) ?????????????#輸出True

      ??????i=0

      ??????m_list=[]

      ??????while i <= 10:

      ???????????m_list.append(i)

      ???????????i+=1

      ??????print('while m_list中的所有元素{}'.format(m_list)) ?????#輸出[0,1,2,3,4,5,6,7,8,9,10]

      ??????for i in range(0, 6): ????????????????????????????????#Pop i in range(0,5)

      ???????????m_list.pop(i) ????????????????????????????????????#remove i in range(0,10)

      ??????????# m_list.remove(i)

      ???????????print('pop操作i={},m_list={}'.format(i,m_list))

      ??????"""

      """

      ??????pop操作i = 0,m_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

      ??????pop操作i = 1,m_list = [1, 3, 4, 5, 6, 7, 8, 9, 10]

      ??????pop操作i = 2,m_list = [1, 3, 5, 6, 7, 8, 9, 10]

      ??????pop操作i = 3,m_list = [1, 3, 5, 7, 8, 9, 10]

      ??????pop操作i = 4,m_list = [1, 3, 5, 7, 9, 10]

      ??????pop操作i=5,m_list=[1, 3, 5, 7, 9]

      """

      """

      ?????remove操作i=0,m_list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

      ?????remove操作i=1,m_list=[2, 3, 4, 5, 6, 7, 8, 9, 10]

      ?????remove操作i=2,m_list=[3, 4, 5, 6, 7, 8, 9, 10]

      ?????remove操作i=3,m_list=[4, 5, 6, 7, 8, 9, 10]

      ?????remove操作i=4,m_list=[5, 6, 7, 8, 9, 10]

      ?????remove操作i=5,m_list=[6, 7, 8, 9, 10]

      ?????remove操作i=6,m_list=[7, 8, 9, 10]

      ?????remove操作i=7,m_list=[8, 9, 10]

      ?????remove操作i=8,m_list=[9, 10]

      ?????remove操作i=9,m_list=[10]

      ?????remove操作i=10,m_list=[]

      ?????"""

      """

      ??????for i in range(11, 20): ??????????????????????????????????# 輸出0開始不包含10

      ???????????m_list.append(i)

      ??????print('for m_list中的所有元素{}'.format(m_list)) ????#

      """

      2.元組?tuple()

      1.元組是結(jié)構(gòu) 列表是順序
      2.元組由不同數(shù)據(jù)組成?列表由相同數(shù)據(jù)組成
      3.一旦被創(chuàng)建 元素不能被修改
      4.('red','blue','green'),(2,4,6)
      5.與list訪問相同
      6.表達固定數(shù)據(jù)項、函數(shù)多返回值

      # def main():

      # ????days_tup = (31, 'abc', 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

      # ????day=days_tup[:3]

      # ????print('訪問前三個元素{}'.format(day)) #輸出為(31 'abc' 31)

      # ????print(type(day)) ???????????????????????#返回的是元組

      # ????day = days_tup[2]

      # ????print('訪問第三個元素{}'.format(day)) ?#輸出為30

      # ????print(type(day)) ????????????????????????#返回的是int

      3.集合?set?{}
      1.無序組合
      2.元素不可重復(fù)
      3.沒有索引和位置的概念
      4.set()用于集合的生成,返回結(jié)果是一個無重復(fù)且排序任意的集合
      5.用于表示成員間的關(guān)系、元素去重

      ?

      ?

      ?

      def main():

      ????days_set1 = {1,2,3,4,5}

      ????days_set2 = {4,5,7,8,9,10,}

      ????i=0

      ????for i ?in range(i,12):

      ????????if i in days_set1:

      ????????????print('i={}在集合days_set1中'.format(i))

      ????????elif i in days_set2:

      ????????????print('i={}在集合days_set2中'.format(i))

      ????????elif i in days_set1-days_set2:

      ????????????print('i={}在days_set1中但不在days_set2中'.format(i))

      ????????elif i in days_set1 & days_set2:

      ????????????print('i={}在days_set1和days_set2交集'.format(i))

      ????????elif i in days_set1 | days_set2:

      ????????????print('i={}在days_set1 days_set2所有元素'.format(i))

      ????????else:

      ????????????print('i={}不在集合兩個集合中'.format(i))

      4.字典??增刪改查

      ?

      ?

      ?

      遍歷所有的key? for key in d.keys():

      ?

      ???????????????? Print(key)

      ?

      遍歷所有的value? for value in d.values():

      ?

      ???????????????? Print(value)

      ?

      遍歷所有的數(shù)據(jù)項 for item in d.items():

      ?

      ???????????????? Print(items)

      ?

      dict 鍵-值

      字典類型數(shù)據(jù)通過映射查找數(shù)據(jù)項

      映射: 任意鍵查找集合中的值的過程

      一個鍵對應(yīng)一個值

      字典是無序的。

      ????# 1.創(chuàng)建變量 增刪改查

      ????#2.是否在字典中

      ????d=dict() ?????????#定義一個空變量

      ????print(d)

      ????d['egg']=2.12 ????#賦值 輸出{'egg': 2.12}

      ????print(d)

      ????d['milk']=3.15 ???#增加 輸出{'egg': 2.12, 'milk': 3.15}

      ????print(d)

      ????????????????????#['1'] 和[1]不同

      ????d['1']=3.15 ???#{'egg': 2.12, 'milk': 3.15, '1': 3.15}

      ????print(d)

      ????d[1] = 3.15 ???#{'egg': 2.12, 'milk': 3.15, '1': 3.15, 1: 3.15}

      ????print(d)

      ?

      ????d['egg'] = 5.15 ??#修改{'egg': 5.15, 'milk': 3.15}

      ????print(d)

      ????del d['egg'] ?????#刪除{'milk': 3.15}

      ????print(d)

      ????b_in = 'milk' in d #是否在字典中True集合一樣

      ????function(){ //MT4手數(shù)?http://www.kaifx.cn/mt4/kaifx/1693.html

      ????print(b_in)

      ?

      ?

      ?

      def main():

      ????# 月份-天數(shù) 字典

      ????month_day_dict = {1: 31,

      ??????????????????????2: 28,

      ??????????????????????3: 31,

      ??????????????????????4: 30,

      ??????????????????????5: 31,

      ??????????????????????6: 30,

      ??????????????????????7: 31,

      ??????????????????????8: 31,

      ??????????????????????9: 30,

      ??????????????????????10: 31,

      ??????????????????????11: 30,

      ??????????????????????12: 31}

      ?

      ????day_month_dict = {30: {4, 6, 9, 11},

      ??????????????????????31: {1, 3, 5, 7, 8, 10, 12}}

      ?

      ????for month_day_key in ?month_day_dict.keys():

      ????????print("month_day_dict中的key值遍歷{}".format(month_day_key))

      ????for month_day_value in ?month_day_dict.values():

      ????????print("month_day_dict中的values值遍歷{}".format(month_day_value))

      ????for item in ?day_month_dict.items():

      ????????print("day_month_dict中的item{}".format(item))

      ????????print (type(item))

      Pass

      ?


      網(wǎng)頁名稱:python-列表list-元組-集合-字典(dict)-實例代碼
      文章分享:http://www.ef60e0e.cn/article/pcdsii.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        双鸭山市| 从化市| 武安市| 五莲县| 田阳县| 汽车| 民勤县| 道真| 青冈县| 潮州市| 镇雄县| 上栗县| 灵宝市| 沛县| 江西省| 麻江县| 郸城县| 夹江县| 华阴市| 双城市| 韩城市| 潮安县| 越西县| 东山县| 丹阳市| 泽州县| 田东县| 新泰市| 磴口县| 犍为县| 大石桥市| 马山县| 柳河县| 陆良县| 长兴县| 青海省| 建阳市| 内江市| 休宁县| 新昌县| 舟山市|