Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learning Python and using dictionaries

Tags:

python

I'm working through exercises in Building Skills in Python, which to my knowledge don't have any published solutions.

In any case, I'm attempting to have a dictionary count the number of occurrences of a certain number in the original list, before duplicates are removed. For some reason, despite a number of variations on the theme below, I cant seem to increment the value for each of the 'keys' in the dictionary.

How could I code this with dictionaries?

dv = list()
# arbitrary sequence of numbers
seq = [2,4,5,2,4,6,3,8,9,3,7,2,47,2]

# dictionary counting number of occurances
seqDic = { }

for v in seq:
    i = 1
    dv.append(v)
    for i in range(len(dv)-1):
        if dv[i] == v:
            del dv[-1]
            seqDic.setdefault(v)
            currentCount = seqDic[v]
            currentCount += 1
            print currentCount # debug
            seqDic[v]=currentCount
print "orig:", seq
print "new: ", dv
print seqDic
like image 658
oneAday Avatar asked Dec 08 '25 10:12

oneAday


1 Answers

defaultdict is not dict (it's a subclass, and may do too much of the work for you to help you learn via this exercise), so here's a simple way to do it with plain dict:

dv = list()
# arbitrary sequence of numbers
seq = [2,4,5,2,4,6,3,8,9,3,7,2,47,2]

# dictionary counting number of occurances
seqDic = { }

for i in seq:
  if i in seqDic:
    seqDic[i] += 1
  else:
    dv.append(i)
    seqDic[i] = 1

this simple approach works particularly well here because you need the if i in seqDic test anyway for the purpose of building dv as well as seqDic. Otherwise, simpler would be:

for i in seq:
  seqDic[i] = 1 + seqDic.get(i, 0)

using the handy method get of dict, which returns the second argument if the first is not a key in the dictionary. If you like this idea, here's a solution that also builds dv:

for i in seq:
  seqDic[i] = 1 + seqDic.get(i, 0)
  if seqDic[i] == 1: dv.append(i)

Edit: If you don't case about the order of items in dv (rather than wanting dv to be in the same order as the first occurrence of item in seq), then just using (after the simple version of the loop)

dv = seqDic.keys()

also works (in Python 2, where .keys returns a list), and so does

dv = list(seqDic)

which is fine in both Python 2 and Python 3. Under the same hypothesis (that you don't care about the order of items in dv) there are also other good solutions, such as

seqDic = dict.fromkeys(seq, 0)
for i in seq: seqDic[i] += 1
dv = list(seqDic)

here, we first use the fromkeys class method of dictionaries to build a new dict which already has 0 as the value corresponding to each key, so we can then just increment each entry without such precautions as .get or membership checks.

like image 141
Alex Martelli Avatar answered Dec 09 '25 23:12

Alex Martelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!