Chapter 9: Dictionaries Flashcards

1
Q

difference between dictionary and list

A

dictionary is more general - maps between a set of indicies (keys) and a set of values

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

key

A

set of indicies that maps to a value

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

association of a key and a value…

A

called a key-value pair, or an item

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

turn eng2ger (english to german dictionary) into a dictionary

A

eng2ger=dict()

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

braket type of dictionary

A

{ } - curly brackets.

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

create item mapping key ‘one’ to value ‘eins’

A

eng2ger[‘one’]=’eins’

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

print(eng2ger)

A

{‘one’: ‘eins’}

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

add more

A

eng2ger[‘x’]=’y’, will just add new ones

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

creating dict with multiple items at start

A

eng2ger={‘three’: ‘drei’, ‘four’: ‘vier’} - output can be used to create new one

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

show value of a key

A

print(eng2ger[‘one’]) returns eins

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

find number of items

A

len(eng2ger), returns no. of items

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

‘in’ operator can be used to tell us if a…

A

key is in the dictionary, but not a value. eg

‘one’ in eng2ger #returns True, but ‘eins’ returns False

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

to see if a value is in dictionary…

A

use the ‘values’ method to return a list, then use the ‘in’ operator. x=list(eng2ger.values())
‘eins’ in x #returns true

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

what does c mean, as in “for c in str

A

character

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

dictionary to tally characters in a string, ie give frequency of each

A
word='brontosaurus'
d=dict()
for c in word:
  if c not in d:  <
    d[c]=1          <
  else:             <
    d[c]=d[c]+1  <

print(d)

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

‘get’ dictionary method

print(dic.get(‘key’, ‘value’))

A

prints the value of the key if key is present, otherwise prints the other value put in brackets

17
Q

use ‘get’ to make character tally more concise

A

word=’brontosaurus’
d=dict()
for c in word:
d[c]=d.get(c, 0)+1 <

print(d)

18
Q

x+=1
-=…
*=…
/=…

A

equiv to x=x+1 etc

19
Q

list all keys with values next to them

A

dic=…
for key in dic:
print(key, dic[key])

20
Q

‘keys’ dictionary method

A

makes a list of keys

lst=list(dic.keys()). can then be sorted, and looped through to report each key and its value

21
Q

do the same, but sorted alphabetically (via list)

A
dic=...
lst=list(dic.keys())
lst.sort()
for key in lst:
   print(key, dic[key])
22
Q

‘lower’ string method

make a line all lowercase

A

line=line.lower()

23
Q

‘punctuation’ string method

identify puntuation in a str

A

str.punctuation

24
Q

‘translate’ string method

A

not sure if important yet

25
Q

delete punctuation on a line with ‘translate’ string method

A

line=line.translate(line.maketrans(‘’, ‘’, string.punctuation))
first 2 parameters empty. deletes all of 3rd parameter (all punctuation present if it exists).

26
Q

swap keys with values in dictionary

A

d={value:key for key, value in d.items()}

27
Q

copy dictionary. NB, multiple values can be the same, keys can’t. so when reversed this well mess things up.

A

dict2=dict1.copy()

28
Q

long-winded way to avoid duplicate key problem above is in chap9ex3-4. May be able to avoid, as in chap9ex3-4_simpler

A

gives a way of identifying a value without knowing the key, and printing both