Home > OS >  Filling a dictionary using for loop in a set
Filling a dictionary using for loop in a set

Time:09-30

I have 2 strings:

s1 = "Are they here"
s2 = "yes, they are here"

I want to create a dictionary (e) that has as key the maximum number of times each shared element is present in the string that contains it the most and as value the element (i.e. the "y" is contained once in s1 and twice in s2. Therefore I want a dict that goes:

e =  {2:y} # and so on

To describe my code, I thought of creating a list (c) with all the shared elements:

c = ['r', 'e', 't', 'h', 'e', 'y', 'h', 'e', 'r', 'e', 'y', 'e', 't', 'h', 'e', 'y', 'r', 'e', 'h', 'e', 'r', 'e']

then switch it to a set to eliminate duplicates and using them as iterators:

d = {'h', 'y', 'r', 't', 'e'}

Ultimately I thought of using a for loop to fill the dict (e) by iterating every element in d and reporting the maximum times it was present.

Here's my full code please note that I don't want to use any library. Also note that the code works with dict comprehension:

def mix(s1, s2):

    c = []                      # create a var to be filled with all shared chars
    for i in s1:
        if i != " ":
            if i in s2:
                c.append(i)
    for i in s2:
        if i != " ":
            if i in s1:
                c.append(i)     # end of 1st process
    d = set(c)                  # remove duplicates
    e = {}                      # create a dict to align counting and relative char
    for i in d:
        a = s1.count(i)
        b = s2.count(i)
        m = max(a, b)
        e[m] = i
    
    # z = {i:max(s1.count(i), s2.count(i)) for i in d} this is what actually works
    
    return e # z works instead

The issue I get is that the for loop stops after 3 iteration.

CodePudding user response:

I have just realized that sets can obviously have UNIQUE values as keys, so, of course my code will be display "partially".

When it gets the same key, it overwrites it.

So using the element as key will work and the for loop can be like so:

    for i in d:
        a = s1.count(i)
        b = s2.count(i)
        m = max(a, b)
        e[i] = m

CodePudding user response:

Edit: I see that Rakshith B S has made a better version of my comment, refer to thiers.

I'll start by saying I'm an amateur, and the following can absolutely be simplified.

First, decide about capitalization, A != a, use str.lower or str.upper.

Second, switching the dictionary to be {'letter':count} would make everything easier.

Then, it would most likely be easier to create two dictionaries to count the unique letters in each string.

d1 = {}
s1 = s1.lower()
for letter in s1:
    if letter != " ":
        if letter in d1:
            d1[letter]  = 1 # if in dict, add one to count
        else:
            d1[letter] = 1 #add new letter to dict


d2 = {}
s2 = s2.lower()
for letter in s2:
    if letter != " ":
        if letter in d2:
            d2[letter]  = 1 # if in dict, add one to count
        else:
            d2[letter] = 1 #add new letter to dict

That should make two dictionaries, for loop it to compare and append the max values (this part can be made more efficiently).

d3 = {}
for let in d1:
    if let not in d2:
        d3[let] = d1.get(let)
    if let in d2:
        if d1[let] >= d2[let]:
            d3[let] = d1.get(let)
        else:
            d3[let] = d2.get(let)
for let in d2:
    if let not in d1:
        d3[let] = d2.get(let)

del d3[',']

This should at least get you on the right track.

CodePudding user response:

def mix(s1, s2):

    dict1 = dict()
    dict2 = dict()
    for i in s1:
        if i != " " and i != ",":
            if i in dict1:
                dict1[i]  = 1
            else:
                dict1[i] = 1
    for i in s2:
        if i != " " and i != ",":
            if i in dict2:
                dict2[i]  = 1
            else:
                dict2[i] = 1
    # print(dict1)
    # print(dict2)
    for key, value in dict2.items():

        if key in dict1:
            # print(f' check {key}, {value}')
            if value >= dict1[key]:
                dict1[key] = value

        else:
            dict1[key] = value
            #print(f' create {key}, {value}')
    return dict1


s1 = "eeeeaaabbbcccc"
s2 = "eeeeeaaa"
print(mix(s1, s2))

Why create a merged list and recheck against the counter set

Here I've compared values from dict1( which is s1) and dict2(again s2) and overwritten dict1 if the value is high else if its not found I've assigned it as the highest OUTPUTS:

{'e': 5, 'a': 3, 'b': 3, 'c': 4}  
  • Related