Qhov txawv ntawm Daim Ntawv Teev Npe thiab Tuple

Cov txheej txheem:

Qhov txawv ntawm Daim Ntawv Teev Npe thiab Tuple
Qhov txawv ntawm Daim Ntawv Teev Npe thiab Tuple

Video: Qhov txawv ntawm Daim Ntawv Teev Npe thiab Tuple

Video: Qhov txawv ntawm Daim Ntawv Teev Npe thiab Tuple
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Kaum ib hlis
Anonim

Qhov Tseem Ceeb Qhov txawv - Sau npe vs Tuple

Python yog ib hom lus dav dav dav dav rau qib siab programming. Nws yog ib qho yooj yim nyeem thiab kawm. Yog li ntawd, nws yog ib hom lus rau beginners pib computer programming. Python cov kev pab cuam yog ib qho yooj yim rau kev sim thiab debug. Nws yog ib hom lus siv los tsim ntau yam kev siv. Ib txhia ntawm lawv yog machine learning, computer vision, web development, network programming. Python yog siv los tsim cov algorithms los daws cov teeb meem nyuaj. Ob txoj kev khaws cov ntaub ntawv ntawm Python yog List thiab Tuple. Cov ntsiab lus ntawm cov npe tuaj yeem hloov pauv. Yog li, daim ntawv teev npe yog hloov tau. Cov ntsiab lus ntawm tuple tsis tuaj yeem hloov pauv. Yog li ntawd, ib tug tuple yog immutable. Kab lus no tham txog qhov sib txawv ntawm daim ntawv teev npe thiab tuple. Qhov sib txawv tseem ceeb ntawm daim ntawv teev npe thiab tuple yog cov npe hloov pauv tau thaum lub tuple tsis hloov pauv.

Dab tsi yog List?

Hauv cov lus programming xws li C lossis C ++, cov array yog siv los tuav cov ntsiab lus ntawm tib hom ntaub ntawv. Tab sis hauv Python List, tag nrho cov ntsiab lus yuav tsum tsis yog tib lub sijhawm. Txhua yam khoom nyob rau hauv daim ntawv yog sib cais los ntawm ib tug comma. Tag nrho cov khoom muaj nyob rau hauv cov square brackets. Ib qho piv txwv ntawm cov npe yog list1=[1, “abc”, 4.5]; Qhov Performance index ntawm ib daim ntawv pib nrog xoom. Yog li ntawd, lub caij 1 muaj qhov Performance index 0, thiab abc muaj qhov Performance index 1 thiab lwm yam. Nws tseem tuaj yeem siv qhov ntsuas tsis zoo. Cov khoom kawg ntawm cov npe muaj qhov ntsuas -1. Tom qab ntawd lub ntsiab "abc" muaj qhov ntsuas ntawm -2 thiab lwm yam.

Nws muaj peev xwm coj ib ntu ntawm cov ntsiab lus los ntawm cov npe. Qhov no yog hu ua slicing. Thaum muaj ib daim ntawv teev npe raws li nram no, uas yog list1=['a', 'b', 'c', 'd', 'e', 'f', 'g'], cov nqe lus print(list1[2: 5]) yuav sau c, d, e. Lub caij hauv qhov ntsuas ob suav nrog tab sis tsis yog lub ntsiab lus hauv qhov ntsuas tsib.

Cov npe tuaj yeem hloov tau. Yog li ntawd, cov ntsiab lus hauv cov npe tuaj yeem hloov pauv. Piv txwv tias muaj ib daim ntawv teev npe li, list1=[2, 4, 6, 8]. Yog tias tus programmer xav hloov thawj lub ntsiab lus rau tus nqi 1, ces nws tuaj yeem hloov nws los ntawm kev sau cov lus teev npe 1[0]=1. Python lus twb inbuilt functions ntxiv cov khoom tshiab rau ib daim ntawv teev npe. Nws yog lub append muaj nuj nqi. Thaum muaj cov npe xws li list1=[1, 2, 3], tus programmer tuaj yeem ntxiv cov khoom tshiab 4 siv list1.append(4).

Cov ntsiab lus ntawm cov npe tuaj yeem raug tshem tawm siv del () los ntawm kev dhau qhov ntsuas qhov cuam tshuam. Xav tias muaj ib daim ntawv teev npe1=[1, 2, 3, 4]. Cov nqe lus del(list1[2]) yuav muab 1, 2, 4. Lub caij nyob rau hauv lub thib ob Performance index yog 3. Lub caij ntawd yuav muab deleted. Thaum muaj ob lub npe xws li list1=[1, 2, 3] thiab list2=[4, 5, 6], tus programmer tuaj yeem koom nrog ob daim ntawv teev npe no siv kev sib txuas ua haujlwm ua list1+list2. Nws yuav muab cov npe ua ke [1, 2, 3, 4, 5, 6].

Muaj ntau txoj hauv kev uas muaj los daws cov haujlwm ua haujlwm. Qee qhov ntawm lawv yog ntxig (), tshem tawm (), suav () thiab lwm yam. Kev siv cov npe hauv Python yog ib qho yooj yim thaum piv rau cov arrays hauv lwm hom lus xws li C, C ++ thiab lwm yam.

Tubple yog dab tsi?

Ib tug tuple zoo ib yam li daim ntawv teev npe. Txhua yam khoom nyob rau hauv daim ntawv yog sib cais los ntawm ib tug comma. Tag nrho cov ntsiab lus suav nrog hauv kab lus. Ib tug tuple tuaj yeem muaj ntau hom ntsiab lus. Txhua lub ntsiab yog sib cais los ntawm ib tug comma. Ib qho piv txwv ntawm tuple yog tuple1=(1, 2, 3). Thawj lub caij muaj qhov ntsuas 0. Lub caij thib ob muaj qhov ntsuas 1 thiab lwm yam. Tuple kuj tuaj yeem muaj qhov tsis zoo indexing. Yog li, tus nqi 3 muaj qhov ntsuas -1. Tus nqi 2 roj qhov ntsuas -2 thiab lwm yam.

