Python program to restrict tuples by frequency of first element's value in a tuple list

Here, we have a list of tuples and we need to restrict the frequency of First Element value in the tuple list to K in Python programming language?
Submitted by Shivang Yadav, on July 18, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered set of values enclosed in square brackets [].

Example:

list = [3 ,1,  5, 7]

List of tuples is a list whose each element is a tuple.

Example:

tupList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]

Restricting tuples by frequency of first element's value in a tuple list

We need to restrict the frequency of occurrence of tuple's first value to K. For this, we will be keeping a check on the count of occurrence of the first value of tuples and discard values where the count exceeds K.

For this we have multiple methods in Python,

Input:
[(1, 7), (6, 4), (3, 5), (1, 4), (7, 3), (3, 7)] k = 1

Output:
[(1, 7), (6, 4), (3, 5), (7, 3)]

Method 1:

A method to solve the problem is by using a dictionary to count the occurrence of the first element of the tuple and accept the value if it is smaller than or equal to K, otherwise discard it. Then print the resultant list.

Program:

# Python program to restrict tuples by frequency of 
# first element's value in a tuple list

# Creating and Printing list of tuples 
tupList = [(1, 7), (6, 4), (3, 5), (1, 4), (7, 3), (3, 7)] 
print("Tuple List before Restricting tuples : " + str(tupList))
K = 1

# Restricting Tuples 
ristTupList = []
valCounter = dict()
for tup in tupList:

	if tup[0] not in valCounter.keys():
		valCounter[tup[0]] = 1
	else:
		valCounter[tup[0]] += 1
	if valCounter[tup[0]] <= K:
		ristTupList.append(tup)

# Printing the tuple List 
print("Tuple List after Restricting tuples : " + str(ristTupList))

Output:

Tuple List before Restricting tuples : [(1, 7), (6, 4), (3, 5), (1, 4), (7, 3), (3, 7)]
Tuple List after Restricting tuples : [(1, 7), (6, 4), (3, 5), (7, 3)]

Method 2:

An alternate way is to use the default dictionary for the value count and using the filter method to filter out values with frequency larger than K.

Program:

# Python program to restrict tuples by frequency of 
# first element's value in a tuple list

from collections import defaultdict

# Creating and Printing list of tuples 
tupList =  [(1, 7), (6, 4), (3, 5), (1, 4), (7, 3), (3, 7)]
print("Tuple List before Restricting tuples : " + str(tupList))
K = 2

# Restricting Tuples 
valCounter = defaultdict(list)
ristTupList = list(filter(lambda tup: valCounter[tup[0]].append(tup[0]) or len(valCounter[tup[0]]) <= K, tupList))

# Printing the tuple List 
print("Tuple List after Restricting tuples : " + str(ristTupList))

Output:

Tuple List before Restricting tuples : [(1, 7), (6, 4), (3, 5), (1, 4), (7, 3), (3, 7)]
Tuple List after Restricting tuples : [(1, 7), (6, 4), (3, 5), (1, 4), (7, 3), (3, 7)]

Python Tuple Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.