-6

I need to determine the most frequent character of a list of strings by each index in Python.

Example

list1 = ['one', 'two', 'twin', 'who']

the most frequent character between all the strings at index 0 is 't' the most frequent character between all the strings at index 1 is 'w' the most frequent character between all the strings at index 2 is 'o' the most frequent character between all the strings at index 3 is 'n'(because there is only one character at the index 3

from that I want to create a final string 'twon' which shows the most frequent character of all the strings by each index

how can I perform this on python without importing any library? thanks

  • Please share the code you have tried with, then we can help you troubleshoot any issues you might have with it. – Cow Nov 11 '22 at 08:01

2 Answers2

3
from itertools import zip_longest

list1 = ['one', 'two', 'twin', 'who']

chars = {}
for i, item in enumerate(zip_longest(*list1)):
    set1 = set(item)
    if None in set1:
        set1.remove(None)
    chars[i] = max(set1, key=item.count)

Without importing any library:

list1 = ['one', 'two', 'twin', 'who']

width = len(max(list1, key=len))

chars = {}

for i, item in enumerate(zip(*[s.ljust(width) for s in list1])):
    set1 = set(item)
    if ' ' in set1:
        set1.remove(' ')
    chars[i] = max(set1, key=item.count)

Output:

chars

{
  0: 't',
  1: 'w',
  2: 'o', 
  3: 'n'
}

"".join(chars.values())

'twon'
Hamid Rasti
  • 813
  • 1
  • 6
  • 16
  • How can I perform the same without importing any library? thanks – nuovo2000ita Nov 11 '22 at 09:08
  • @nuovo2000ita I added the `without importing` version! – Hamid Rasti Nov 11 '22 at 10:58
  • You could also use your original solution with `for i, item in enumerate(zip(*[s.ljust(width) for s in list1])):`, with `width = max(len(s) for s in list1`) and then `None` replaced with `" "`. – Timus Nov 11 '22 at 11:33
  • @Timus Good idea! I edited my code. also, we can use `map` instead of list comprehension like this: `map(lambda s: s.ljust(width), list1)` – Hamid Rasti Nov 11 '22 at 11:59
  • this method looks great, I just would like to get it faster on the execution. Do I have to change something on the code you posted on the solution? thanks – nuovo2000ita Nov 12 '22 at 08:49
  • @nuovo2000ita You can use a `map without lambda!` instead of `list comprehension`. it can be faster: [list-comprehension-vs-map](https://stackoverflow.com/questions/1247486/list-comprehension-vs-map) – Hamid Rasti Nov 12 '22 at 09:16
  • how would you write the code without lambda in the example above? – nuovo2000ita Nov 12 '22 at 10:44
  • I need to have time and get some benchmarks from the code. but, based on [this SO answer](https://stackoverflow.com/a/70507442/11100417): you can replace `zip(*[s.ljust(width) for s in list1])` with `zip(*map(lambda s: s.ljust(width), list1))` and get better execution time. @nuovo2000ita – Hamid Rasti Nov 13 '22 at 11:47
0
from collections import Counter

list1 = ['one', 'two', 'twin', 'who']

idx =0  #t 
print(Counter(list(zip(*[i for i in list1 if len(i)>3]))[idx]).most_common()[0][0])

without importing any libraries

list1 = ['one', 'two', 'twin', 'who']

idx =0  #t 
l = list(list(zip(*[i for i in list1 if len(i)>3]))[idx])
print(max(set(l), key = l.count))
Dmitriy Neledva
  • 867
  • 4
  • 10