-2

This post was not useful to me post_link

So i am asking here:

i have a python dict:

a = {'Andres':234,'Paul':345,'Andres':675}

And I have this code

def get_index(dict, a_string):
    # Variable to store the result (updated after each iteration)
    result = 0
    #Variable to append the final result of each key in the dict
    collisions =[]
    
    for a_character in a_string:
        # Convert the character to a number (using ord)
        a_number = ord(a_character)
        # Update result by adding the number
        result += a_number
    collisions.append(result)
    
    # Take the remainder of the result with the size of the data list
    list_index = result % len(data_list)
    return collisions

That returns the unicode of a string, for exemple:

get_index(teste, 'Andres') 

returns [605]

What a I want to to is tho pass my dict an for every key,value the code calculates de unicode_sum of the each string and append it to collisions:

I tried:

def get_index(dict):
    for k,v in teste.items:
          for a_character in a_string:
            # Convert the character to a number (using ord)
            a_number = ord(a_character)
            # Update result by adding the number
            result += a_number
        collisions.append(result)

What I want to get as result is:

get_index(a)

output: [605, 402, 605]

Then i can calculate the number of collision by just doing len(collision) - set(collision)

ddejohn
  • 8,775
  • 3
  • 17
  • 30
pouchewar
  • 399
  • 1
  • 10
  • Look up [`collections.Counter`](https://docs.python.org/3/library/collections.html#collections.Counter). It might make your task a whole lot easier. – RoadieRich Sep 23 '21 at 19:02
  • 2
    Python isn't going to let you have the dictionary `{'Andres':234, 'Paul':345, 'Andres':675}`. What you actually have is `{'Andres':675, 'Paul':345}`. – ddejohn Sep 23 '21 at 19:06
  • This also isn't a reliable way to calculate collisions. There's more than string whose sum of ordinals is 605. – ddejohn Sep 23 '21 at 19:08
  • It's also not really clear why you're "calculating" collisions when Python is just going to overwrite any existing keys' value if you try to add the same key to a dictionary. – ddejohn Sep 23 '21 at 19:10
  • @ddejohn I got your point. I put Andres twice just to have the same ord() result! I did ittrying to make it simple as in my hash function ( i will not use python dict) I´ll allow this to happen. – pouchewar Sep 23 '21 at 19:13
  • I just want to get both unicodes in a list. It´s all I want. – pouchewar Sep 23 '21 at 19:14
  • I think you're misunderstanding. The sum of the ordinals of the string `"dnreAs"` will *also* be 605. Does that mean you'd consider that a "collision"? – ddejohn Sep 23 '21 at 19:15
  • @ddejohn yes ! In my project Andreas and dnreAs will be treated as collision because Ill get 2 ( or more) 605 sum! – pouchewar Sep 23 '21 at 19:17
  • What does your ***actual*** input look like? Are you working with a list of strings? Again, you won't be able to use a `dict` if you want both instances of `"Andres"` to be counted. – ddejohn Sep 23 '21 at 19:21
  • I have a dict with 5000 pairs of random names: value. I am iterating each letter of each name(key) using ord(letter) and summing the result: I will get a list of 5000 integers. I want to know how many unique integers I have in this final list. And dosent matter if 2 or more strings sums to the same integer. I just want to know how many uniques. – pouchewar Sep 23 '21 at 19:28

1 Answers1

0

Since "Andres" can't be present in your dictionary twice, I'll scramble the letters of the other instance to show that you'll get both results of 605:

>>> d = {"Andres": 234, "Paul": 345, "nAsedr": 675}
>>> collisions = [sum(map(ord, s)) for s in d]
>>> collisions
[605, 402, 605]
>>> len(collisions) - len(set(collisions))
1

This will also work for a list of strings, a tuple of strings, and a set of strings (where a set will also not allow you to have more than one "Andres").

ddejohn
  • 8,775
  • 3
  • 17
  • 30
  • the names comes from this code: [https://stackoverflow.com/questions/69304159/random-generate-a-dictionary-with-random-names-random-integers/69304331#69304331] – pouchewar Sep 23 '21 at 19:35