Home › Forums › Python Programming › list data structurein python . (continued..) .. [P-3]
- This topic has 0 replies, 1 voice, and was last updated 12 years, 3 months ago by
Humble.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
Humble
KeymasterLists are one of the important data structure in python.
Lists are defined with the help of []
">>>> l=[] >>> type(l) <type 'list'> lists can hold any data type in it..
>>>> l=[1, "how", 10.0] >>> type(l) <type 'list'> >>> lists are mutable !!! That means you can change list entries/items.. Important functions .. | append(...) | L.append(object) -- append object to end |
>>>> l.append(100) >>> l [1, 'how', 10.0, 100] >>> >>> j=[1,2,"second"] >>> l.append(j) >>> l [1, 'how', 10.0, 100, [1, 2, 'second']] >>> t= (1,3) >>> type(t) <type 'tuple'> >>> l.append(t) >>> l [1, 'how', 10.0, 100, [1, 2, 'second'], (1, 3)] >>> l[4] [1, 2, 'second'] >>> l[5] (1, 3) >>> When you append **any ** item to end of list it goes as an 'entry' in short 'append' take only one argument.. :) | count(...) | L.count(value) -> integer -- return number of occurrences of value |
>>>> l [1, 'how', 10.0, 100, [1, 2, 'second'], (1, 3)] >>> l.count(1) 1 >>> l.append(1) >>> l [1, 'how', 10.0, 100, [1, 2, 'second'], (1, 3), 1] >>> l.count(1) 2 | extend(...) | L.extend(iterable) -- extend list by appending elements from the iterable
>>>> s="I am " >>> l.extend(s) >>> l [1, 'how', 10.0, 100, [1, 2, 'second'], (1, 3), 1, 'I', ' ', 'a', 'm', ' '] >>> | | index(...) | L.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value is not present.
>>>> l.index("a") 9 >>> >>> l.index("l") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 'l' is not in list >>> | | insert(...) | L.insert(index, object) -- insert object before index |>>>> l [1, 'how', 10.0, 100, [1, 2, 'second'], (1, 3), 1, 'I', ' ', 'a', 'm', ' '] >>> l.insert(2,20.0) >>> l [1, 'how', 20.0, 10.0, 100, [1, 2, 'second'], (1, 3), 1, 'I', ' ', 'a', 'm', ' '] >>> "inserting" to an index is like a left push.. | pop(...) | L.pop([index]) -> item -- remove and return item at index (default last). | Raises IndexError if list is empty or index is out of range. | To get /remove an item you can use 'pop':
>>>> l.pop(2) 20.0 >>> l [1, 'how', 10.0, 100, [1, 2, 'second'], (1, 3), 1, 'I', ' ', 'a', 'm', ' '] >>> | remove(...) | L.remove(value) -- remove first occurrence of value. | Raises ValueError if the value is not present.
>>>> l.remove('how') >>> l [1, 10.0, 100, [1, 2, 'second'], (1, 3), 1, 'I', ' ', 'a', 'm', ' '] >>> | | reverse(...) | L.reverse() -- reverse *IN PLACE*>>>> l [1, 10.0, 100, [1, 2, 'second'], (1, 3), 1, 'I', ' ', 'a', 'm', ' '] >>> l.reverse() >>> l [' ', 'm', 'a', ' ', 'I', 1, (1, 3), [1, 2, 'second'], 100, 10.0, 1] >>> To reverse a string I would do something like this :)
>>>> k="humble" >>> z=[] >>> z.extend(k) >>> z ['h', 'u', 'm', 'b', 'l', 'e'] >>> z.reverse() >>> z ['e', 'l', 'b', 'm', 'u', 'h'] >>> | | sort(...) | L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
>>>> for i in range(10): ... l.append(random.randint(1,1000)) >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 850, 575, 910, 34, 436, 268, 218, 322, 212, 235] >>> l.sort() >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 34, 212, 218, 235, 268, 322, 436, 575, 850, 910] >>> | cmp(x, y) -> -1, 0, 1
>>>> l=[1,2] >>> j=[3] >>> cmp(l,j) -1 >>> l [1, 2] >>> j [1] >>> j.append(2) >>> cmp(l,j) 0 >>>
-
AuthorPosts
Viewing 1 post (of 1 total)