Learning Python and Using Dictionaries

I am working on Building Skills in Python exercises that, to my knowledge, have no published solutions.

In any case, I try to have the dictionary count the number of occurrences of a certain number in the source list before duplicates are deleted. For some reason, despite a number of theme options below, I cannot increase 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
+3
source share
6 answers

defaultdict dict ( , ), 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

, if i in seqDic dv, seqDic. :

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

get of dict, , . , , dv:

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

. dv ( , dv , seq), ( )

dv = seqDic.keys()

( Python 2, .keys ),

dv = list(seqDic)

Python 2 , Python 3. ( dv) ,

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

fromkeys dict, 0 , , , .get .

+2

defaultdict :

>>> from collections import defaultdict

>>> seq = [2,4,5,2,4,6,3,8,9,3,7,2,47,2]

>>> seqDic = defaultdict(int)

>>> for v in seq:
...     seqDic[v] += 1

>>> print seqDic
defaultdict(<type 'int'>, {2: 4, 3: 2, 4: 2, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 47: 1})
+2

, . , ?

#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 = {}

### what you want to do, spelled out
for number in seq:
    if number in seqDic: # we had the number before
        seqDic[number] += 1
    else: # first time we see it
        seqDic[number] = 1

#### or:
for number in seq:
    current = seqDic.get(number, 0) # current count in the dict, or 0
    seqDic[number] = current + 1

### or, to show you how setdefault works
for number in seq:
    seqDic.setdefault(number, 0) # set to 0 if it doesnt exist
    seqDic[number] += 1 # increase by one

print "orig:", seq
print seqDic
+2

:

#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:
    if v in seqDic:
        seqDic[v] += 1
    else:
        seqDic[v] = 1

dv = seqDic.keys()

print "orig:", seq
print "new: ", dv
print seqDic

, , , . defaultdict, , , , .

+1

, Python3, collections.Counter, dict, .

>>> from collections import Counter
>>> seq = [2,4,5,2,4,6,3,8,9,3,7,2,47,2]
>>> Counter(seq)
Counter({2: 4, 3: 2, 4: 2, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 47: 1}
0
for v in seq:
    try:
        seqDic[v] += 1
    except KeyError:
        seqDic[v] = 1

, .

, , , , , .

0

Source: https://habr.com/ru/post/1727124/


All Articles