Home > Net >  Finding words of the same length
Finding words of the same length

Time:01-20

I am new to programming and I am trying to figure out this problem. I have a list of elements and I want to find words that have the same length. This is what I've tried:

list1 = ["dog","cat","people","tank","pop","joop","count"]    
list3 = []

for i in range(len(list1)):   
    for j in range(len(list1):    
        if len(list1[i]) == len(list1[j]):    
            list3.append(list[i])
return list3

I want to return list3 = [ "dog","cat","joop","tank"] because each word in this last has the same length as at least one other word in the list.

CodePudding user response:

If we understand your question by now, you do want to group all same-size words? IF so, then you could try this defaultdict from collections module:

If this is not what you expect, then please make the goal clear.


L = ['dog', 'cat', 'bike', 'book', 'packet']

from collections import defaultdict

ddc = defaultdict(list)

for item in L:
    size = len(item)          # find the size of each word
    ddc[size].append(item)    # then group them together by size

    
print(ddc)
defaultdict(<class 'list'>, {3: ['dog', 'cat'], 4: ['bike', 'book'], 6: ['packet']})

CodePudding user response:

My advice for situations like this is to use a map rather than using a list:

list1 = ["dog","cat","people","tank","pop","joop","count"]     
results = {}
for item in list1:
    length = len(item)
    if length in results:
        results[length].append(item)
    else:
        results[length] = [item]

print(results) # {3: ['dog', 'cat', 'pop'], 6: ['people'], 4: ['tank', 'joop'], 5: ['count']}

This works by iterating over all words in list1, getting the number of characters in each and adding it to the dictionary entry associated with that length. You can then filter that dictionary for entries that contain more than one word:

import itertools
filtered = list(itertools.chain(*filter(lambda v: len(v) > 1, results.values())))
print(filtered) # ['dog', 'cat', 'pop', 'tank', 'joop']

This code works by calling the filter function with a lambda that checks if the value associated with each key (length) has more than one value, and then chaining these sub-lists together into a single list, which is returned. For more information on how itertools.chain works and why I included the *, see this answer.

CodePudding user response:

If it is possible the same word appears multiple times, you might choose this solution:

def tryit():
    list1 = ["dog","cat","people","tank","pop","joop","count"]    
    mydict = {}
    l = len(list1)
    for i in range(l):
        vi = list1[i]
        li = len(vi)
        for j in range(l):
            vj=list1[j]
            lj = len(vj)   
            if li == lj and i != j :
                if li in mydict:
                    additem(mydict[li],vi)
                    additem(mydict[li],vj)
                else:        
                    mydict[li] = [vi,vj]
    return mydict

def additem(list,v):
    if v in list:
        return # don't place duplicates in list
    else:
        list.append(v)
    # This depends on the fact that list is mutable, so 
    # the passed list is updated. 
    
if __name__ == '__main__':
    print(tryit())    enter code here

Result: {3: ['dog', 'cat', 'pop'], 4: ['tank', 'joop']}

  • Related