Home > OS >  Store count of unique characters in a temporary variable
Store count of unique characters in a temporary variable

Time:02-17

Background: For a previous task I needed to count and store unique characters from a key in a dictionary(beginning of Rubik's cube solver)

Issue: Afterwards ive seen that the amount of keys I have are incrementing because I have stored the count in result which is later used as a return value, and was wondering how to instead store the counts in a temporary variable?

result={}
encodedCube = parms.get('cube',None)

for keys in encodedCube:
   result[keys] = result.get(keys,0)   1 

How would I go about using a temporary variable that is destroyed later?(sorry I'm very very new to Python)

Example of issue :

def test_check_020_shouldReturnOKOnSingleReturnKey(self):
parm = {'op':'check',
        'cube':'bbbbbbbbbyyyyyyyyyeeeeeeeeeoooooooootttttttttuuuuuuuuu'}
expectedResult = 1
result = check._check(parm)
status = result.get('status', None)
self.assertEqual(expectedResult, len(result))

would say

AssertionError: 1 != 7

CodePudding user response:

It looks like you're trying to do what a counter was made for. As an example,

from collections import Counter

parm = {'op':'check',
        'cube':'bbbbbbbbbyyyyyyyyyeeeeeeeeeoooooooootttttttttuuuuuuuuu'}
c = Counter(parm['cube'])
print(dict(c))

The result:

{'b': 9, 'y': 9, 'e': 9, 'o': 9, 't': 9, 'u': 9}
  • Related