index string by character
Question:
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
Answers:
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))
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'
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
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))
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'