Data Structures Flashcards

1
Q

“Type” command

A

Returns the class of the data, or the exact type if a Python object

Ex
Type(5)
Class ‘int’

Int, str, float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Sequences

A

An ordered collection of values.

Sequence types:

Strings

Tuples

Lists

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Tuple

A

A tuple is an immutable sequence of 0 or more values.

Enclosed in round brackets and separated by commas.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Tuple functions

A

X in tup. - True if x is an element in tup

Len(tup) - # of elements in tup

tup.count(x) - # of times element x occurs in tup

Tup.Index(x) - index location of first occ of x, returns ValueError exception if not in tup

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Lists

A

Lists are the same as tuples, but they are mutable- ie they can be changed.

Separated by commas, enclosed in square brackets.

Can use len, concatenate, indexing, slicing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List functions

A

Most are mutating functions, ie they modify the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

S.append (x)

A

Appends x to the end of s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does “pop” refer to?

A

The act of removing the last element of a list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does “push “ refer to?

A

Adding an element to the same end of a list (like append)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does “stack “ refer to?

A

When push and pop are used on the same list.

Items are pushed onto the top of the stack, and then popped from the top of the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

S.count(x)

A

Returns the number of times x appears in s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

S.extend(lst)

A

Appends each item of “lst” to s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

S.pop(i)

A

Removes and returns the item at index i in s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

S.index(x)

A

Returns the index value of the leftmost occurrence of x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

S.insert(i, x)

A

Inserts x before index location i (so that s[i] == x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

S.remove(x)

A

Removes the leftmost occurrence of x in s

17
Q

S.reverse()

A

Reverses the order of the elements of s

18
Q

S.sort()

A

Sorts the elements of s into increasing order

19
Q

What is lexicographical ordering?

A

The order in which Python sorts a list of sequences. This is a general term meaning “alphabetical order “. It applies to any sequence of orderable values.

20
Q

List comprehensions

A

A special notation for creating lists

> > > [n * n for n in range(1, 11):

Creates list of squares from 1-10

21
Q

Filtered comprehensions

A

List comprehensions can filter out elements you don’t want.

> > > result = [n for n in nums if n > 0]
Returns only positive numbers

22
Q

What is a Python dictionary?

A

This is an extremely efficient data structure for storing pairs of values in the form key:value

Also referred to as associative arrays, maps, or hash tables.

> > > color = {‘red’ : 1, ‘blue’ : 2, ‘green’ : 3}

‘color’ is the dictionary name, has three members, one is ‘blue : 2’, where ‘blue’ is the key and ‘2’ is the associated value.

23
Q

Key restrictions

A

Keys have to be unique. You can’t have more than one key:value pair in the same dictionary.

Keys must be immutable, values can be mutable.

24
Q

Dictionary function

d.items()

A

Returns a view of the (key, value) pairs in d

25
Q

Dictionary function

d.keys()

A

Returns a view of the keys of d

26
Q

Dictionary function

d.values()

A

Returns a view of the values in d

27
Q

Dictionary function

d,get(key)

A

Returns the value associated with key

28
Q

Dictionary function

d.pop(key)

A

Removes key and returns its corresponding value

29
Q

Dictionary function

d.popitem()

A

Returns some (key, value) pair from d

30
Q

Dictionary function

d.clear()

A

Removes all items from d

31
Q

Dictionary function

d.copy()

A

Creates a copy of d

32
Q

Dictionary function

d.fromkeys(s, t)

A

Creates a new dictionary with keys taken from s and values taken from t

33
Q

Dictionary function

d.setdefault(key, v)

A

If key is in d, returns its value; if key is not in d, returns v and adds (key, v) to d

34
Q

Dictionary function

d.update(e)

A

Adds the (key, value) pairs in e to d