1 / 18

Python Data Structures

Python Data Structures. Jugal Lodaya. Lists. list.index (x) list.count (x ) list.sort () list.reverse () list.copy (). list.append (x ) list.extend (L ) list.insert ( i , x ) list.remove (x ) list.pop ([ i ]) list.clear (). Lists (example). >>> a.remove (333) >>> a

Download Presentation

Python Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Data Structures Jugal Lodaya

  2. Lists • list.index(x) • list.count(x) • list.sort() • list.reverse() • list.copy() • list.append(x) • list.extend(L) • list.insert(i, x) • list.remove(x) • list.pop([i]) • list.clear()

  3. Lists (example) >>> a.remove(333) >>> a [66.25, -1, 333, 1, 1234.5, 333] >>> a.reverse() >>> a [333, 1234.5, 1, 333, -1, 66.25] >>> a.sort() >>> a [-1, 1, 66.25, 333, 333, 1234.5] >>> a = [66.25, 333, 333, 1, 1234.5] >>> print(a.count(333), a.count(66.25), a.count('x')) 2 1 0 >>> a.insert(2, -1) >>> a.append(333) >>> a [66.25, 333, -1, 333, 1, 1234.5, 333] >>> a.index(333) 1

  4. Lists as Stacks (last-in, first-out) >>> stack [3, 4, 5, 6] >>> stack.pop() 6 >>> stack.pop() 5 >>> stack [3, 4] >>> stack = [3, 4, 5] >>> stack.append(6) >>> stack.append(7) >>> stack [3, 4, 5, 6, 7] >>> stack.pop() 7

  5. Lists as Queues (first-in, first-out) >>> from collections import deque >>> queue = deque(["Eric", "John", "Michael"]) >>> queue.append("Terry") # Terry arrives >>> queue.append("Graham") # Graham arrives >>> queue.popleft() # The first to arrive now leaves 'Eric' >>> queue.popleft() # The second to arrive now leaves 'John' >>> queue # Remaining queue in order of arrival deque(['Michael', 'Terry', 'Graham'])

  6. Lists Comprehensions • List comprehensions provide a concise way to create lists. Common applications • make new lists where each element is the result of some operations applied to each member of another sequence or iterable • create a subsequence of those elements that satisfy a certain condition.

  7. Lists Comprehensions

  8. Nested List Comprehensions

  9. The del Statement >>> a = [-1, 1, 66.25, 333, 333, 1234.5] >>> del a[0] >>> a [1, 66.25, 333, 333, 1234.5] >>> del a[2:4] >>> a [1, 66.25, 1234.5] >>> del a[:] >>> a [] del can also be used to delete entire variables: >>> >>> del a

  10. Tuples and Sequences

  11. Tuple packing and Sequence unpacking The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible: >>> >>> x, y, z = t This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side.

  12. Sets

  13. Dictionaries • sometimes found in other languages as “associative memories” or “associative arrays”. • Indexed by keys instead of a range of value, which can be any immutable type • Tuples can be used as keys if they contain only strings, numbers, or tuples • if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can’t use lists as keys, since lists can be modified • unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {} • The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:valuepair with del. • Storing with used keys causes replacement.

  14. Dictionaries

  15. Looping Techniques

  16. More on Conditions • In, not in – check if value is in a sequence • a<b==c checks if a is less than b and if b equals c • not, and, and or (in order from highest to lowest priority) • and/or are “short-circuit” operators • A and B and C --- if B is false, it doesn’t check C • >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance' • >>> non_null = string1 or string2 or string3 • >>> non_null • 'Trondheim'

  17. Comparing Sequences and Other Types • Lexicographical ordering • First two items are compares, then second two, then third two, etc • First items can determine the order of the entire type • (1, 2, 3) < (1, 2, 4) • [1, 2, 3] < [1, 2, 4] • 'ABC' < 'C' < 'Pascal' < 'Python' • (1, 2, 3, 4) < (1, 2, 4) • (1, 2) < (1, 2, -1) • (1, 2, 3) == (1.0, 2.0, 3.0) • (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)

  18. presentation.end()

More Related