I have a list of words with different length.
I want to find the most frequent character in each position of all words. what is the efficient way to do that and prevent the Error index out of range
in different length strings?
for example:
alist = ['fowey', 'tynemouth', 'unfortunates', 'patroness', 'puttying', 'presumptuousness', 'lustrous', 'gloxinia']
The most frequent character in each position of all words (0 to len(max(alist, key=len))
) is equal to: poternusakesness
for p in range (len(max(words, key=len))):
and what about, if two characters have the same frequenct, the first character (based on alphabet) be selected as the most frequent character?
CodePudding user response:
Try:
from collections import Counter
alist = [
"fowey",
"tynemouth",
"unfortunates",
"patroness",
"puttying",
"presumptuousness",
"lustrous",
"gloxinia",
]
for i in (
(w[idx] if idx < len(w) else None for w in alist)
for idx in range(len(max(alist, key=len)))
):
print(
min(
Counter(ch for ch in i if ch is not None).items(),
key=lambda k: (-k[1], k[0]),
)[0]
)
Prints:
p
u
t
e
r
n
u
s
a
o
e
s
n
e
s
s
EDIT: Withour collections.Counter
:
alist = [
"fowey",
"tynemouth",
"unfortunates",
"patroness",
"puttying",
"presumptuousness",
"lustrous",
"gloxinia",
]
for i in (
(w[idx] if idx < len(w) else None for w in alist)
for idx in range(len(max(alist, key=len)))
):
cnt = {}
for ch in i:
if ch is not None:
cnt[ch] = cnt.get(ch, 0) 1
print(
min(
cnt.items(),
key=lambda k: (-k[1], k[0]),
)[0]
)