Given 2 strings on a line divided by a comma, with each string containing various words separated by underscores, reutrn the number of words in the first string that have the same length (in charaters as any one or more words in the second string
In regards to this I have only been able to get the length of complete full strings but not the length of specific words in the string and then compare it to another. Looking for guidance.
CodePudding user response:
I don't really understand the question without an example and the code, but as far as I understood: Given your example as follows:
x = "hello_i_am_here,what_are_you_doing"
you can do the following to split the 2 strings that are seperated with commas:
my_list = x.split(",")
Now
print(my_list)
Outputs
['hello_i_am_here', 'what_are_you_doing']
Then you can work on each element in the list seperately as following:
my_list2 = my_list[0].split("_")
This will return
['hello', 'i', 'am', 'here']
By counting the list elements you will have
len(my_list2)
which will be
3
CodePudding user response:
What I understand from your question is that
from collections import Counter
a = 'abd_anad_amdad_amdd_aadkbdakd'
b = 'add_ass_add_adad'
a_d = Counter(map(len, a.split("_")))
b_d = Counter(map(len, b.split("_")))
sum(a_d[word_len] for word_len in a_d if b_d.get(word_len))
# output : 3
That is in first string we have 3 words that have same length in second string
In this case abd
, anad
and amdd
are of 3,4,4 lengths and same matching length is in second string as add
, ass
, add
, adad
(length: 3,3,3,4)
So 3
is the total number of words in first string with length same from second string.