Description
stringlengths 9
105
| Link
stringlengths 45
135
| Code
stringlengths 10
26.8k
| Test_Case
stringlengths 9
202
| Merge
stringlengths 63
27k
|
---|---|---|---|---|
Python - Modulo of tuple elements
|
https://www.geeksforgeeks.org/python-modulo-of-tuple-elements/
|
# Python3 code to demonstrate working of
# Tuple modulo
# using map() + mod
from operator import mod
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
# using map() + mod
res = tuple(map(mod, test_tup1, test_tup2))
# printing result
print("The modulus tuple : " + str(res))
|
#Output : The original tuple 1 : (10, 4, 5, 6)
|
Python - Modulo of tuple elements
# Python3 code to demonstrate working of
# Tuple modulo
# using map() + mod
from operator import mod
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
# using map() + mod
res = tuple(map(mod, test_tup1, test_tup2))
# printing result
print("The modulus tuple : " + str(res))
#Output : The original tuple 1 : (10, 4, 5, 6)
[END]
|
Python - Modulo of tuple elements
|
https://www.geeksforgeeks.org/python-modulo-of-tuple-elements/
|
# Python3 code to demonstrate working of
# Tuple modulo
# Initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# Printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
res = []
for i in range(0, len(test_tup1)):
res.append(test_tup1[i] % test_tup2[i])
res = tuple(res)
# Printing result
print("The modulus tuple : " + str(res))
|
#Output : The original tuple 1 : (10, 4, 5, 6)
|
Python - Modulo of tuple elements
# Python3 code to demonstrate working of
# Tuple modulo
# Initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# Printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
res = []
for i in range(0, len(test_tup1)):
res.append(test_tup1[i] % test_tup2[i])
res = tuple(res)
# Printing result
print("The modulus tuple : " + str(res))
#Output : The original tuple 1 : (10, 4, 5, 6)
[END]
|
Python - Modulo of tuple elements
|
https://www.geeksforgeeks.org/python-modulo-of-tuple-elements/
|
import numpy as np
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo using numpy
res = tuple(np.mod(test_tup1, test_tup2))
# printing result
print("The modulus tuple : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy
|
#Output : The original tuple 1 : (10, 4, 5, 6)
|
Python - Modulo of tuple elements
import numpy as np
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo using numpy
res = tuple(np.mod(test_tup1, test_tup2))
# printing result
print("The modulus tuple : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy
#Output : The original tuple 1 : (10, 4, 5, 6)
[END]
|
Python - Modulo of tuple elements
|
https://www.geeksforgeeks.org/python-modulo-of-tuple-elements/
|
# Python3 code to demonstrate working of
# Tuple modulo
# using recursive method
def modulo_tuple(t1, t2, result=()):
if not t1:
return result
return modulo_tuple(t1[1:], t2[1:], result + (t1[0] % t2[0],))
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
result = modulo_tuple(test_tup1, test_tup2)
# printing result
print("The modulus tuple : " + str(result))
# this code contributed by tvsk
|
#Output : The original tuple 1 : (10, 4, 5, 6)
|
Python - Modulo of tuple elements
# Python3 code to demonstrate working of
# Tuple modulo
# using recursive method
def modulo_tuple(t1, t2, result=()):
if not t1:
return result
return modulo_tuple(t1[1:], t2[1:], result + (t1[0] % t2[0],))
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
result = modulo_tuple(test_tup1, test_tup2)
# printing result
print("The modulus tuple : " + str(result))
# this code contributed by tvsk
#Output : The original tuple 1 : (10, 4, 5, 6)
[END]
|
Python - Modulo of tuple elements
|
https://www.geeksforgeeks.org/python-modulo-of-tuple-elements/
|
import itertools
# Initializing list
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# Printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
result = tuple(itertools.starmap(lambda x, y: x % y, zip(test_tup1, test_tup2)))
# Printing the result
print("The modulus tuple : " + str(result))
# This code is contributed by Jyothi pinjala.
|
#Output : The original tuple 1 : (10, 4, 5, 6)
|
Python - Modulo of tuple elements
import itertools
# Initializing list
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# Printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
result = tuple(itertools.starmap(lambda x, y: x % y, zip(test_tup1, test_tup2)))
# Printing the result
print("The modulus tuple : " + str(result))
# This code is contributed by Jyothi pinjala.
#Output : The original tuple 1 : (10, 4, 5, 6)
[END]
|
Python - Modulo of tuple elements
|
https://www.geeksforgeeks.org/python-modulo-of-tuple-elements/
|
import heapq
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
modulus_tup = tuple(
heapq.nlargest(len(test_tup1), (x % y for x, y in zip(test_tup1, test_tup2)))
)
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
print("The modulus tuple : " + str(modulus_tup))
# This code is contributed by Rayudu.
|
#Output : The original tuple 1 : (10, 4, 5, 6)
|
Python - Modulo of tuple elements
import heapq
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
modulus_tup = tuple(
heapq.nlargest(len(test_tup1), (x % y for x, y in zip(test_tup1, test_tup2)))
)
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
print("The modulus tuple : " + str(modulus_tup))
# This code is contributed by Rayudu.
#Output : The original tuple 1 : (10, 4, 5, 6)
[END]
|
Python - Modulo of tuple elements
|
https://www.geeksforgeeks.org/python-modulo-of-tuple-elements/
|
# Python3 code to demonstrate working of
# Tuple modulo
# using List comprehension
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
# using List comprehension
res = [ele1 % ele2 for ele1, ele2 in zip(test_tup1, test_tup2)]
res = tuple(res)
# printing result
print("The modulus tuple : " + str(res))
|
#Output : The original tuple 1 : (10, 4, 5, 6)
|
Python - Modulo of tuple elements
# Python3 code to demonstrate working of
# Tuple modulo
# using List comprehension
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
# using List comprehension
res = [ele1 % ele2 for ele1, ele2 in zip(test_tup1, test_tup2)]
res = tuple(res)
# printing result
print("The modulus tuple : " + str(res))
#Output : The original tuple 1 : (10, 4, 5, 6)
[END]
|
Python - Update each element in tuple list
|
https://www.geeksforgeeks.org/python-update-each-element-in-tuple-list/
|
# Python3 code to demonstrate working of
# Update each element in tuple list
# Using list comprehension
# initialize list
test_list = [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
# printing original list
print("The original list : " + str(test_list))
# initialize add element
add_ele = 4
# Update each element in tuple list
# Using list comprehension
res = [tuple(j + add_ele for j in sub) for sub in test_list]
# printing result
print("List after bulk update : " + str(res))
|
#Output : The original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
|
Python - Update each element in tuple list
# Python3 code to demonstrate working of
# Update each element in tuple list
# Using list comprehension
# initialize list
test_list = [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
# printing original list
print("The original list : " + str(test_list))
# initialize add element
add_ele = 4
# Update each element in tuple list
# Using list comprehension
res = [tuple(j + add_ele for j in sub) for sub in test_list]
# printing result
print("List after bulk update : " + str(res))
#Output : The original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
[END]
|
Python - Update each element in tuple list
|
https://www.geeksforgeeks.org/python-update-each-element-in-tuple-list/
|
# Python3 code to demonstrate working of
# Update each element in tuple list
# Using list comprehension + map() + lambda
# initialize list
test_list = [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
# printing original list
print("The original list : " + str(test_list))
# initialize add element
add_ele = 4
# Update each element in tuple list
# Using list comprehension + map() + lambda
res = [tuple(map(lambda ele: ele + add_ele, sub)) for sub in test_list]
# printing result
print("List after bulk update : " + str(res))
|
#Output : The original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
|
Python - Update each element in tuple list
# Python3 code to demonstrate working of
# Update each element in tuple list
# Using list comprehension + map() + lambda
# initialize list
test_list = [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
# printing original list
print("The original list : " + str(test_list))
# initialize add element
add_ele = 4
# Update each element in tuple list
# Using list comprehension + map() + lambda
res = [tuple(map(lambda ele: ele + add_ele, sub)) for sub in test_list]
# printing result
print("List after bulk update : " + str(res))
#Output : The original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
[END]
|
Python - Update each element in tuple list
|
https://www.geeksforgeeks.org/python-update-each-element-in-tuple-list/
|
def update_tuples(tuples, new_val):
for i in range(len(tuples)):
x, y, z = tuples[i]
tuples[i] = (new_val, y, z)
return tuples
tuples = [(1, 56, "M"), (1, 14, "F"), (2, 43, "F"), (2, 10, "M")]
new_val = 5
updated_tuples = update_tuples(tuples, new_val)
print(updated_tuples)
|
#Output : The original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
|
Python - Update each element in tuple list
def update_tuples(tuples, new_val):
for i in range(len(tuples)):
x, y, z = tuples[i]
tuples[i] = (new_val, y, z)
return tuples
tuples = [(1, 56, "M"), (1, 14, "F"), (2, 43, "F"), (2, 10, "M")]
new_val = 5
updated_tuples = update_tuples(tuples, new_val)
print(updated_tuples)
#Output : The original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
[END]
|
Python - Update each element in tuple list
|
https://www.geeksforgeeks.org/python-update-each-element-in-tuple-list/
|
test_list = [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
index = 1
value = 5
new_list = []
for tup in test_list:
temp_list = list(tup)
temp_list[index] = value
new_tup = tuple(temp_list)
new_list.append(new_tup)
print("Updated list:", new_list)
|
#Output : The original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
|
Python - Update each element in tuple list
test_list = [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
index = 1
value = 5
new_list = []
for tup in test_list:
temp_list = list(tup)
temp_list[index] = value
new_tup = tuple(temp_list)
new_list.append(new_tup)
print("Updated list:", new_list)
#Output : The original list : [(1, 3, 4), (2, 4, 6), (3, 8, 1)]
[END]
|
Python - Multiply Adjacent elements
|
https://www.geeksforgeeks.org/python-multiply-adjacent-elements/
|
# Python3 code to demonstrate working of
# Adjacent element multiplication
# using zip() + generator expression + tuple
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
# printing original tuple
print("The original tuple : " + str(test_tup))
# Adjacent element multiplication
# using zip() + generator expression + tuple
res = tuple(i * j for i, j in zip(test_tup, test_tup[1:]))
# printing result
print("Resultant tuple after multiplication : " + str(res))
|
#Output : The original tuple : (1, 5, 7, 8, 10)
|
Python - Multiply Adjacent elements
# Python3 code to demonstrate working of
# Adjacent element multiplication
# using zip() + generator expression + tuple
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
# printing original tuple
print("The original tuple : " + str(test_tup))
# Adjacent element multiplication
# using zip() + generator expression + tuple
res = tuple(i * j for i, j in zip(test_tup, test_tup[1:]))
# printing result
print("Resultant tuple after multiplication : " + str(res))
#Output : The original tuple : (1, 5, 7, 8, 10)
[END]
|
Python - Multiply Adjacent elements
|
https://www.geeksforgeeks.org/python-multiply-adjacent-elements/
|
# Python3 code to demonstrate working of
# Adjacent element multiplication
# using tuple() + map() + lambda
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
# printing original tuple
print("The original tuple : " + str(test_tup))
# Adjacent element multiplication
# using tuple() + map() + lambda
res = tuple(map(lambda i, j: i * j, test_tup[1:], test_tup[:-1]))
# printing result
print("Resultant tuple after multiplication : " + str(res))
|
#Output : The original tuple : (1, 5, 7, 8, 10)
|
Python - Multiply Adjacent elements
# Python3 code to demonstrate working of
# Adjacent element multiplication
# using tuple() + map() + lambda
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
# printing original tuple
print("The original tuple : " + str(test_tup))
# Adjacent element multiplication
# using tuple() + map() + lambda
res = tuple(map(lambda i, j: i * j, test_tup[1:], test_tup[:-1]))
# printing result
print("Resultant tuple after multiplication : " + str(res))
#Output : The original tuple : (1, 5, 7, 8, 10)
[END]
|
Python - Multiply Adjacent elements
|
https://www.geeksforgeeks.org/python-multiply-adjacent-elements/
|
import numpy as np
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
# printing original tuple
print("The original tuple : " + str(test_tup))
# Adjacent element multiplication using numpy
res = np.multiply(test_tup[1:], test_tup[:-1])
# printing result
print("Resultant tuple after multiplication : " + str(tuple(res)))
# This code is contributed by Edula Vinay Kumar Reddy
|
#Output : The original tuple : (1, 5, 7, 8, 10)
|
Python - Multiply Adjacent elements
import numpy as np
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
# printing original tuple
print("The original tuple : " + str(test_tup))
# Adjacent element multiplication using numpy
res = np.multiply(test_tup[1:], test_tup[:-1])
# printing result
print("Resultant tuple after multiplication : " + str(tuple(res)))
# This code is contributed by Edula Vinay Kumar Reddy
#Output : The original tuple : (1, 5, 7, 8, 10)
[END]
|
Python - Multiply Adjacent elements
|
https://www.geeksforgeeks.org/python-multiply-adjacent-elements/
|
# Python3 code to demonstrate working of
# Adjacent element multiplication
# using for loop
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
# printing original tuple
print("The original tuple : " + str(test_tup))
# initialize an empty list to store the result
res = []
# iterate over the tuple and perform multiplication of adjacent elements
for i in range(len(test_tup) - 1):
res.append(test_tup[i] * test_tup[i + 1])
# convert the list to a tuple
res = tuple(res)
# printing result
print("Resultant tuple after multiplication : " + str(res))
|
#Output : The original tuple : (1, 5, 7, 8, 10)
|
Python - Multiply Adjacent elements
# Python3 code to demonstrate working of
# Adjacent element multiplication
# using for loop
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
# printing original tuple
print("The original tuple : " + str(test_tup))
# initialize an empty list to store the result
res = []
# iterate over the tuple and perform multiplication of adjacent elements
for i in range(len(test_tup) - 1):
res.append(test_tup[i] * test_tup[i + 1])
# convert the list to a tuple
res = tuple(res)
# printing result
print("Resultant tuple after multiplication : " + str(res))
#Output : The original tuple : (1, 5, 7, 8, 10)
[END]
|
Python - Join Tuples if similar initial element
|
https://www.geeksforgeeks.org/python-join-tuples-if-similar-initial-element/
|
# Python3 code to demonstrate working of
# Join Tuples if similar initial element
# Using loop
# initializing list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples if similar initial element
# Using loop
res = []
for sub in test_list:
if res and res[-1][0] == sub[0]:
res[-1].extend(sub[1:])
else:
res.append([ele for ele in sub])
res = list(map(tuple, res))
# printing result
print("The extracted elements : " + str(res))
|
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
|
Python - Join Tuples if similar initial element
# Python3 code to demonstrate working of
# Join Tuples if similar initial element
# Using loop
# initializing list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples if similar initial element
# Using loop
res = []
for sub in test_list:
if res and res[-1][0] == sub[0]:
res[-1].extend(sub[1:])
else:
res.append([ele for ele in sub])
res = list(map(tuple, res))
# printing result
print("The extracted elements : " + str(res))
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
[END]
|
Python - Join Tuples if similar initial element
|
https://www.geeksforgeeks.org/python-join-tuples-if-similar-initial-element/
|
# Python3 code to demonstrate working of
# Join Tuples if similar initial element
# Using defaultdict() + loop
from collections import defaultdict
# initializing list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples if similar initial element
# Using defaultdict() + loop
mapp = defaultdict(list)
for key, val in test_list:
mapp[key].append(val)
res = [(key, *val) for key, val in mapp.items()]
# printing result
print("The extracted elements : " + str(res))
|
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
|
Python - Join Tuples if similar initial element
# Python3 code to demonstrate working of
# Join Tuples if similar initial element
# Using defaultdict() + loop
from collections import defaultdict
# initializing list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples if similar initial element
# Using defaultdict() + loop
mapp = defaultdict(list)
for key, val in test_list:
mapp[key].append(val)
res = [(key, *val) for key, val in mapp.items()]
# printing result
print("The extracted elements : " + str(res))
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
[END]
|
Python - Join Tuples if similar initial element
|
https://www.geeksforgeeks.org/python-join-tuples-if-similar-initial-element/
|
# Python3 code to demonstrate working of
# Join Tuples if similar initial element
# Using loop
# initializing list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples if similar initial element
# Using loop
res = []
x = []
for i in test_list:
if i[0] not in x:
x.append(i[0])
for i in x:
p = []
p.append(i)
for j in test_list:
if i == j[0]:
p.append(j[1])
res.append(p)
# printing result
print("The extracted elements : " + str(res))
|
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
|
Python - Join Tuples if similar initial element
# Python3 code to demonstrate working of
# Join Tuples if similar initial element
# Using loop
# initializing list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples if similar initial element
# Using loop
res = []
x = []
for i in test_list:
if i[0] not in x:
x.append(i[0])
for i in x:
p = []
p.append(i)
for j in test_list:
if i == j[0]:
p.append(j[1])
res.append(p)
# printing result
print("The extracted elements : " + str(res))
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
[END]
|
Python - Join Tuples if similar initial element
|
https://www.geeksforgeeks.org/python-join-tuples-if-similar-initial-element/
|
# initializing list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples if similar initial element
# Using dictionary and list comprehension
temp_dict = {}
for x in test_list:
temp_dict[x[0]] = temp_dict.get(x[0], []) + list(x[1:])
res = [(k,) + tuple(v) for k, v in temp_dict.items()]
# printing result
print("The extracted elements : " + str(res))
|
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
|
Python - Join Tuples if similar initial element
# initializing list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples if similar initial element
# Using dictionary and list comprehension
temp_dict = {}
for x in test_list:
temp_dict[x[0]] = temp_dict.get(x[0], []) + list(x[1:])
res = [(k,) + tuple(v) for k, v in temp_dict.items()]
# printing result
print("The extracted elements : " + str(res))
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
[END]
|
Python - Join Tuples if similar initial element
|
https://www.geeksforgeeks.org/python-join-tuples-if-similar-initial-element/
|
from itertools import groupby
# initializing list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples if similar initial element
# Using itertools.groupby()
res = []
for k, g in groupby(test_list, key=lambda x: x[0]):
values = [v for _, v in g]
res.append((k, *values))
# printing result
print("The extracted elements : " + str(res))
|
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
|
Python - Join Tuples if similar initial element
from itertools import groupby
# initializing list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# printing original list
print("The original list is : " + str(test_list))
# Join Tuples if similar initial element
# Using itertools.groupby()
res = []
for k, g in groupby(test_list, key=lambda x: x[0]):
values = [v for _, v in g]
res.append((k, *values))
# printing result
print("The extracted elements : " + str(res))
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
[END]
|
Python - Join Tuples if similar initial element
|
https://www.geeksforgeeks.org/python-join-tuples-if-similar-initial-element/
|
# Define the list test_list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# Import the pandas library
import pandas as pd
# Create a DataFrame from the list test_list
df = pd.DataFrame(test_list, columns=["A", "B"])
# Group the DataFrame by column A and aggregate column B as a list for each group
grouped = df.groupby("A")["B"].apply(list)
# Convert the resulting Series to a list of tuples
res = [tuple([k] + v) for k, v in grouped.items()]
# Print the result
print("The extracted elements : " + str(res))
|
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
|
Python - Join Tuples if similar initial element
# Define the list test_list
test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# Import the pandas library
import pandas as pd
# Create a DataFrame from the list test_list
df = pd.DataFrame(test_list, columns=["A", "B"])
# Group the DataFrame by column A and aggregate column B as a list for each group
grouped = df.groupby("A")["B"].apply(list)
# Convert the resulting Series to a list of tuples
res = [tuple([k] + v) for k, v in grouped.items()]
# Print the result
print("The extracted elements : " + str(res))
Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
#Output : [(5, 6, 7, 8), (6, 10), (7, 13)]
[END]
|
Python - All pair combinations of 2 Tuples
|
https://www.geeksforgeeks.org/python-all-pair-combinations-of-2-tuples/
|
# Python3 code to demonstrate working of
# All pair combinations of 2 tuples
# Using list comprehension
# initializing tuples
test_tuple1 = (4, 5)
test_tuple2 = (7, 8)
# printing original tuples
print("The original tuple 1 : " + str(test_tuple1))
print("The original tuple 2 : " + str(test_tuple2))
# All pair combinations of 2 tuples
# Using list comprehension
res = [(a, b) for a in test_tuple1 for b in test_tuple2]
res = res + [(a, b) for a in test_tuple2 for b in test_tuple1]
# printing result
print("The filtered tuple : " + str(res))
|
#Output : The original tuple 1 : (4, 5)
|
Python - All pair combinations of 2 Tuples
# Python3 code to demonstrate working of
# All pair combinations of 2 tuples
# Using list comprehension
# initializing tuples
test_tuple1 = (4, 5)
test_tuple2 = (7, 8)
# printing original tuples
print("The original tuple 1 : " + str(test_tuple1))
print("The original tuple 2 : " + str(test_tuple2))
# All pair combinations of 2 tuples
# Using list comprehension
res = [(a, b) for a in test_tuple1 for b in test_tuple2]
res = res + [(a, b) for a in test_tuple2 for b in test_tuple1]
# printing result
print("The filtered tuple : " + str(res))
#Output : The original tuple 1 : (4, 5)
[END]
|
Python - All pair combinations of 2 Tuples
|
https://www.geeksforgeeks.org/python-all-pair-combinations-of-2-tuples/
|
# Python3 code to demonstrate working of
# All pair combinations of 2 tuples
# Using chain() + product()
from itertools import chain, product
# initializing tuples
test_tuple1 = (4, 5)
test_tuple2 = (7, 8)
# printing original tuples
print("The original tuple 1 : " + str(test_tuple1))
print("The original tuple 2 : " + str(test_tuple2))
# All pair combinations of 2 tuples
# Using chain() + product()
res = list(chain(product(test_tuple1, test_tuple2), product(test_tuple2, test_tuple1)))
# printing result
print("The filtered tuple : " + str(res))
|
#Output : The original tuple 1 : (4, 5)
|
Python - All pair combinations of 2 Tuples
# Python3 code to demonstrate working of
# All pair combinations of 2 tuples
# Using chain() + product()
from itertools import chain, product
# initializing tuples
test_tuple1 = (4, 5)
test_tuple2 = (7, 8)
# printing original tuples
print("The original tuple 1 : " + str(test_tuple1))
print("The original tuple 2 : " + str(test_tuple2))
# All pair combinations of 2 tuples
# Using chain() + product()
res = list(chain(product(test_tuple1, test_tuple2), product(test_tuple2, test_tuple1)))
# printing result
print("The filtered tuple : " + str(res))
#Output : The original tuple 1 : (4, 5)
[END]
|
Python - All pair combinations of 2 Tuples
|
https://www.geeksforgeeks.org/python-all-pair-combinations-of-2-tuples/
|
import itertools
# initializing tuples
test_tuple1 = (4, 5)
test_tuple2 = (7, 8)
# printing original tuples
print("The original tuple 1 : " + str(test_tuple1))
print("The original tuple 2 : " + str(test_tuple2))
# generating all pair combinations of 2 tuples using list comprehension
res = [(a, b) for a in test_tuple1 for b in test_tuple2] + [
(a, b) for a in test_tuple2 for b in test_tuple1
]
# printing result
print("All pair combinations of 2 tuples : " + str(res))
# This code is contributed by Jyothi pinjala.
|
#Output : The original tuple 1 : (4, 5)
|
Python - All pair combinations of 2 Tuples
import itertools
# initializing tuples
test_tuple1 = (4, 5)
test_tuple2 = (7, 8)
# printing original tuples
print("The original tuple 1 : " + str(test_tuple1))
print("The original tuple 2 : " + str(test_tuple2))
# generating all pair combinations of 2 tuples using list comprehension
res = [(a, b) for a in test_tuple1 for b in test_tuple2] + [
(a, b) for a in test_tuple2 for b in test_tuple1
]
# printing result
print("All pair combinations of 2 tuples : " + str(res))
# This code is contributed by Jyothi pinjala.
#Output : The original tuple 1 : (4, 5)
[END]
|
Python - All pair combinations of 2 Tuples
|
https://www.geeksforgeeks.org/python-all-pair-combinations-of-2-tuples/
|
# input
tuple1 = (4, 5)
tuple2 = (7, 8)
# initialize an empty list to store the filtered tuples
filtered_tuples = []
# iterate over each element in tuple 1
for element1 in tuple1:
# iterate over each element in tuple 2
for element2 in tuple2:
# append a tuple of the two elements to the filtered list
filtered_tuples.append((element1, element2))
# append a tuple of the two elements in reverse order to the filtered list
filtered_tuples.append((element2, element1))
# output
print(filtered_tuples)
|
#Output : The original tuple 1 : (4, 5)
|
Python - All pair combinations of 2 Tuples
# input
tuple1 = (4, 5)
tuple2 = (7, 8)
# initialize an empty list to store the filtered tuples
filtered_tuples = []
# iterate over each element in tuple 1
for element1 in tuple1:
# iterate over each element in tuple 2
for element2 in tuple2:
# append a tuple of the two elements to the filtered list
filtered_tuples.append((element1, element2))
# append a tuple of the two elements in reverse order to the filtered list
filtered_tuples.append((element2, element1))
# output
print(filtered_tuples)
#Output : The original tuple 1 : (4, 5)
[END]
|
Python - Remove Tuples of Length K
|
https://www.geeksforgeeks.org/python-remove-tuples-of-length-k/
|
# Python3 code to demonstrate working of
# Remove Tuples of Length K
# Using list comprehension
# initializing list
test_list = [(4, 5), (4,), (8, 6, 7), (1,), (3, 4, 6, 7)]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 1
# 1 liner to perform task
# filter just lengths other than K
# len() used to compute length
res = [ele for ele in test_list if len(ele) != K]
# printing result
print("Filtered list : " + str(res))
|
#Output : The original list : [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
|
Python - Remove Tuples of Length K
# Python3 code to demonstrate working of
# Remove Tuples of Length K
# Using list comprehension
# initializing list
test_list = [(4, 5), (4,), (8, 6, 7), (1,), (3, 4, 6, 7)]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 1
# 1 liner to perform task
# filter just lengths other than K
# len() used to compute length
res = [ele for ele in test_list if len(ele) != K]
# printing result
print("Filtered list : " + str(res))
#Output : The original list : [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
[END]
|
Python - Remove Tuples of Length K
|
https://www.geeksforgeeks.org/python-remove-tuples-of-length-k/
|
# Python3 code to demonstrate working of
# Remove Tuples of Length K
# Using filter() + lambda + len()
# initializing list
test_list = [(4, 5), (4,), (8, 6, 7), (1,), (3, 4, 6, 7)]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 1
# filter() filters non K length values and returns result
res = list(filter(lambda x: len(x) != K, test_list))
# printing result
print("Filtered list : " + str(res))
|
#Output : The original list : [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
|
Python - Remove Tuples of Length K
# Python3 code to demonstrate working of
# Remove Tuples of Length K
# Using filter() + lambda + len()
# initializing list
test_list = [(4, 5), (4,), (8, 6, 7), (1,), (3, 4, 6, 7)]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 1
# filter() filters non K length values and returns result
res = list(filter(lambda x: len(x) != K, test_list))
# printing result
print("Filtered list : " + str(res))
#Output : The original list : [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
[END]
|
Python - Remove Tuples of Length K
|
https://www.geeksforgeeks.org/python-remove-tuples-of-length-k/
|
# Python3 code to demonstrate working of
# Remove Tuples of Length K
# Using list comprehension
# initializing list
test_list = [(4, 5), (4,), (8, 6, 7), (1,), (3, 4, 6, 7)]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 1
# using list comprehension to filter out tuples of length K
# len() is used to compute length
res = [ele for ele in test_list if len(ele) != K]
# printing result
print("Filtered list : " + str(res))
|
#Output : The original list : [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
|
Python - Remove Tuples of Length K
# Python3 code to demonstrate working of
# Remove Tuples of Length K
# Using list comprehension
# initializing list
test_list = [(4, 5), (4,), (8, 6, 7), (1,), (3, 4, 6, 7)]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 1
# using list comprehension to filter out tuples of length K
# len() is used to compute length
res = [ele for ele in test_list if len(ele) != K]
# printing result
print("Filtered list : " + str(res))
#Output : The original list : [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
[END]
|
Python - Remove Tuples of Length K
|
https://www.geeksforgeeks.org/python-remove-tuples-of-length-k/
|
# input
original_list = [(4, 5), (4,), (8, 6, 7), (1,), (3, 4, 6, 7)]
k = 1
# use map() and a lambda function to filter out tuples of length k
filtered_list = list(map(lambda x: x, filter(lambda x: len(x) != k, original_list)))
# output
print(filtered_list)
|
#Output : The original list : [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
|
Python - Remove Tuples of Length K
# input
original_list = [(4, 5), (4,), (8, 6, 7), (1,), (3, 4, 6, 7)]
k = 1
# use map() and a lambda function to filter out tuples of length k
filtered_list = list(map(lambda x: x, filter(lambda x: len(x) != k, original_list)))
# output
print(filtered_list)
#Output : The original list : [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
[END]
|
Python - Remove Tuples of Length K
|
https://www.geeksforgeeks.org/python-remove-tuples-of-length-k/
|
import heapq
# initializing list
test_list = [(4, 5), (4,), (8, 6, 7), (1,), (3, 4, 6, 7)]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 1
# filtering non K length values using heapq
res = list(filter(lambda x: len(x) != K, test_list))
# printing result
print("Filtered list : " + str(res))
# This code is contributed by Vinay Pinjala.
|
#Output : The original list : [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
|
Python - Remove Tuples of Length K
import heapq
# initializing list
test_list = [(4, 5), (4,), (8, 6, 7), (1,), (3, 4, 6, 7)]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 1
# filtering non K length values using heapq
res = list(filter(lambda x: len(x) != K, test_list))
# printing result
print("Filtered list : " + str(res))
# This code is contributed by Vinay Pinjala.
#Output : The original list : [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
[END]
|
Python - Remove Tuples from the List having every element as None
|
https://www.geeksforgeeks.org/python-remove-tuples-from-the-list-having-every-element-as-none/
|
# Python3 code to demonstrate working of
# Remove None Tuples from List
# Using all() + list comprehension
# initializing list
test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None,)]
# printing original list
print("The original list is : " + str(test_list))
# negating result for discarding all None Tuples
res = [sub for sub in test_list if not all(ele == None for ele in sub)]
# printing result
print("Removed None Tuples : " + str(res))
|
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
|
Python - Remove Tuples from the List having every element as None
# Python3 code to demonstrate working of
# Remove None Tuples from List
# Using all() + list comprehension
# initializing list
test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None,)]
# printing original list
print("The original list is : " + str(test_list))
# negating result for discarding all None Tuples
res = [sub for sub in test_list if not all(ele == None for ele in sub)]
# printing result
print("Removed None Tuples : " + str(res))
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
[END]
|
Python - Remove Tuples from the List having every element as None
|
https://www.geeksforgeeks.org/python-remove-tuples-from-the-list-having-every-element-as-none/
|
# Python3 code to demonstrate working of
# Remove None Tuples from List
# Using filter() + lambda + all()
# initializing list
test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None,)]
# printing original list
print("The original list is : " + str(test_list))
# filter() + lambda to drive logic of discarding tuples
res = list(filter(lambda sub: not all(ele == None for ele in sub), test_list))
# printing result
print("Removed None Tuples : " + str(res))
|
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
|
Python - Remove Tuples from the List having every element as None
# Python3 code to demonstrate working of
# Remove None Tuples from List
# Using filter() + lambda + all()
# initializing list
test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None,)]
# printing original list
print("The original list is : " + str(test_list))
# filter() + lambda to drive logic of discarding tuples
res = list(filter(lambda sub: not all(ele == None for ele in sub), test_list))
# printing result
print("Removed None Tuples : " + str(res))
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
[END]
|
Python - Remove Tuples from the List having every element as None
|
https://www.geeksforgeeks.org/python-remove-tuples-from-the-list-having-every-element-as-none/
|
# Python3 code to demonstrate working of
# Remove None Tuples from List
# initializing list
test_list = [(None, None), (None, None), (3, 4), (12, 3), (None,)]
# printing original list
print("The original list is : " + str(test_list))
# negating result for discarding all None Tuples
res = []
for i in test_list:
if not (i.count(None) == len(i)):
res.append(i)
# printing result
print("Removed None Tuples : " + str(res))
|
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
|
Python - Remove Tuples from the List having every element as None
# Python3 code to demonstrate working of
# Remove None Tuples from List
# initializing list
test_list = [(None, None), (None, None), (3, 4), (12, 3), (None,)]
# printing original list
print("The original list is : " + str(test_list))
# negating result for discarding all None Tuples
res = []
for i in test_list:
if not (i.count(None) == len(i)):
res.append(i)
# printing result
print("Removed None Tuples : " + str(res))
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
[END]
|
Python - Remove Tuples from the List having every element as None
|
https://www.geeksforgeeks.org/python-remove-tuples-from-the-list-having-every-element-as-none/
|
test_list = [(None, None), (None, None), (3, 4), (12, 3), (None,)]
res = [sub for i, sub in enumerate(test_list) if not all(ele == None for ele in sub)]
print(res)
|
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
|
Python - Remove Tuples from the List having every element as None
test_list = [(None, None), (None, None), (3, 4), (12, 3), (None,)]
res = [sub for i, sub in enumerate(test_list) if not all(ele == None for ele in sub)]
print(res)
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
[END]
|
Python - Remove Tuples from the List having every element as None
|
https://www.geeksforgeeks.org/python-remove-tuples-from-the-list-having-every-element-as-none/
|
# Python3 code to demonstrate working of
# Remove None Tuples from List
# initializing list
test_list = [(None, None), (None, None), (3, 4), (12, 3), (None,)]
# printing original list
print("The original list is : " + str(test_list))
# negating result for discarding all None Tuples
res = []
import operator
for i in test_list:
if not (operator.countOf(i, None) == len(i)):
res.append(i)
# printing result
print("Removed None Tuples : " + str(res))
|
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
|
Python - Remove Tuples from the List having every element as None
# Python3 code to demonstrate working of
# Remove None Tuples from List
# initializing list
test_list = [(None, None), (None, None), (3, 4), (12, 3), (None,)]
# printing original list
print("The original list is : " + str(test_list))
# negating result for discarding all None Tuples
res = []
import operator
for i in test_list:
if not (operator.countOf(i, None) == len(i)):
res.append(i)
# printing result
print("Removed None Tuples : " + str(res))
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
[END]
|
Python - Remove Tuples from the List having every element as None
|
https://www.geeksforgeeks.org/python-remove-tuples-from-the-list-having-every-element-as-none/
|
# Python3 code to demonstrate working of
# Remove None Tuples from List
# initializing list
test_list = [(None, None), (None, None), (3, 4), (12, 3), (None,)]
# printing original list
print("The original list is : " + str(test_list))
# using for loop and slicing to remove None Tuples
res = []
for i in range(len(test_list)):
if None not in test_list[i]:
res.append(test_list[i])
# printing result
print("Removed None Tuples : " + str(res))
|
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
|
Python - Remove Tuples from the List having every element as None
# Python3 code to demonstrate working of
# Remove None Tuples from List
# initializing list
test_list = [(None, None), (None, None), (3, 4), (12, 3), (None,)]
# printing original list
print("The original list is : " + str(test_list))
# using for loop and slicing to remove None Tuples
res = []
for i in range(len(test_list)):
if None not in test_list[i]:
res.append(test_list[i])
# printing result
print("Removed None Tuples : " + str(res))
#Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]??????
#Output : [(None, 2), (3, 4), (12, 3
[END]
|
Sort a list of tuples by second Item
|
https://www.geeksforgeeks.org/python-program-to-sort-a-list-of-tuples-by-second-item/
|
# Python program to sort a list of tuples by the second Item
# Function to sort the list of tuples by its second item
def Sort_Tuple(tup):
# getting length of list of tuples
lst = len(tup)
for i in range(0, lst):
for j in range(0, lst - i - 1):
if tup[j][1] > tup[j + 1][1]:
temp = tup[j]
tup[j] = tup[j + 1]
tup[j + 1] = temp
return tup
# Driver Code
tup = [
("for", 24),
("is", 10),
("Geeks", 28),
("Geeksforgeeks", 5),
("portal", 20),
("a", 15),
]
print(Sort_Tuple(tup))
|
#Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)]
#Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]
|
Sort a list of tuples by second Item
# Python program to sort a list of tuples by the second Item
# Function to sort the list of tuples by its second item
def Sort_Tuple(tup):
# getting length of list of tuples
lst = len(tup)
for i in range(0, lst):
for j in range(0, lst - i - 1):
if tup[j][1] > tup[j + 1][1]:
temp = tup[j]
tup[j] = tup[j + 1]
tup[j + 1] = temp
return tup
# Driver Code
tup = [
("for", 24),
("is", 10),
("Geeks", 28),
("Geeksforgeeks", 5),
("portal", 20),
("a", 15),
]
print(Sort_Tuple(tup))
#Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)]
#Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]
[END]
|
Sort a list of tuples by second Item
|
https://www.geeksforgeeks.org/python-program-to-sort-a-list-of-tuples-by-second-item/
|
# Python program to sort a list of
# tuples by the second Item using sort()
# Function to sort the list by second item of tuple
def Sort_Tuple(tup):
# reverse = None (Sorts in Ascending order)
# key is set to sort using second element of
# sublist lambda has been used
tup.sort(key=lambda x: x[1])
return tup
# Driver Code
tup = [("rishav", 10), ("akash", 5), ("ram", 20), ("gaurav", 15)]
# printing the sorted list of tuples
print(Sort_Tuple(tup))
|
#Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)]
#Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]
|
Sort a list of tuples by second Item
# Python program to sort a list of
# tuples by the second Item using sort()
# Function to sort the list by second item of tuple
def Sort_Tuple(tup):
# reverse = None (Sorts in Ascending order)
# key is set to sort using second element of
# sublist lambda has been used
tup.sort(key=lambda x: x[1])
return tup
# Driver Code
tup = [("rishav", 10), ("akash", 5), ("ram", 20), ("gaurav", 15)]
# printing the sorted list of tuples
print(Sort_Tuple(tup))
#Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)]
#Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]
[END]
|
Sort a list of tuples by second Item
|
https://www.geeksforgeeks.org/python-program-to-sort-a-list-of-tuples-by-second-item/
|
# Python program to sort a list of
# tuples by the second Item using sorted()
# Function to sort the list by second item of tuple
def Sort_Tuple(tup):
# reverse = None (Sorts in Ascending order)
# key is set to sort using second element of
# sublist lambda has been used
return sorted(tup, key=lambda x: x[1])
# Driver Code
tup = [("rishav", 10), ("akash", 5), ("ram", 20), ("gaurav", 15)]
# printing the sorted list of tuples
print(Sort_Tuple(tup))
|
#Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)]
#Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]
|
Sort a list of tuples by second Item
# Python program to sort a list of
# tuples by the second Item using sorted()
# Function to sort the list by second item of tuple
def Sort_Tuple(tup):
# reverse = None (Sorts in Ascending order)
# key is set to sort using second element of
# sublist lambda has been used
return sorted(tup, key=lambda x: x[1])
# Driver Code
tup = [("rishav", 10), ("akash", 5), ("ram", 20), ("gaurav", 15)]
# printing the sorted list of tuples
print(Sort_Tuple(tup))
#Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)]
#Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]
[END]
|
Sort a list of tuples by second Item
|
https://www.geeksforgeeks.org/python-program-to-sort-a-list-of-tuples-by-second-item/
|
from operator import itemgetter
def sort_tuples(tuples):
# Sort the tuples by the second item using the itemgetter function
return sorted(tuples, key=itemgetter(1))
# Test the function
tuples = [("for", 24), ("Geeks", 8), ("Geeks", 30)]
print(sort_tuples(tuples))
# This code is contributed by Edula Vinay Kumar Reddy
|
#Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)]
#Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]
|
Sort a list of tuples by second Item
from operator import itemgetter
def sort_tuples(tuples):
# Sort the tuples by the second item using the itemgetter function
return sorted(tuples, key=itemgetter(1))
# Test the function
tuples = [("for", 24), ("Geeks", 8), ("Geeks", 30)]
print(sort_tuples(tuples))
# This code is contributed by Edula Vinay Kumar Reddy
#Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)]
#Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]
[END]
|
Sort a list of tuples by second Item
|
https://www.geeksforgeeks.org/python-program-to-sort-a-list-of-tuples-by-second-item/
|
import numpy as np
def sort_tuple(tup):
# convert the list of tuples to a numpy array with data type (object, int)
arr = np.array(tup, dtype=[("col1", object), ("col2", int)])
# get the indices that would sort the array based on the second column
indices = np.argsort(arr["col2"])
# use the resulting indices to sort the array
sorted_arr = arr[indices]
# convert the sorted numpy array back to a list of tuples
sorted_tup = [(row["col1"], row["col2"]) for row in sorted_arr]
return sorted_tup
# Driver code
tup = [
("for", 24),
("is", 10),
("Geeks", 28),
("Geeksforgeeks", 5),
("portal", 20),
("a", 15),
]
print(sort_tuple(tup))
|
#Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)]
#Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]
|
Sort a list of tuples by second Item
import numpy as np
def sort_tuple(tup):
# convert the list of tuples to a numpy array with data type (object, int)
arr = np.array(tup, dtype=[("col1", object), ("col2", int)])
# get the indices that would sort the array based on the second column
indices = np.argsort(arr["col2"])
# use the resulting indices to sort the array
sorted_arr = arr[indices]
# convert the sorted numpy array back to a list of tuples
sorted_tup = [(row["col1"], row["col2"]) for row in sorted_arr]
return sorted_tup
# Driver code
tup = [
("for", 24),
("is", 10),
("Geeks", 28),
("Geeksforgeeks", 5),
("portal", 20),
("a", 15),
]
print(sort_tuple(tup))
#Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)]
#Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]
[END]
|
Python - Sort Tuples by Total Digits
|
https://www.geeksforgeeks.org/python-sort-tuples-by-total-digits/
|
# Python3 code to demonstrate working of
# Sort Tuples by Total digits
# Using sort() + len() + sum()
def count_digs(tup):
# gets total digits in tuples
return sum([len(str(ele)) for ele in tup])
# initializing list
test_list = [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
# printing original list
print("The original list is : " + str(test_list))
# performing sort
test_list.sort(key=count_digs)
# printing result
print("Sorted tuples : " + str(test_list))
|
#Output : The original list is : [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
|
Python - Sort Tuples by Total Digits
# Python3 code to demonstrate working of
# Sort Tuples by Total digits
# Using sort() + len() + sum()
def count_digs(tup):
# gets total digits in tuples
return sum([len(str(ele)) for ele in tup])
# initializing list
test_list = [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
# printing original list
print("The original list is : " + str(test_list))
# performing sort
test_list.sort(key=count_digs)
# printing result
print("Sorted tuples : " + str(test_list))
#Output : The original list is : [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
[END]
|
Python - Sort Tuples by Total Digits
|
https://www.geeksforgeeks.org/python-sort-tuples-by-total-digits/
|
# Python3 code to demonstrate working of
# Sort Tuples by Total digits
# Using sorted() + lambda + sum() + len()
# initializing list
test_list = [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
# printing original list
print("The original list is : " + str(test_list))
# performing sort, lambda function provides logic
res = sorted(test_list, key=lambda tup: sum([len(str(ele)) for ele in tup]))
# printing result
print("Sorted tuples : " + str(res))
|
#Output : The original list is : [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
|
Python - Sort Tuples by Total Digits
# Python3 code to demonstrate working of
# Sort Tuples by Total digits
# Using sorted() + lambda + sum() + len()
# initializing list
test_list = [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
# printing original list
print("The original list is : " + str(test_list))
# performing sort, lambda function provides logic
res = sorted(test_list, key=lambda tup: sum([len(str(ele)) for ele in tup]))
# printing result
print("Sorted tuples : " + str(res))
#Output : The original list is : [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
[END]
|
Python - Sort Tuples by Total Digits
|
https://www.geeksforgeeks.org/python-sort-tuples-by-total-digits/
|
from functools import reduce
# initializing list
test_list = [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
# printing original list
print("The original list is : " + str(test_list))
# performing sort, reduce function provides logic to count total digits in each tuple
res = sorted(test_list, key=lambda tup: reduce(lambda x, y: x + len(str(y)), tup, 0))
# printing result
print("Sorted tuples : " + str(res))
# This code is contributed by Jyothi pinjala,
|
#Output : The original list is : [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
|
Python - Sort Tuples by Total Digits
from functools import reduce
# initializing list
test_list = [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
# printing original list
print("The original list is : " + str(test_list))
# performing sort, reduce function provides logic to count total digits in each tuple
res = sorted(test_list, key=lambda tup: reduce(lambda x, y: x + len(str(y)), tup, 0))
# printing result
print("Sorted tuples : " + str(res))
# This code is contributed by Jyothi pinjala,
#Output : The original list is : [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)]
[END]
|
Python - Elements frequency in tuple
|
https://www.geeksforgeeks.org/python-elements-frequency-in-tuple/
|
# Python3 code to demonstrate working of
# Elements frequency in Tuple
# Using defaultdict()
from collections import defaultdict
# initializing tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
res = defaultdict(int)
for ele in test_tup:
# incrementing frequency
res[ele] += 1
# printing result
print("Tuple elements frequency is : " + str(dict(res)))
|
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
|
Python - Elements frequency in tuple
# Python3 code to demonstrate working of
# Elements frequency in Tuple
# Using defaultdict()
from collections import defaultdict
# initializing tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
res = defaultdict(int)
for ele in test_tup:
# incrementing frequency
res[ele] += 1
# printing result
print("Tuple elements frequency is : " + str(dict(res)))
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
[END]
|
Python - Elements frequency in tuple
|
https://www.geeksforgeeks.org/python-elements-frequency-in-tuple/
|
# Python3 code to demonstrate working of
# Elements frequency in Tuple
# Using Counter()
from collections import Counter
# initializing tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# converting result back from defaultdict to dict
res = dict(Counter(test_tup))
# printing result
print("Tuple elements frequency is : " + str(res))
|
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
|
Python - Elements frequency in tuple
# Python3 code to demonstrate working of
# Elements frequency in Tuple
# Using Counter()
from collections import Counter
# initializing tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# converting result back from defaultdict to dict
res = dict(Counter(test_tup))
# printing result
print("Tuple elements frequency is : " + str(res))
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
[END]
|
Python - Elements frequency in tuple
|
https://www.geeksforgeeks.org/python-elements-frequency-in-tuple/
|
# Python3 code to demonstrate working of
# Elements frequency in Tuple
# initializing tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
res = dict()
x = list(test_tup)
y = []
for i in x:
if i not in y:
y.append(i)
for i in y:
res[i] = x.count(i)
# printing result
print("Tuple elements frequency is : " + str(res))
|
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
|
Python - Elements frequency in tuple
# Python3 code to demonstrate working of
# Elements frequency in Tuple
# initializing tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
res = dict()
x = list(test_tup)
y = []
for i in x:
if i not in y:
y.append(i)
for i in y:
res[i] = x.count(i)
# printing result
print("Tuple elements frequency is : " + str(res))
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
[END]
|
Python - Elements frequency in tuple
|
https://www.geeksforgeeks.org/python-elements-frequency-in-tuple/
|
# Python3 code to demonstrate working of
# Elements frequency in Tuple
import operator as op
# initializing tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
res = dict()
x = list(test_tup)
y = []
for i in x:
if i not in y:
y.append(i)
for i in y:
res[i] = op.countOf(x, i)
# printing result
print("Tuple elements frequency is : " + str(res))
|
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
|
Python - Elements frequency in tuple
# Python3 code to demonstrate working of
# Elements frequency in Tuple
import operator as op
# initializing tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
res = dict()
x = list(test_tup)
y = []
for i in x:
if i not in y:
y.append(i)
for i in y:
res[i] = op.countOf(x, i)
# printing result
print("Tuple elements frequency is : " + str(res))
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
[END]
|
Python - Elements frequency in tuple
|
https://www.geeksforgeeks.org/python-elements-frequency-in-tuple/
|
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
freq_dict = {}
for elem in test_tup:
if elem in freq_dict:
freq_dict[elem] += 1
else:
freq_dict[elem] = 1
print("Tuple elements frequency is : " + str(freq_dict))
|
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
|
Python - Elements frequency in tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
freq_dict = {}
for elem in test_tup:
if elem in freq_dict:
freq_dict[elem] += 1
else:
freq_dict[elem] = 1
print("Tuple elements frequency is : " + str(freq_dict))
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
[END]
|
Python - Elements frequency in tuple
|
https://www.geeksforgeeks.org/python-elements-frequency-in-tuple/
|
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
unique_elems = set(test_tup)
freq_dict = {elem: [x for x in test_tup].count(elem) for elem in unique_elems}
print("Tuple elements frequency is : " + str(freq_dict))
|
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
|
Python - Elements frequency in tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
unique_elems = set(test_tup)
freq_dict = {elem: [x for x in test_tup].count(elem) for elem in unique_elems}
print("Tuple elements frequency is : " + str(freq_dict))
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
[END]
|
Python - Elements frequency in tuple
|
https://www.geeksforgeeks.org/python-elements-frequency-in-tuple/
|
from collections import Counter
from functools import reduce
# initializing tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Elements frequency in Tuple
# Using reduce() + Counter()
res = reduce(lambda x, y: Counter(x) + Counter(y), [test_tup], Counter())
# printing result
print("Tuple elements frequency is : " + str(res))
# This code is contrinuted by Pushpa.
|
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
|
Python - Elements frequency in tuple
from collections import Counter
from functools import reduce
# initializing tuple
test_tup = (4, 5, 4, 5, 6, 6, 5, 5, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Elements frequency in Tuple
# Using reduce() + Counter()
res = reduce(lambda x, y: Counter(x) + Counter(y), [test_tup], Counter())
# printing result
print("Tuple elements frequency is : " + str(res))
# This code is contrinuted by Pushpa.
#Output : The original tuple is : (4, 5, 4, 5, 6, 6, 5, 5, 4)
[END]
|
Python - Filter Range Length Tuples
|
https://www.geeksforgeeks.org/python-filter-range-length-tuples/
|
# Python3 code to demonstrate working of
# Filter Range Length Tuples
# Using list comprehension + len()
# Initializing list
test_list = [(4,), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired lengths
i, j = 2, 3
# Filter Range Length Tuples
# Using list comprehension + len()
res = [sub for sub in test_list if len(sub) >= i and len(sub) <= j]
# printing result
print("The tuple list after filtering range records : " + str(res))
|
#Output : The original list is : [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
|
Python - Filter Range Length Tuples
# Python3 code to demonstrate working of
# Filter Range Length Tuples
# Using list comprehension + len()
# Initializing list
test_list = [(4,), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired lengths
i, j = 2, 3
# Filter Range Length Tuples
# Using list comprehension + len()
res = [sub for sub in test_list if len(sub) >= i and len(sub) <= j]
# printing result
print("The tuple list after filtering range records : " + str(res))
#Output : The original list is : [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
[END]
|
Python - Filter Range Length Tuples
|
https://www.geeksforgeeks.org/python-filter-range-length-tuples/
|
# Python3 code to demonstrate working of
# Filter Range Length Tuples
# Using filter() + lambda + len()
# Initializing list
test_list = [(4,), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired lengths
i, j = 2, 3
# Filter Range Length Tuples
# Using filter() + lambda + len()
res = list(filter(lambda ele: len(ele) >= i and len(ele) <= j, test_list))
# printing result
print("The tuple list after filtering range records : " + str(res))
|
#Output : The original list is : [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
|
Python - Filter Range Length Tuples
# Python3 code to demonstrate working of
# Filter Range Length Tuples
# Using filter() + lambda + len()
# Initializing list
test_list = [(4,), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired lengths
i, j = 2, 3
# Filter Range Length Tuples
# Using filter() + lambda + len()
res = list(filter(lambda ele: len(ele) >= i and len(ele) <= j, test_list))
# printing result
print("The tuple list after filtering range records : " + str(res))
#Output : The original list is : [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
[END]
|
Python - Filter Range Length Tuples
|
https://www.geeksforgeeks.org/python-filter-range-length-tuples/
|
def filter_tuples(tuples_list, min_length, max_length):
filtered_list = []
for t in tuples_list:
if min_length <= len(t) <= max_length:
filtered_list.append(t)
return filtered_list
original_list = [(4,), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
filtered_list = [t for t in original_list if 2 <= len(t) <= 3]
print(filtered_list)
|
#Output : The original list is : [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
|
Python - Filter Range Length Tuples
def filter_tuples(tuples_list, min_length, max_length):
filtered_list = []
for t in tuples_list:
if min_length <= len(t) <= max_length:
filtered_list.append(t)
return filtered_list
original_list = [(4,), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
filtered_list = [t for t in original_list if 2 <= len(t) <= 3]
print(filtered_list)
#Output : The original list is : [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
[END]
|
Python - Assign Frequency to Tuples
|
https://www.geeksforgeeks.org/python-assign-frequency-to-tuples/
|
# Python3 code to demonstrate working of
# Assign Frequency to Tuples
# Using Counter() + items() + * operator + list comprehension
from collections import Counter
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# one-liner to solve problem
# assign Frequency as last element of tuple
res = [(*key, val) for key, val in Counter(test_list).items()]
# printing results
print("Frequency Tuple list : " + str(res))
|
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
|
Python - Assign Frequency to Tuples
# Python3 code to demonstrate working of
# Assign Frequency to Tuples
# Using Counter() + items() + * operator + list comprehension
from collections import Counter
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# one-liner to solve problem
# assign Frequency as last element of tuple
res = [(*key, val) for key, val in Counter(test_list).items()]
# printing results
print("Frequency Tuple list : " + str(res))
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
[END]
|
Python - Assign Frequency to Tuples
|
https://www.geeksforgeeks.org/python-assign-frequency-to-tuples/
|
# Python3 code to demonstrate working of
# Assign Frequency to Tuples
# Using most_common() + Counter() + * operator + list comprehension
from collections import Counter
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# most_common performs sort on arg. list
# assign Frequency as last element of tuple
res = [(*key, val) for key, val in Counter(test_list).most_common()]
# printing results
print("Frequency Tuple list : " + str(res))
|
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
|
Python - Assign Frequency to Tuples
# Python3 code to demonstrate working of
# Assign Frequency to Tuples
# Using most_common() + Counter() + * operator + list comprehension
from collections import Counter
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# most_common performs sort on arg. list
# assign Frequency as last element of tuple
res = [(*key, val) for key, val in Counter(test_list).most_common()]
# printing results
print("Frequency Tuple list : " + str(res))
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
[END]
|
Python - Assign Frequency to Tuples
|
https://www.geeksforgeeks.org/python-assign-frequency-to-tuples/
|
# Python3 code to demonstrate working of
# Assign Frequency to Tuples
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# one-liner to solve problem
# assign Frequency as last element of tuple
res = []
for i in test_list:
if i not in res:
res.append(i)
res1 = []
for i in res:
x = list(i)
x.append(test_list.count(i))
p = tuple(x)
res1.append(p)
# printing results
print("Frequency Tuple list : " + str(res1))
|
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
|
Python - Assign Frequency to Tuples
# Python3 code to demonstrate working of
# Assign Frequency to Tuples
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# one-liner to solve problem
# assign Frequency as last element of tuple
res = []
for i in test_list:
if i not in res:
res.append(i)
res1 = []
for i in res:
x = list(i)
x.append(test_list.count(i))
p = tuple(x)
res1.append(p)
# printing results
print("Frequency Tuple list : " + str(res1))
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
[END]
|
Python - Assign Frequency to Tuples
|
https://www.geeksforgeeks.org/python-assign-frequency-to-tuples/
|
# Python3 code to demonstrate working of
# Assign Frequency to Tuples
import operator as op
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# one-liner to solve problem
# assign Frequency as last element of tuple
res = []
for i in test_list:
if i not in res:
res.append(i)
res1 = []
for i in res:
x = list(i)
x.append(op.countOf(test_list, i))
p = tuple(x)
res1.append(p)
# printing results
print("Frequency Tuple list : " + str(res1))
|
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
|
Python - Assign Frequency to Tuples
# Python3 code to demonstrate working of
# Assign Frequency to Tuples
import operator as op
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# one-liner to solve problem
# assign Frequency as last element of tuple
res = []
for i in test_list:
if i not in res:
res.append(i)
res1 = []
for i in res:
x = list(i)
x.append(op.countOf(test_list, i))
p = tuple(x)
res1.append(p)
# printing results
print("Frequency Tuple list : " + str(res1))
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
[END]
|
Python - Assign Frequency to Tuples
|
https://www.geeksforgeeks.org/python-assign-frequency-to-tuples/
|
# Python program for the above approach
# Function to assign frequency
def assign_frequency(lst):
freq_dict = {}
for tup in lst:
if tup in freq_dict:
freq_dict[tup] += 1
else:
freq_dict[tup] = 1
return [(key + (value,)) for key, value in freq_dict.items()]
# Driver Code
lst = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
print(assign_frequency(lst))
|
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
|
Python - Assign Frequency to Tuples
# Python program for the above approach
# Function to assign frequency
def assign_frequency(lst):
freq_dict = {}
for tup in lst:
if tup in freq_dict:
freq_dict[tup] += 1
else:
freq_dict[tup] = 1
return [(key + (value,)) for key, value in freq_dict.items()]
# Driver Code
lst = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
print(assign_frequency(lst))
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
[END]
|
Python - Assign Frequency to Tuples
|
https://www.geeksforgeeks.org/python-assign-frequency-to-tuples/
|
from collections import Counter
from functools import reduce
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# using reduce to merge tuples and sum their counts
res = reduce(lambda x, y: x + y, [Counter([t]) for t in test_list])
# creating list of tuples with frequency as last element
res = [(*key, val) for key, val in res.items()]
# sorting the list in descending order of frequency
res.sort(key=lambda x: x[-1], reverse=True)
# printing results
print("Frequency Tuple list : " + str(res))
# This code is contributed by Rayudu.
|
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
|
Python - Assign Frequency to Tuples
from collections import Counter
from functools import reduce
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# using reduce to merge tuples and sum their counts
res = reduce(lambda x, y: x + y, [Counter([t]) for t in test_list])
# creating list of tuples with frequency as last element
res = [(*key, val) for key, val in res.items()]
# sorting the list in descending order of frequency
res.sort(key=lambda x: x[-1], reverse=True)
# printing results
print("Frequency Tuple list : " + str(res))
# This code is contributed by Rayudu.
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
[END]
|
Python - Assign Frequency to Tuples
|
https://www.geeksforgeeks.org/python-assign-frequency-to-tuples/
|
import itertools
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# use groupby to get unique rows and their counts
res = []
for row, group in itertools.groupby(sorted(test_list)):
count = sum(1 for _ in group)
res.append(row + (count,))
# print result
print("Frequency Tuple list : " + str(res))
# This code is contributed by Vinay pinjala.
|
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
|
Python - Assign Frequency to Tuples
import itertools
# initializing list
test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9,), (2, 7)]
# printing original list
print("The original list is : " + str(test_list))
# use groupby to get unique rows and their counts
res = []
for row, group in itertools.groupby(sorted(test_list)):
count = sum(1 for _ in group)
res.append(row + (count,))
# print result
print("Frequency Tuple list : " + str(res))
# This code is contributed by Vinay pinjala.
#Output : The original list is : [(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)]
[END]
|
Python - Records with Value at K index
|
https://www.geeksforgeeks.org/python-records-with-value-at-k-index/
|
# Python3 code to demonstrate working of
# Records with Value at K index
# Using loop
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = 3
# initialize K
K = 1
# Records with Value at K index
# Using loop
# using y for K = 1
res = []
for x, y, z in test_list:
if y == ele:
res.append((x, y, z))
# printing result
print("The tuples of element at Kth position : " + str(res))
|
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
|
Python - Records with Value at K index
# Python3 code to demonstrate working of
# Records with Value at K index
# Using loop
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = 3
# initialize K
K = 1
# Records with Value at K index
# Using loop
# using y for K = 1
res = []
for x, y, z in test_list:
if y == ele:
res.append((x, y, z))
# printing result
print("The tuples of element at Kth position : " + str(res))
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
[END]
|
Python - Records with Value at K index
|
https://www.geeksforgeeks.org/python-records-with-value-at-k-index/
|
# Python3 code to demonstrate working of
# Records with Value at K index
# Using enumerate() + list comprehension
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = 3
# initialize K
K = 1
# Records with Value at K index
# Using enumerate() + list comprehension
res = [b for a, b in enumerate(test_list) if b[K] == ele]
# printing result
print("The tuples of element at Kth position : " + str(res))
|
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
|
Python - Records with Value at K index
# Python3 code to demonstrate working of
# Records with Value at K index
# Using enumerate() + list comprehension
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = 3
# initialize K
K = 1
# Records with Value at K index
# Using enumerate() + list comprehension
res = [b for a, b in enumerate(test_list) if b[K] == ele]
# printing result
print("The tuples of element at Kth position : " + str(res))
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
[END]
|
Python - Records with Value at K index
|
https://www.geeksforgeeks.org/python-records-with-value-at-k-index/
|
# Python3 code to demonstrate working of
# Records with Value at K index
# Using filter()
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = 3
# initialize K
K = 1
# Records with Value at K index
# Using filter()
res = list(filter(lambda x: x[K] == ele, test_list))
# printing result
print("The tuples of element at Kth position : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy
|
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
|
Python - Records with Value at K index
# Python3 code to demonstrate working of
# Records with Value at K index
# Using filter()
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = 3
# initialize K
K = 1
# Records with Value at K index
# Using filter()
res = list(filter(lambda x: x[K] == ele, test_list))
# printing result
print("The tuples of element at Kth position : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
[END]
|
Python - Records with Value at K index
|
https://www.geeksforgeeks.org/python-records-with-value-at-k-index/
|
def filter_by_index(lst, ele, k):
if not lst:
return []
if lst[0][k] == ele:
return [lst[0]] + filter_by_index(lst[1:], ele, k)
else:
return filter_by_index(lst[1:], ele, k)
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = 3
# initialize K
K = 1
res = filter_by_index(test_list, ele, K)
# printing result
print("The tuples of element at Kth position : " + str(res))
# this code contributed by tvsk
|
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
|
Python - Records with Value at K index
def filter_by_index(lst, ele, k):
if not lst:
return []
if lst[0][k] == ele:
return [lst[0]] + filter_by_index(lst[1:], ele, k)
else:
return filter_by_index(lst[1:], ele, k)
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = 3
# initialize K
K = 1
res = filter_by_index(test_list, ele, K)
# printing result
print("The tuples of element at Kth position : " + str(res))
# this code contributed by tvsk
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
[END]
|
Python - Records with Value at K index
|
https://www.geeksforgeeks.org/python-records-with-value-at-k-index/
|
# Python3 code to demonstrate working of
# Records with Value at K index
# Using map() and lambda function
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = 3
# initialize K
K = 1
# Records with Value at K index
# Using map() and lambda function
res = list(filter(lambda tup: tup[K] == ele, test_list))
# printing result
print("The tuples of element at Kth position : " + str(res))
|
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
|
Python - Records with Value at K index
# Python3 code to demonstrate working of
# Records with Value at K index
# Using map() and lambda function
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# printing original list
print("The original list is : " + str(test_list))
# initialize ele
ele = 3
# initialize K
K = 1
# Records with Value at K index
# Using map() and lambda function
res = list(filter(lambda tup: tup[K] == ele, test_list))
# printing result
print("The tuples of element at Kth position : " + str(res))
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
[END]
|
Python - Records with Value at K index
|
https://www.geeksforgeeks.org/python-records-with-value-at-k-index/
|
import numpy as np
# Initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# Initialize ele
ele = 3
# Initialize K
K = 1
# Records with Value at K index using numpy
res = np.array(test_list)[np.array(test_list)[:, K] == ele]
# Printing result
print("The tuples of element at Kth position : " + str(res.tolist()))
# this code is contributed by Rayudu.
|
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
|
Python - Records with Value at K index
import numpy as np
# Initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
# Initialize ele
ele = 3
# Initialize K
K = 1
# Records with Value at K index using numpy
res = np.array(test_list)[np.array(test_list)[:, K] == ele]
# Printing result
print("The tuples of element at Kth position : " + str(res.tolist()))
# this code is contributed by Rayudu.
#Output : The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
[END]
|
Python - Test if tuple is distinct
|
https://www.geeksforgeeks.org/python-test-if-tuple-is-distinct/
|
# Python3 code to demonstrate working of
# Test if tuple is distinct
# Using loop
# initialize tuple
test_tup = (1, 4, 5, 6, 1, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Test if tuple is distinct
# Using loop
res = True
temp = set()
for ele in test_tup:
if ele in temp:
res = False
break
temp.add(ele)
# printing result
print("Is tuple distinct ? : " + str(res))
|
#Output : The original tuple is : (1, 4, 5, 6, 1, 4)
|
Python - Test if tuple is distinct
# Python3 code to demonstrate working of
# Test if tuple is distinct
# Using loop
# initialize tuple
test_tup = (1, 4, 5, 6, 1, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Test if tuple is distinct
# Using loop
res = True
temp = set()
for ele in test_tup:
if ele in temp:
res = False
break
temp.add(ele)
# printing result
print("Is tuple distinct ? : " + str(res))
#Output : The original tuple is : (1, 4, 5, 6, 1, 4)
[END]
|
Python - Test if tuple is distinct
|
https://www.geeksforgeeks.org/python-test-if-tuple-is-distinct/
|
# Python3 code to demonstrate working of
# Test if tuple is distinct
# Using set() + len()
# initialize tuple
test_tup = (1, 4, 5, 6)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Test if tuple is distinct
# Using set() + len()
res = len(set(test_tup)) == len(test_tup)
# printing result
print("Is tuple distinct ? : " + str(res))
|
#Output : The original tuple is : (1, 4, 5, 6, 1, 4)
|
Python - Test if tuple is distinct
# Python3 code to demonstrate working of
# Test if tuple is distinct
# Using set() + len()
# initialize tuple
test_tup = (1, 4, 5, 6)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Test if tuple is distinct
# Using set() + len()
res = len(set(test_tup)) == len(test_tup)
# printing result
print("Is tuple distinct ? : " + str(res))
#Output : The original tuple is : (1, 4, 5, 6, 1, 4)
[END]
|
Python - Test if tuple is distinct
|
https://www.geeksforgeeks.org/python-test-if-tuple-is-distinct/
|
# Python3 code to demonstrate working of
# Test if tuple is distinct
# Using collections.Counter()
# importing collections for Counter
import collections
# initialize tuple
test_tup = (1, 4, 5, 6, 1, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Test if tuple is distinct
# Using collections.Counter()
res = max(collections.Counter(test_tup).values()) == 1
# printing result
print("Is tuple distinct ? : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy
|
#Output : The original tuple is : (1, 4, 5, 6, 1, 4)
|
Python - Test if tuple is distinct
# Python3 code to demonstrate working of
# Test if tuple is distinct
# Using collections.Counter()
# importing collections for Counter
import collections
# initialize tuple
test_tup = (1, 4, 5, 6, 1, 4)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Test if tuple is distinct
# Using collections.Counter()
res = max(collections.Counter(test_tup).values()) == 1
# printing result
print("Is tuple distinct ? : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy
#Output : The original tuple is : (1, 4, 5, 6, 1, 4)
[END]
|
Python - Test if tuple is distinct
|
https://www.geeksforgeeks.org/python-test-if-tuple-is-distinct/
|
# initialize tuple
test_tup = (1, 4, 5, 6, 1, 4)
# Test if tuple is distinct
# Using boolean flag
distinct = True
for i in range(len(test_tup)):
if test_tup[i] in test_tup[i + 1 :]:
distinct = False
break
# printing result
print("Is tuple distinct? : " + str(distinct))
|
#Output : The original tuple is : (1, 4, 5, 6, 1, 4)
|
Python - Test if tuple is distinct
# initialize tuple
test_tup = (1, 4, 5, 6, 1, 4)
# Test if tuple is distinct
# Using boolean flag
distinct = True
for i in range(len(test_tup)):
if test_tup[i] in test_tup[i + 1 :]:
distinct = False
break
# printing result
print("Is tuple distinct? : " + str(distinct))
#Output : The original tuple is : (1, 4, 5, 6, 1, 4)
[END]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-which-have-all-elements-divisible-by-k-from-a-list-of-tuples/
|
# Python3 code to demonstrate working of
# K Multiple Elements Tuples
# Using list comprehension + all()
# initializing list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# all() used to filter elements
res = [sub for sub in test_list if all(ele % K == 0 for ele in sub)]
# printing result
print("K Multiple elements tuples : " + str(res))
|
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
# Python3 code to demonstrate working of
# K Multiple Elements Tuples
# Using list comprehension + all()
# initializing list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# all() used to filter elements
res = [sub for sub in test_list if all(ele % K == 0 for ele in sub)]
# printing result
print("K Multiple elements tuples : " + str(res))
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
[END]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-which-have-all-elements-divisible-by-k-from-a-list-of-tuples/
|
# Python3 code to demonstrate working of
# K Multiple Elements Tuples
# Using filter() + lambda + all()
# initializing list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# filter() + lambda for filter operation
res = list(filter(lambda sub: all(ele % K == 0 for ele in sub), test_list))
# printing result
print("K Multiple elements tuples : " + str(res))
|
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
# Python3 code to demonstrate working of
# K Multiple Elements Tuples
# Using filter() + lambda + all()
# initializing list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# filter() + lambda for filter operation
res = list(filter(lambda sub: all(ele % K == 0 for ele in sub), test_list))
# printing result
print("K Multiple elements tuples : " + str(res))
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
[END]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-which-have-all-elements-divisible-by-k-from-a-list-of-tuples/
|
# Initialize list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# Initialize K
K = 6
# list comprehension
result = [tup for tup in test_list if not any(x % K != 0 for x in tup)]
# printing original list
print("The original list is : " + str(test_list))
# Print the list of tuples whose elements are multiples of K
print("K Multiple elements tuples : " + str(result))
|
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
# Initialize list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# Initialize K
K = 6
# list comprehension
result = [tup for tup in test_list if not any(x % K != 0 for x in tup)]
# printing original list
print("The original list is : " + str(test_list))
# Print the list of tuples whose elements are multiples of K
print("K Multiple elements tuples : " + str(result))
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
[END]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-which-have-all-elements-divisible-by-k-from-a-list-of-tuples/
|
# Python3 code to demonstrate working of
# K Multiple Elements Tuples
# initializing list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
res = []
for i in test_list:
c = 0
for j in i:
if j % K == 0:
c += 1
if c == len(i):
res.append(i)
# printing result
print("K Multiple elements tuples : " + str(res))
|
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
# Python3 code to demonstrate working of
# K Multiple Elements Tuples
# initializing list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
res = []
for i in test_list:
c = 0
for j in i:
if j % K == 0:
c += 1
if c == len(i):
res.append(i)
# printing result
print("K Multiple elements tuples : " + str(res))
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
[END]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-which-have-all-elements-divisible-by-k-from-a-list-of-tuples/
|
# Python3 code to demonstrate working of
# K Multiple Elements Tuples
def even_tuple(lst, K, newlst=[], start=0):
if start == len(lst):
return newlst
for i in lst[start]:
if i % K != 0:
return even_tuple(lst, K, newlst, start + 1)
else:
newlst.append(lst[start])
return even_tuple(lst, K, newlst, start + 1)
# initializing list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
res = even_tuple(test_list, K)
# printing result
print("K Multiple elements tuples : " + str(res))
# this code contributed by tvsk
|
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
# Python3 code to demonstrate working of
# K Multiple Elements Tuples
def even_tuple(lst, K, newlst=[], start=0):
if start == len(lst):
return newlst
for i in lst[start]:
if i % K != 0:
return even_tuple(lst, K, newlst, start + 1)
else:
newlst.append(lst[start])
return even_tuple(lst, K, newlst, start + 1)
# initializing list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
res = even_tuple(test_list, K)
# printing result
print("K Multiple elements tuples : " + str(res))
# this code contributed by tvsk
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
[END]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-which-have-all-elements-divisible-by-k-from-a-list-of-tuples/
|
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
K = 6
# define a function to check if all elements in a tuple are multiples of K
def is_multiple_of_K(tup):
return all(ele % K == 0 for ele in tup)
# use filter() to keep only tuples that satisfy the condition
res = list(filter(is_multiple_of_K, test_list))
# printing original list
print("The original list is : " + str(test_list))
print("K Multiple elements tuples : " + str(res))
|
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
K = 6
# define a function to check if all elements in a tuple are multiples of K
def is_multiple_of_K(tup):
return all(ele % K == 0 for ele in tup)
# use filter() to keep only tuples that satisfy the condition
res = list(filter(is_multiple_of_K, test_list))
# printing original list
print("The original list is : " + str(test_list))
print("K Multiple elements tuples : " + str(res))
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
[END]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-which-have-all-elements-divisible-by-k-from-a-list-of-tuples/
|
import numpy as np
# initializing the list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# printing the original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# converting the list to a numpy array
arr = np.array(test_list)
# using np.all() to filter elements
res = arr[np.all(arr % K == 0, axis=1)]
# printing the result
print("K Multiple elements tuples : " + str(res))
|
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
|
Python program to find tuples which have all elements divisible by K from a list of tuples
import numpy as np
# initializing the list
test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
# printing the original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# converting the list to a numpy array
arr = np.array(test_list)
# using np.all() to filter elements
res = arr[np.all(arr % K == 0, axis=1)]
# printing the result
print("K Multiple elements tuples : " + str(res))
#Output : The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
[END]
|
Python program to find Tuples with positive elements in List of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-with-positive-elements-in-list-of-tuples/
|
# Python3 code to demonstrate working of
# Positive Tuples in List
# Using list comprehension + all()
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
# all() to check each element
res = [sub for sub in test_list if all(ele >= 0 for ele in sub)]
# printing result
print("Positive elements Tuples : " + str(res))
|
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
|
Python program to find Tuples with positive elements in List of tuples
# Python3 code to demonstrate working of
# Positive Tuples in List
# Using list comprehension + all()
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
# all() to check each element
res = [sub for sub in test_list if all(ele >= 0 for ele in sub)]
# printing result
print("Positive elements Tuples : " + str(res))
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
[END]
|
Python program to find Tuples with positive elements in List of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-with-positive-elements-in-list-of-tuples/
|
# Python3 code to demonstrate working of
# Positive Tuples in List
# Using filter() + lambda + all()
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
# all() to check each element
res = list(filter(lambda sub: all(ele >= 0 for ele in sub), test_list))
# printing result
print("Positive elements Tuples : " + str(res))
|
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
|
Python program to find Tuples with positive elements in List of tuples
# Python3 code to demonstrate working of
# Positive Tuples in List
# Using filter() + lambda + all()
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
# all() to check each element
res = list(filter(lambda sub: all(ele >= 0 for ele in sub), test_list))
# printing result
print("Positive elements Tuples : " + str(res))
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
[END]
|
Python program to find Tuples with positive elements in List of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-with-positive-elements-in-list-of-tuples/
|
# Python3 code to demonstrate working of
# Positive Tuples in List
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
res = []
for i in test_list:
x = list(map(str, i))
a = " ".join(x)
if a.find("-") == -1:
res.append(i)
# printing result
print("Positive elements Tuples : " + str(res))
|
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
|
Python program to find Tuples with positive elements in List of tuples
# Python3 code to demonstrate working of
# Positive Tuples in List
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
res = []
for i in test_list:
x = list(map(str, i))
a = " ".join(x)
if a.find("-") == -1:
res.append(i)
# printing result
print("Positive elements Tuples : " + str(res))
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
[END]
|
Python program to find Tuples with positive elements in List of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-with-positive-elements-in-list-of-tuples/
|
# Python3 code to demonstrate working of
# Positive Tuples in List
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
res = []
for i in test_list:
x = sorted(i)
x = list(map(str, x))
b = "".join(x)
if not b.startswith("-"):
res.append(i)
# printing result
print("Positive elements Tuples : " + str(res))
|
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
|
Python program to find Tuples with positive elements in List of tuples
# Python3 code to demonstrate working of
# Positive Tuples in List
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
res = []
for i in test_list:
x = sorted(i)
x = list(map(str, x))
b = "".join(x)
if not b.startswith("-"):
res.append(i)
# printing result
print("Positive elements Tuples : " + str(res))
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
[END]
|
Python program to find Tuples with positive elements in List of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-with-positive-elements-in-list-of-tuples/
|
# Python3 code to demonstrate working of
# Positive Tuples in List
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
res = []
def fun(x):
c = 0
for i in x:
if i > 0:
c += 1
if c == len(x):
return True
return False
for i in test_list:
if fun(i):
res.append(i)
# printing result
print("Positive elements Tuples : " + str(res))
|
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
|
Python program to find Tuples with positive elements in List of tuples
# Python3 code to demonstrate working of
# Positive Tuples in List
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
res = []
def fun(x):
c = 0
for i in x:
if i > 0:
c += 1
if c == len(x):
return True
return False
for i in test_list:
if fun(i):
res.append(i)
# printing result
print("Positive elements Tuples : " + str(res))
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
[END]
|
Python program to find Tuples with positive elements in List of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-with-positive-elements-in-list-of-tuples/
|
# Python3 code to demonstrate working of
# Positive Tuples in List
# Using list comprehension + all()
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
# not any() to check each element
res = [sub for sub in test_list if not any(ele < 0 for ele in sub)]
# printing result
print("Positive elements Tuples : " + str(res))
|
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
|
Python program to find Tuples with positive elements in List of tuples
# Python3 code to demonstrate working of
# Positive Tuples in List
# Using list comprehension + all()
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# printing original list
print("The original list is : " + str(test_list))
# not any() to check each element
res = [sub for sub in test_list if not any(ele < 0 for ele in sub)]
# printing result
print("Positive elements Tuples : " + str(res))
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
[END]
|
Python program to find Tuples with positive elements in List of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-with-positive-elements-in-list-of-tuples/
|
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
result = []
# printing original list
print("The original list is : " + str(test_list))
for tup in test_list:
positive = True
for ele in tup:
if ele < 0:
positive = False
break
if positive:
result.append(tup)
# printing result
print("Positive elements Tuples : " + str(result))
# This code contributed by Vinay Pinjala.
|
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
|
Python program to find Tuples with positive elements in List of tuples
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
result = []
# printing original list
print("The original list is : " + str(test_list))
for tup in test_list:
positive = True
for ele in tup:
if ele < 0:
positive = False
break
if positive:
result.append(tup)
# printing result
print("Positive elements Tuples : " + str(result))
# This code contributed by Vinay Pinjala.
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
[END]
|
Python program to find Tuples with positive elements in List of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-with-positive-elements-in-list-of-tuples/
|
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# Create an empty list to hold the tuples with positive elements
res = []
# Loop through each tuple in the original list
for tup in test_list:
# Check if all elements in the tuple are positive
if all(ele >= 0 for ele in tup):
# If so, add the tuple to the result list
res.append(tup)
# Print the result
print("Positive elements Tuples : " + str(res))
|
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
|
Python program to find Tuples with positive elements in List of tuples
# initializing list
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# Create an empty list to hold the tuples with positive elements
res = []
# Loop through each tuple in the original list
for tup in test_list:
# Check if all elements in the tuple are positive
if all(ele >= 0 for ele in tup):
# If so, add the tuple to the result list
res.append(tup)
# Print the result
print("Positive elements Tuples : " + str(res))
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
[END]
|
Python program to find Tuples with positive elements in List of tuples
|
https://www.geeksforgeeks.org/python-program-to-find-tuples-with-positive-elements-in-list-of-tuples/
|
import re
# initializing list of tuples containing positive and negative integers
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# empty list to store tuples with only positive integers
positive_list = []
# loop through each tuple in the list of tuples
for tup in test_list:
# convert tuple to string using str()
str_tup = str(tup)
# use re.search() to check if string contains a "-"
if not re.search("-", str_tup):
# if string does not contain "-", append tuple to positive_list
positive_list.append(tup)
# print original list of tuples and list of tuples with only positive integers
print("Original List: ", test_list)
print("Positive List: ", positive_list)
|
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
|
Python program to find Tuples with positive elements in List of tuples
import re
# initializing list of tuples containing positive and negative integers
test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
# empty list to store tuples with only positive integers
positive_list = []
# loop through each tuple in the list of tuples
for tup in test_list:
# convert tuple to string using str()
str_tup = str(tup)
# use re.search() to check if string contains a "-"
if not re.search("-", str_tup):
# if string does not contain "-", append tuple to positive_list
positive_list.append(tup)
# print original list of tuples and list of tuples with only positive integers
print("Original List: ", test_list)
print("Positive List: ", positive_list)
#Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]??????
#Output : [(4, 5, 9
[END]
|
Python - Count tuples occurrence in list of tuples
|
https://www.geeksforgeeks.org/python-count-tuples-occurrence-in-list-of-tuples/
|
# Python code to count unique
# tuples in list of list
import collections
Output = collections.defaultdict(int)
# List initialization
Input = [
[("hi", "bye")],
[("Geeks", "forGeeks")],
[("a", "b")],
[("hi", "bye")],
[("a", "b")],
]
# Using iteration
for elem in Input:
Output[elem[0]] += 1
# Printing output
print(Output)
|
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
|
Python - Count tuples occurrence in list of tuples
# Python code to count unique
# tuples in list of list
import collections
Output = collections.defaultdict(int)
# List initialization
Input = [
[("hi", "bye")],
[("Geeks", "forGeeks")],
[("a", "b")],
[("hi", "bye")],
[("a", "b")],
]
# Using iteration
for elem in Input:
Output[elem[0]] += 1
# Printing output
print(Output)
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
[END]
|
Python - Count tuples occurrence in list of tuples
|
https://www.geeksforgeeks.org/python-count-tuples-occurrence-in-list-of-tuples/
|
# Python code to count unique
# tuples in list of list
# Importing
from collections import Counter
from itertools import chain
# List initialization
Input = [
[("hi", "bye")],
[("Geeks", "forGeeks")],
[("a", "b")],
[("hi", "bye")],
[("a", "b")],
]
# Using counter and chain
Output = Counter(chain(*Input))
# Printing output
print(Output)
|
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
|
Python - Count tuples occurrence in list of tuples
# Python code to count unique
# tuples in list of list
# Importing
from collections import Counter
from itertools import chain
# List initialization
Input = [
[("hi", "bye")],
[("Geeks", "forGeeks")],
[("a", "b")],
[("hi", "bye")],
[("a", "b")],
]
# Using counter and chain
Output = Counter(chain(*Input))
# Printing output
print(Output)
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
[END]
|
Python - Count tuples occurrence in list of tuples
|
https://www.geeksforgeeks.org/python-count-tuples-occurrence-in-list-of-tuples/
|
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
check_ele = ("a", "b")
x = [i for i in Input if i == check_ele]
print("tuple ('a', 'b') occurs", len(x), "times")
|
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
|
Python - Count tuples occurrence in list of tuples
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
check_ele = ("a", "b")
x = [i for i in Input if i == check_ele]
print("tuple ('a', 'b') occurs", len(x), "times")
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
[END]
|
Python - Count tuples occurrence in list of tuples
|
https://www.geeksforgeeks.org/python-count-tuples-occurrence-in-list-of-tuples/
|
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
check_ele = ("a", "b")
x = [i for a, i in enumerate(Input) if i == check_ele]
print("tuple ('a', 'b') occurs", len(x), "times")
|
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
|
Python - Count tuples occurrence in list of tuples
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
check_ele = ("a", "b")
x = [i for a, i in enumerate(Input) if i == check_ele]
print("tuple ('a', 'b') occurs", len(x), "times")
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
[END]
|
Python - Count tuples occurrence in list of tuples
|
https://www.geeksforgeeks.org/python-count-tuples-occurrence-in-list-of-tuples/
|
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
check_ele = ("a", "b")
x = list(filter(lambda i: (i == check_ele), Input))
print("tuple ('a', 'b') occurs", len(x), "times")
|
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
|
Python - Count tuples occurrence in list of tuples
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
check_ele = ("a", "b")
x = list(filter(lambda i: (i == check_ele), Input))
print("tuple ('a', 'b') occurs", len(x), "times")
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
[END]
|
Python - Count tuples occurrence in list of tuples
|
https://www.geeksforgeeks.org/python-count-tuples-occurrence-in-list-of-tuples/
|
from collections import Counter
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
check_ele = ("a", "b")
x = Counter(Input)
print("tuple ('a', 'b') occurs", x[check_ele], "times")
|
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
|
Python - Count tuples occurrence in list of tuples
from collections import Counter
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
check_ele = ("a", "b")
x = Counter(Input)
print("tuple ('a', 'b') occurs", x[check_ele], "times")
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
[END]
|
Python - Count tuples occurrence in list of tuples
|
https://www.geeksforgeeks.org/python-count-tuples-occurrence-in-list-of-tuples/
|
import operator as op
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
check_ele = ("a", "b")
print(op.countOf(Input, check_ele))
|
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
|
Python - Count tuples occurrence in list of tuples
import operator as op
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
check_ele = ("a", "b")
print(op.countOf(Input, check_ele))
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
[END]
|
Python - Count tuples occurrence in list of tuples
|
https://www.geeksforgeeks.org/python-count-tuples-occurrence-in-list-of-tuples/
|
# Initializing tuples in a list
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
# creating empty dictionary
count_tuples = dict()
# using for loop to iterate every value in Input
for i in Input:
if i not in count_tuples: # checking if element i present in dictionary or not
count_tuples[i] = 1
else:
count_tuples[i] += 1
# printing dictionary of elements and their count
print(count_tuples)
|
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
|
Python - Count tuples occurrence in list of tuples
# Initializing tuples in a list
Input = [("hi", "bye"), ("Geeks", "forGeeks"), ("a", "b"), ("hi", "bye"), ("a", "b")]
# creating empty dictionary
count_tuples = dict()
# using for loop to iterate every value in Input
for i in Input:
if i not in count_tuples: # checking if element i present in dictionary or not
count_tuples[i] = 1
else:
count_tuples[i] += 1
# printing dictionary of elements and their count
print(count_tuples)
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
[END]
|
Python - Count tuples occurrence in list of tuples
|
https://www.geeksforgeeks.org/python-count-tuples-occurrence-in-list-of-tuples/
|
# Python code to count unique
# tuples in list of list
# List initialization
Input = [
[("hi", "bye")],
[("Geeks", "forGeeks")],
[("a", "b")],
[("hi", "bye")],
[("a", "b")],
]
# Using map() function and tuple() constructor
Output = {}
for elem in Input:
t = tuple(map(tuple, elem))
Output[t] = Output.get(t, 0) + 1
# Printing output
print(Output)
|
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
|
Python - Count tuples occurrence in list of tuples
# Python code to count unique
# tuples in list of list
# List initialization
Input = [
[("hi", "bye")],
[("Geeks", "forGeeks")],
[("a", "b")],
[("hi", "bye")],
[("a", "b")],
]
# Using map() function and tuple() constructor
Output = {}
for elem in Input:
t = tuple(map(tuple, elem))
Output[t] = Output.get(t, 0) + 1
# Printing output
print(Output)
#Output : defaultdict(<class 'int'>, {('Geeks', 'forGeeks'): 1, ('hi', 'bye'): 2, ('a', 'b'): 2})
[END]
|
Python - Removing duplicates from tuple
|
https://www.geeksforgeeks.org/python-removing-duplicates-from-tuple/
|
# Python3 code to demonstrate working of
# Removing duplicates from tuple
# using tuple() + set()
# initialize tuple
test_tup = (1, 3, 5, 2, 3, 5, 1, 1, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Removing duplicates from tuple
# using tuple() + set()
res = tuple(set(test_tup))
# printing result
print("The tuple after removing duplicates : " + str(res))
|
#Output : The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
|
Python - Removing duplicates from tuple
# Python3 code to demonstrate working of
# Removing duplicates from tuple
# using tuple() + set()
# initialize tuple
test_tup = (1, 3, 5, 2, 3, 5, 1, 1, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Removing duplicates from tuple
# using tuple() + set()
res = tuple(set(test_tup))
# printing result
print("The tuple after removing duplicates : " + str(res))
#Output : The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
[END]
|
Python - Removing duplicates from tuple
|
https://www.geeksforgeeks.org/python-removing-duplicates-from-tuple/
|
# Python3 code to demonstrate working of
# Removing duplicates from tuple
# using OrderedDict() + fromkeys()
from collections import OrderedDict
# initialize tuple
test_tup = (1, 3, 5, 2, 3, 5, 1, 1, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Removing duplicates from tuple
# using OrderedDict() + fromkeys()
res = tuple(OrderedDict.fromkeys(test_tup).keys())
# printing result
print("The tuple after removing duplicates : " + str(res))
|
#Output : The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
|
Python - Removing duplicates from tuple
# Python3 code to demonstrate working of
# Removing duplicates from tuple
# using OrderedDict() + fromkeys()
from collections import OrderedDict
# initialize tuple
test_tup = (1, 3, 5, 2, 3, 5, 1, 1, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Removing duplicates from tuple
# using OrderedDict() + fromkeys()
res = tuple(OrderedDict.fromkeys(test_tup).keys())
# printing result
print("The tuple after removing duplicates : " + str(res))
#Output : The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
[END]
|
Python - Removing duplicates from tuple
|
https://www.geeksforgeeks.org/python-removing-duplicates-from-tuple/
|
# Python3 code to demonstrate working of
# Removing duplicates from tuple
# initialize tuple
test_tup = (1, 3, 5, 2, 3, 5, 1, 1, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Removing duplicates from tuple
x = []
for i in test_tup:
if i not in x:
x.append(i)
res = tuple(x)
# printing result
print("The tuple after removing duplicates : " + str(res))
|
#Output : The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
|
Python - Removing duplicates from tuple
# Python3 code to demonstrate working of
# Removing duplicates from tuple
# initialize tuple
test_tup = (1, 3, 5, 2, 3, 5, 1, 1, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Removing duplicates from tuple
x = []
for i in test_tup:
if i not in x:
x.append(i)
res = tuple(x)
# printing result
print("The tuple after removing duplicates : " + str(res))
#Output : The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
[END]
|
Python - Removing duplicates from tuple
|
https://www.geeksforgeeks.org/python-removing-duplicates-from-tuple/
|
# Python3 code to demonstrate working of
# Removing duplicates from tuple using list comprehension
# initialize tuple
test_tup = (1, 3, 5, 2, 3, 5, 1, 1, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Removing duplicates from tuple using list comprehension
# creating a list of only unique elements from the tuple
res = tuple([x for i, x in enumerate(test_tup) if x not in test_tup[:i]])
# printing result
print("The tuple after removing duplicates : " + str(res))
# This code is contributed by Vinay Pinjala.
|
#Output : The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
|
Python - Removing duplicates from tuple
# Python3 code to demonstrate working of
# Removing duplicates from tuple using list comprehension
# initialize tuple
test_tup = (1, 3, 5, 2, 3, 5, 1, 1, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Removing duplicates from tuple using list comprehension
# creating a list of only unique elements from the tuple
res = tuple([x for i, x in enumerate(test_tup) if x not in test_tup[:i]])
# printing result
print("The tuple after removing duplicates : " + str(res))
# This code is contributed by Vinay Pinjala.
#Output : The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
[END]
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.