国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python學(xué)習(xí)筆記:List、Tuple、for循環(huán)

這篇具有很好參考價(jià)值的文章主要介紹了Python學(xué)習(xí)筆記:List、Tuple、for循環(huán)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

1.list

list_demo = [7, 7, 8, 9, 9, 9]
print(list_demo.index(7))       # index 方法返回第一個(gè)index
list_demo.sort()                # 排序list
list_demo.reverse()             # 倒序list
list_demo1 = list_demo.copy()   # 復(fù)制list

Python學(xué)習(xí)筆記:List、Tuple、for循環(huán),Python,學(xué)習(xí),筆記?

2.matrix

其實(shí)就是list嵌套,可以使用雙重for遍歷,所包含方法與list一致

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[0][0])
for row in matrix:
    for i in row:
        print(i)
1
1
2
3
4
5
6
7
8
9

Process finished with exit code 0

3.for循環(huán)實(shí)例:隨機(jī)生成一個(gè)固定大小的list,并找到list中最大的數(shù)和對(duì)應(yīng)的index

import random
list_demo = []
for i in range(10):
    list_demo.append(random.randint(0, 100))
max_num = list_demo[0]
print(list_demo)
for i in list_demo:
    if i >= max_num:
        max_num = i
print(f"Max_Num = list_demo[{list_demo.index(max_num)}] = {max_num}")

4.for循環(huán)實(shí)例:刪除list中重復(fù)的元素

list_num = [1, 1, 2, 3, 4, 5, 3, 1]
list_copy = []
for i in list_num:
    if i not in list_copy:
        list_copy.append(i)
print(list_copy)

?5.tuple

tuple不可變,但是可以多個(gè)tuple拼接組合

tuple_demo = (1, 2, 3, 1, 1)        # tuple不可變
tuple_demo.index(1)
tuple_demo.count(1)
coordinates = (1, 2, 3)
x, y, z = coordinates               # python優(yōu)勢(shì),string\list\tuple等都可以
msg = 'string'
a, b, c = msg[0:3]
print()

6.dictionary

{key:value}

dic_demo = {"name": "abc", "age": 25, "is_verified": True, "birth_year": 1998}
print(dic_demo["name"])
print(dic_demo.get("age"))
print(dic_demo.get("1sad4"))
print(dic_demo.get("birth_year"))
print(dic_demo)
abc
25
None
1998
{'name': 'abc', 'age': 25, 'is_verified': True, 'birth_year': 1998}

Process finished with exit code 0

?7.dictionary application: Translate the input number into English

input默認(rèn)是string,可以直接遍歷string建立string:string的dictionary,也可以將input變?yōu)閕nt,建立int:string的dictionary

define的method是將數(shù)字拆分單個(gè)數(shù)的組成的list文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-630353.html

dic_demo = {
    1: "one",
    2: "two",
    3: "three",
    4: "four",
    5: "five",
    6: "six",
    7: "seven",
    8: "eight",
    9: "nine",
    0: "zero"
}
dic_demo1 = {
    "1": "one",
    "2": "two",
    "3": "three",
    "4": "four",
    "5": "five",
    "6": "six",
    "7": "seven",
    "8": "eight",
    "9": "nine",
    "0": "zero"
}
string_num = input("Please input the numbers:")


# string方法
string_ans = ""
for i in string_num:
    string_ans = string_ans+dic_demo1[i]+" "
print(string_ans)


def nums_to_single(nums_demo):
    nums_ans = []
    if nums_demo == 0:
        nums_ans.append(0)
    while nums_demo > 0:
        nums_ans.append(nums_demo % 10)
        nums_demo = int(nums_demo / 10)
    nums_ans.reverse()
    return nums_ans


# int方法
string_ans1 = ""
int_num = int(string_num)
num_list = nums_to_single(int_num)
for i in num_list:
    string_ans1 = string_ans1+dic_demo[i]+" "
print(string_ans1)

到了這里,關(guān)于Python學(xué)習(xí)筆記:List、Tuple、for循環(huán)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀(guān)點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Linux shell編程學(xué)習(xí)筆記17:for循環(huán)語(yǔ)句

    Linux shell編程學(xué)習(xí)筆記17:for循環(huán)語(yǔ)句

    Linux Shell 腳本編程和其他編程語(yǔ)言一樣,支持算數(shù)、關(guān)系、布爾、字符串、文件測(cè)試等多種運(yùn)算,同樣也需要進(jìn)行根據(jù)條件進(jìn)行流程控制,提供了if、for、while、until等語(yǔ)句。? 之前我們探討了if語(yǔ)句,現(xiàn)在我們來(lái)探討for循環(huán)語(yǔ)句。 Linux Shell中的for語(yǔ)句十分靈活,格式多樣,我

    2024年02月06日
    瀏覽(26)
  • Python列表(list)、元組(tuple)和字典(dictionary)的區(qū)別

    Python列表(list)、元組(tuple)和字典(dictionary)的區(qū)別

    目錄 列表(list) ?訪(fǎng)問(wèn)列表元素 ?更新和刪除列表元素 元組(tuple) 元組的訪(fǎng)問(wèn)、更新、刪除操作? 字典(dictionary)? 創(chuàng)建空字典 修改、刪除字典 總結(jié) 列表(list)、元組(tuple)和字典(dictionary)都是序列,序列都是由索引和元素組成。遍歷和訪(fǎng)問(wèn)都按照如下格式: 具

    2023年04月13日
    瀏覽(22)
  • SOP/詳解*和**/python數(shù)據(jù)結(jié)構(gòu)(iter,list,tuple,dict)/ 解包

    SOP/詳解*和**/python數(shù)據(jù)結(jié)構(gòu)(iter,list,tuple,dict)/ 解包

    1 . ==== combined_seq.named_children() 2 . isinstance 2th parameter : must be a type or tuple of types ==== 改為tuple,不要用列表。改為 LLLayer = (nn.Conv2d,nn.Linear) 3 . File “test.py”, line 90, in calculate_fin_fout print(“hi”, seq[k].weight.shape[0], layers_fin_fout[“fout”][module]) KeyError: ScaledNeuron( (neuron): IFNode( v_thres

    2024年02月14日
    瀏覽(22)
  • 拋棄for循環(huán)遍歷list

    Java 8 API添加了一個(gè)新的抽象稱(chēng)為流Stream,可以讓你以一種聲明的方式處理數(shù)據(jù)。 Stream 使用一種類(lèi)似用 SQL 語(yǔ)句從數(shù)據(jù)庫(kù)查詢(xún)數(shù)據(jù)的直觀(guān)方式來(lái)提供一種對(duì) Java 集合運(yùn)算和表達(dá)的高階抽象。 filter filter:過(guò)濾,就是過(guò)濾器,符合條件的通過(guò),不符合條件的過(guò)濾掉 map map:映射,

    2024年02月20日
    瀏覽(15)
  • 微信小程序開(kāi)發(fā)學(xué)習(xí)筆記——3.4for循環(huán)列表渲染的用法

    微信小程序開(kāi)發(fā)學(xué)習(xí)筆記——3.4for循環(huán)列表渲染的用法

    跟著b站up主“咸蝦米_”學(xué)習(xí)微信小程序開(kāi)發(fā)中,把學(xué)習(xí)記錄存到這方便后續(xù)查找。 課程連接:https://www.bilibili.com/video/BV19G4y1K74d?p=18vd_source=9b149469177ab5fdc47515e14cf3cf74 https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/list.html data.js文件的data部分:? data.wxml文件中添加如下代碼:

    2024年02月22日
    瀏覽(25)
  • Python數(shù)據(jù)容器(列表list、元組tuple、字符串str、字典dict、集合set)詳解

    相關(guān)介紹: 一種可以容納多份數(shù)據(jù)的數(shù)據(jù)類(lèi)型,容納的每一份數(shù)據(jù)稱(chēng)之為一個(gè)元素。每一個(gè)元素,可以是任意類(lèi)型的數(shù)據(jù) 分為五類(lèi): 列表[list]、元組(tuple)、字符串(str)、集合{set}、字典{dict} 相應(yīng)區(qū)別: 列表 元祖 字符串 集合 字典 元素?cái)?shù)量 多個(gè) 多個(gè) 多個(gè) 多個(gè) 多個(gè) 元素類(lèi)

    2024年02月11日
    瀏覽(40)
  • Python錯(cuò)誤解決:list indices must be integers or slices, not tuple

    Python錯(cuò)誤解決:list indices must be integers or slices, not tuple 在Python編程中,我們經(jīng)常會(huì)遇到代碼運(yùn)行時(shí)出現(xiàn)錯(cuò)誤的情況。其中,\\\"list indices must be integers or slices, not tuple\\\"是一種常見(jiàn)的錯(cuò)誤類(lèi)型。它通常發(fā)生在使用列表時(shí),我們將元組作為索引值傳遞給列表時(shí)會(huì)出現(xiàn)這個(gè)錯(cuò)誤。 該錯(cuò)

    2024年02月11日
    瀏覽(27)
  • 【Python】進(jìn)階學(xué)習(xí):列表推導(dǎo)式如何使用兩個(gè)for循環(huán)

    【Python】進(jìn)階學(xué)習(xí):列表推導(dǎo)式如何使用兩個(gè)for循環(huán)

    【Python】進(jìn)階學(xué)習(xí):列表推導(dǎo)式如何使用兩個(gè)for循環(huán) ?? 個(gè)人主頁(yè):高斯小哥 ?? 高質(zhì)量專(zhuān)欄:Matplotlib之旅:零基礎(chǔ)精通數(shù)據(jù)可視化、Python基礎(chǔ)【高質(zhì)量合集】、PyTorch零基礎(chǔ)入門(mén)教程?? 希望得到您的訂閱和支持~ ?? 創(chuàng)作高質(zhì)量博文(平均質(zhì)量分92+),分享更多關(guān)于深度學(xué)習(xí)、

    2024年03月17日
    瀏覽(35)
  • 任何時(shí)候都不要在 for 循環(huán)中刪除 List 集合元素?。?!

    首先說(shuō)結(jié)論:無(wú)論什么場(chǎng)景,都不要對(duì)List使用for循環(huán)的同時(shí),刪除List集合元素,因?yàn)檫@么做就是不對(duì)的。 阿里開(kāi)發(fā)手冊(cè)也明確說(shuō)明禁止使用foreach刪除、增加List元素。 正確刪除元素的方式是使用迭代器(Iterator),代碼如下: JDK8后lambda寫(xiě)法: 不想知道為什么不能使用for循

    2023年04月15日
    瀏覽(27)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包