Tus programmer tuaj yeem ua raws cov ntsiab lus hauv tuple. Piv txwv tias muaj tuple, tuple1=(1, 2, 3, 4, 5). Cov nqe lus luam tawm (list1[2: 5]) yuav luam tawm 3, 4. Cov ntsiab lus hauv index ob suav nrog tab sis tsis yog lub caij hauv qhov ntsuas tsib.

Tuples yog hloov tsis tau. Yog li ntawd, cov ntsiab lus hauv daim ntawv yuav hloov tsis tau. Hloov cov ntsiab lus yuav ua rau yuam kev. Tab sis yog tias lub caij yog hom ntaub ntawv hloov tau, ces nws cov khoom nested tuaj yeem hloov pauv. Xav tias muaj ib tug tuple li tuple1=(1, 2, [3, 4]). Txawm tias qhov no yog tuple, lub caij hauv index 2 muaj ib daim ntawv teev npe. Txhawm rau hloov 1st keeb hauv cov npe ntawd rau 5, nqe lus tuple1[2][0]=5 tuaj yeem siv. Raws li cov tuple tsis hloov pauv, cov ntsiab lus tsis tuaj yeem raug tshem tawm. Tab sis siv cov haujlwm del, tag nrho tuple tuaj yeem raug tshem tawm. e.g. del (tuple1).

Qhov txawv ntawm Daim Ntawv Teev Npe thiab Tuple
Qhov txawv ntawm Daim Ntawv Teev Npe thiab Tuple

Daim duab 01: Piv txwv ntawm Daim Ntawv Teev Npe thiab Tuple

Muaj cov haujlwm muab los ntawm Python rau kev ua haujlwm tuple. Lub len () muaj nuj nqi pab nrhiav cov naj npawb ntawm cov ntsiab lus hauv ib tuple. Cov haujlwm max thiab min tuaj yeem siv los nrhiav tus nqi siab tshaj plaws thiab qhov tsawg kawg nkaus tus nqi ntawm tuple. Kev siv tuple yog txheej txheem yooj yim piv nrog cov arrays hauv lwm hom lus xws li C / C ++.

Dab tsi zoo sib xws ntawm Daim Ntawv Teev Npe thiab Tuple?

  • Ob Daim Ntawv Teev Npe thiab Tuple yog siv los khaws cov txheej txheem hauv Python.
  • Qhov ntsuas ntawm ob daim ntawv teev npe thiab tuple pib nrog xoom.
  • Txhua lub ntsiab lus sib cais los ntawm tus lej cim hauv ob Daim Ntawv Teev Npe thiab Tuple.
  • Ob Daim Ntawv Teev Npe thiab Tuple tuaj yeem muaj ntau hom ntsiab lus.
  • Daim ntawv tuaj yeem muaj cov npe nested thiab tuple tuaj yeem muaj nested tuple.
  • Ob Daim Ntawv Teev Npe thiab Tuple txhawb nqa qhov tsis zoo indexing.

Qhov txawv ntawm Daim Ntawv Teev Npe thiab Tuple yog dab tsi?

List vs Tuple

Ib daim ntawv yog hom ntaub ntawv sib xyaw hauv Python programming lus uas tuaj yeem khaws cov ntaub ntawv sib txawv thiab tuaj yeem hloov cov ntsiab lus ib zaug tsim. Ib tug tuple yog hom ntaub ntawv sib xyaw hauv Python programming lus uas tuaj yeem khaws cov ntaub ntawv sib txawv thiab tsis tuaj yeem hloov cov ntsiab lus ib zaug tsim.
Kev sib hloov
Ib daim ntawv yuav hloov tau. Nws tuaj yeem hloov tau ib zaug tsim. Ib tug tuple tsis hloov. Nws hloov tsis tau ib zaug tsim.
Cov ntsiab lus
Cov ntsiab lus ntawm daim ntawv teev npe yog nyob rau hauv cov kab lus square. Cov ntsiab lus ntawm tus tuple yog nyob rau hauv kab lus.
Iterating los ntawm cov ntsiab lus hauv ib daim ntawv teev npe tsis nrawm li hauv ib lub tuple. Iterating los ntawm cov ntsiab lus hauv ib tuple yog nrawm dua li cov npe.

Summary – List vs Tuple

Python siv Daim Ntawv Teev Npe thiab Tuple khaws cov ntaub ntawv. Daim Ntawv Teev Npe thiab tuple tuaj yeem siv los khaws cov ntaub ntawv sib txawv. Kab lus no tau tham txog qhov sib txawv ntawm Daim Ntawv Teev Npe thiab Tuple. Cov ntsiab lus hauv ib daim ntawv tuaj yeem hloov pauv. Yog li, daim ntawv teev npe yog hloov tau. Cov ntsiab lus hauv tuple tsis tuaj yeem hloov pauv. Yog li ntawd, ib tug tuple yog immutable. Qhov txawv ntawm daim ntawv teev npe thiab tuple yog tias daim ntawv teev npe hloov pauv tau thaum lub tuple tsis hloov pauv.

Download PDF of List vs Tuple

Koj tuaj yeem rub tawm PDF version ntawm kab lus no thiab siv nws rau offline hom phiaj raws li daim ntawv ceeb toom. Thov rub tawm PDF version ntawm no: Qhov Sib txawv Ntawm Cov Npe thiab Tuple

Pom zoo: