How to combine similar characters in a Python List
Given a list or string, the task is to combine all similar characters of the list or string and print in a list. First, find the frequency of all characters and store in a dictionary with key as the character and its value as the frequency in a given list or string.
For that, we have a few methods,
- Native approach.
- By using dict.get().
- By using the counter.
Method 1:
- Simply start iteration through the list and form a key in a dictionary of newly occurred character or if a character is already occurred then increase its value by 1.
- Then separates the key and values from this dictionary and store in the other two V, K lists.
- By using the zip() built-in function for mapping the values from V to K.
- Then multiply every key from K with its value from V respectively.
- Append the multiplied value in the product list and print it.
Below the implementation of the procedure.
s = ['c','o','d','e','s','p','e','e','d','y'] # initialize the list
dict1={} # dictionary
for i in s: # method to get count each character of list.
key = dict1.keys()
if i in key:
dict1[i]+=1
else:
dict1[i]=1
v = list(dict1.values()) # v = [1, 1, 2, 3, 1, 1, 1]
k = list(dict1.keys()) # k = ['c', 'o', 'd', 'e', 's', 'p', 'y']
product = []
for i,j in zip(k,v):
product.append(i*j) # i*j = 'c'*1='c'.....'d'*2='dd'...
print(product)OUTPUT:
['c', 'dd', 'eee', 'o', 'p', 's', 'y']
Method 2:
- In this method, we use dict.get() method for counting the frequency of every character in the list.
- here get() method is used to check the previous value in the list if it is new then initials with 0 and append 1 to it, else append 1 to the previously held value of that character in the dictionary.
- After we separate the dict_keys and dict_values from the dictionary in V & K.
- By using the zip() built-in function for mapping the values from V to K.
- Then multiply every key from K with its value from V respectively.
- Append the multiplied value in the product list and print it.
Below the implementation of the procedure.
s = ['c','o','d','e','s','p','e','e','d','y'] # initialize the list
dict1 = {}
for key in s:
dict1[key] = dict1.get(key, 0) + 1
v,k = list(dict1.values()),list(dict1.keys())
product = []
for i,j in zip(v,k):
product.append(i*j)
print(sorted(product))OUTPUT:
['c', 'dd', 'eee', 'o', 'p', 's', 'y']
Method 3:
This is the most suggested method for finding the frequency of the characters in the list
- Here firstly import the counter from collections
from collections import Counter
- After that apply the previous procedure to find the output.
Below the implementation of the procedure.
from collections import Counter
s = ['c','o','d','e','s','p','e','e','d','y'] # initialize the list
dict1 = Counter(s) # using the collections.counter
# to get count of each character in list
v,k = list(dict1.values()),list(dict1.keys())
product = []
for i,j in zip(v,k):
product.append(i*j)
print(sorted(product))OUTPUT:
['c', 'dd', 'eee', 'o', 'p', 's', 'y']
Thanks for visiting codespeedy. I hope it helps you.
Leave a Reply