Puas yog daim ntawv hashable Python?
Puas yog daim ntawv hashable Python?

Video: Puas yog daim ntawv hashable Python?

Video: Puas yog daim ntawv hashable Python?
Video: Da Vang - Puas Yog Nyuab2 Koj (nkauj tawm tshiab 2021) 2024, Hlis ntuj nqeg
Anonim

Tag nrho cov Python cov immutable built-in khoom yog hashable , thaum tsis muaj cov thawv hloov pauv (xws li cov npe los yog phau ntawv txhais lus) yog. Cov khoom uas yog cov piv txwv ntawm cov neeg siv cov chav kawm txhais yog hashable los ntawm lub neej ntawd; lawv txhua tus sib piv tsis sib xws, thiab lawv tus nqi hash yog lawv tus lej ().

Ib yam li ntawd, puas yog tuples hashable hauv Python?

10.1 Tuples yog immutable Cov nqi khaws cia hauv a tuple tuaj yeem ua tau txhua yam, thiab lawv raug ntsuas los ntawm cov lej. Qhov txawv tseem ceeb yog qhov ntawd tuples yog immutable. Tuples kuj muab piv thiab hashable yog li peb tuaj yeem txheeb cov npe ntawm lawv thiab siv tuples raws li qhov tseem ceeb hauv Python phau ntawv txhais lus.

Nws txhais li cas rau ib yam dab tsi kom hashable? Ib yam khoom yog hais tias yog hashable yog nws muaj tus nqi hash uas tseem zoo ib yam thaum nws lub neej. Cov khoom uas yog piv txwv ntawm cov neeg siv cov chav kawm rhiab tau los ntawm lub neej ntawd; lawv txhua tus piv tsis sib npaug (tshwj tsis yog nrog lawv tus kheej), thiab lawv tus nqi hash yog tau los ntawm lawv ().

Xav txog qhov no, Unhashable txhais li cas Python?

TypeError: unhashable hom: 'list' feem ntau txhais tau tias tias koj tab tom sim siv ib daim ntawv teev npe raws li kev sib cav hash. Qhov no txhais tau tias uas thaum koj sim hash ib unhashable txwv nws yuav ua rau yuam kev. Rau ex. Tus txheej txheem los daws qhov teeb meem no yog pov ib daim ntawv teev npe mus rau tuple.

Puas yog cov npe hloov tsis tau hauv Python?

Txhua yam hauv Python yog ib yam khoom. Thiab dab tsi txhua tus tuaj tshiab Python yuav tsum kawm sai sai yog tias txhua yam khoom hauv Python tuaj yeem hloov tau los yog hloov tsis tau . Cov khoom ntawm hom built-in xws li (int, ntab, bool, str, tuple, unicode) yog hloov tsis tau . Cov khoom ntawm built-in hom xws li ( lis , set, dict) yog mutable.

Pom zoo: