Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Concatenate Dynamic Frequency

Improve Article
Save Article
  • Last Updated : 07 Feb, 2023
Improve Article
Save Article

Given List of elements, perform concatenation with frequency dynamically, i.e each element is concatenated with its frequency till its index.

Input : test_list = [‘z’, ‘z’, ‘e’, ‘f’, ‘f’] 
Output : [‘1z’, ‘2z’, ‘1e’, ‘1f’, ‘2f’] 
Explanation : As occurrence increase, concat number is increased. 

Input : test_list = [‘g’, ‘f’, ‘g’] 
Output : [‘1g’, ‘1f’, ‘2g’] 
Explanation : As occurrence increase, concat number is increased.

Method 1 : Using defaultdict() + “+” operator + str()

In this, the dynamic frequency computation is done using defaultdict() and str() is used to convert elements to string for valid concatenation using “+” operator.

Python3




# Python3 code to demonstrate working of
# Concatenate Dynamic Frequency
# Using defaultdict() + "+" operator + str()
from collections import defaultdict
 
# initializing list
test_list = ['z', 'z', 'e', 'f', 'f', 'e', 'f', 'z', 'c']
 
# printing original list
print("The original list is : " + str(test_list))
 
memo = defaultdict(int)
res = []
for ele in test_list:
    memo[ele] += 1
 
    # adding Frequency with element
    res.append(str(memo[ele]) + str(ele))
 
# printing result
print("Dynamic Frequency list : " + str(res))

Output

The original list is : ['z', 'z', 'e', 'f', 'f', 'e', 'f', 'z', 'c']
Dynamic Frequency list : ['1z', '2z', '1e', '1f', '2f', '2e', '3f', '3z', '1c']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 2: Using slicing and count() method

Python3




# Python3 code to demonstrate working of
# Concatenate Dynamic Frequency
 
# initializing list
test_list = ['z', 'z', 'e', 'f', 'f', 'e', 'f', 'z', 'c']
 
# printing original list
print("The original list is : " + str(test_list))
res = []
for i in range(0, len(test_list)):
    a = test_list[:i+1].count(test_list[i])
    b = str(a)+test_list[i]
    res.append(b)
 
# printing result
print("Dynamic Frequency list : " + str(res))

Output

The original list is : ['z', 'z', 'e', 'f', 'f', 'e', 'f', 'z', 'c']
Dynamic Frequency list : ['1z', '2z', '1e', '1f', '2f', '2e', '3f', '3z', '1c']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3:  using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Concatenate Dynamic Frequency
import operator as op
# initializing list
test_list = ['z', 'z', 'e', 'f', 'f', 'e', 'f', 'z', 'c']
 
# printing original list
print("The original list is : " + str(test_list))
res = []
for i in range(0, len(test_list)):
    a = op.countOf(test_list[:i+1], test_list[i])
    b = str(a)+test_list[i]
    res.append(b)
 
# printing result
print("Dynamic Frequency list : " + str(res))

Output

The original list is : ['z', 'z', 'e', 'f', 'f', 'e', 'f', 'z', 'c']
Dynamic Frequency list : ['1z', '2z', '1e', '1f', '2f', '2e', '3f', '3z', '1c']

Time Complexity: O(N)
Auxiliary Space : O(N)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!