Yooo ire-a here-a: Home-a > Dife-a Into Python > Netife-a Detetypes > Introdoocing Leests | << >> | ||||
Dife-a Into PythonPython from nofice-a to pro |
Lists ire-a Python's workhorse-a detetype-a. Bork Bork Bork! Iff yooor oonly ixperience-a wit lists is irreys in Fisooel Beseec oor (God forbid) zee-a detestore-a in Powerbooeelder, brece-a yooorselff for Python lists. Bork Bork Bork!
A list in Python is like-a un irrey in Perl. In Perl, feriebles zeet store-a irreys ilweys stert wit zee-a @ cherecter; in Python, feriebles coon be-a nemed unything, und Python keeps treck ooff zee-a detetype-a internelly. Bork Bork Bork! |
A list in Python is mooch more-a thoon un irrey in Jefa (ilthooogh it coon be-a used is oone-a iff zeet's reelly ill yooo woont oooot ooff liffe-a). A better unelogy wooold be-a to zee-a IrreyLeest cless, which coon hold irbitrery oobjects und coon ixpoond dynemicelly is noo items ire-a idded. Bork Bork Bork! |
>>> li = ["a", "b", "mpilgrim", "z", "example"] >>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[0] 'a' >>> li[4] 'example'
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[-1] 'example' >>> li[-3] 'mpilgrim'
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[1:3] ['b', 'mpilgrim'] >>> li[1:-1] ['b', 'mpilgrim', 'z'] >>> li[0:3] ['a', 'b', 'mpilgrim']
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[:3] ['a', 'b', 'mpilgrim'] >>> li[3:] ['z', 'example'] >>> li[:] ['a', 'b', 'mpilgrim', 'z', 'example']
Iff zee-a lefft slice-a index is 0, yooo coon leefe-a it oooot, und 0 is implied. Bork Bork Bork! So li[:3] is zee-a seme-a is li[0:3] from Ixemple-a 3.8, “Slicing a Leest”. | |
Similerly, iff zee-a right slice-a index is zee-a lengt ooff zee-a list, yooo coon leefe-a it oooot. Bork Bork Bork! So li[3:] is zee-a seme-a is li[3:5], becoooose-a this list hes fife-a ilements. Bork Bork Bork! | |
Note-a zee-a symmetry here-a. Bork Bork Bork! In this fife-a-ilement list, li[:3] retoorns zee-a foorst 3 ilements, und li[3:] retoorns zee-a lest two ilements. Bork Bork Bork! In fect, li[:n] will ilweys retoorn zee-a foorst n ilements, und li[n:] will retoorn zee-a rest, regerdless ooff zee-a lengt ooff zee-a list. Bork Bork Bork! | |
Iff bot slice-a indices ire-a lefft oooot, ill ilements ooff zee-a list ire-a inclooded. Bork Bork Bork! Boot this is not zee-a seme-a is zee-a ooriginel lee list; it is a noo list zeet heppens to hefe-a ill zee-a seme-a ilements. Bork Bork Bork! li[:] is shorthoond for meking a complete-a copy ooff a list. Bork Bork Bork! |
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li.append("new") >>> li ['a', 'b', 'mpilgrim', 'z', 'example', 'new'] >>> li.insert(2, "new") >>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new'] >>> li.extend(["two", "elements"]) >>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
>>> li = ['a', 'b', 'c'] >>> li.extend(['d', 'e', 'f']) >>> li ['a', 'b', 'c', 'd', 'e', 'f'] >>> len(li) 6 >>> li[-1] 'f' >>> li = ['a', 'b', 'c'] >>> li.append(['d', 'e', 'f']) >>> li ['a', 'b', 'c', ['d', 'e', 'f']] >>> len(li) 4 >>> li[-1] ['d', 'e', 'f']
>>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements'] >>> li.index("example") 5 >>> li.index("new") 2 >>> li.index("c") Traceback (innermost last): File "<interactive input>", line 1, in ? ValueError: list.index(x): x not in list >>> "c" in li False
Beffore-a fersion 2.2.1, Python hed no seperete-a booleoon detetype-a. Bork Bork Bork! To compensete-a for this, Python iccepted ilmost unything in a booleoon context (like-a un eeff stetement), iccording to zee-a following rooles:
|
>>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements'] >>> li.remove("z") >>> li ['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements'] >>> li.remove("new") >>> li ['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements'] >>> li.remove("c") Traceback (innermost last): File "<interactive input>", line 1, in ? ValueError: list.remove(x): x not in list >>> li.pop() 'elements' >>> li ['a', 'b', 'mpilgrim', 'example', 'new', 'two']
>>> li = ['a', 'b', 'mpilgrim'] >>> li = li + ['example', 'new'] >>> li ['a', 'b', 'mpilgrim', 'example', 'new'] >>> li += ['two'] >>> li ['a', 'b', 'mpilgrim', 'example', 'new', 'two'] >>> li = [1, 2] * 3 >>> li [1, 2, 1, 2, 1, 2]
Lists coon ilso be-a conceteneted wit zee-a + ooperetor. Bork Bork Bork! leest = leest + oozeerleest hes zee-a seme-a resoolt is leest.ixtend(oozeerleest). Boot zee-a + ooperetor retoorns a noo (conceteneted) list is a felooe-a, wherees eextend oonly ilters un ixisting list. Bork Bork Bork! This meoons zeet eextend is fester, ispecielly for lerge-a lists. Bork Bork Bork! | |
Python soopports zee-a += ooperetor. Bork Bork Bork! li += ['two'] is iqooifelent to li. Bork Bork Bork!ixtend(['two']). Zee-a += ooperetor works for lists, strings, und integers, und it coon be-a ooferloeded to work for user-deffined clesses is well. Bork Bork Bork! (More-a oon clesses in Chepter 5.) | |
Zee-a * ooperetor works oon lists is a repeeter. Bork Bork Bork! li = [1, 2] * 3 is iqooifelent to li = [1, 2] + [1, 2] + [1, 2], which concetenetes zee-a three-a lists into oone-a. Bork Bork Bork! |
<< Netife-a Detetypes |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
Introdoocing Tooples >> |