Found 10476 Articles for Python

Count tuples occurrence in list of tuples in Python

Pradeep Elance
Updated on 04-Jun-2020 11:39:00

568 Views

A list is made up of tuples as its element. In this article we will count the number of unique tuples present in the list.With defaultdictWe treat the given list as a defaultdict data container and count the elements in it using the in condition.Example Live Demoimport collections Alist = [[('Mon', 'Wed')], [('Mon')], [('Tue')], [('Mon', 'Wed')] ] # Given list print("Given list:", Alist) res = collections.defaultdict(int) for elem in Alist:    res[elem[0]] += 1 print("Count of tuples present in the list:", res)OutputRunning the above code gives us the following result −Given list: [[('Mon', 'Wed')], ['Mon'], ['Tue'], [('Mon', 'Wed')]] Count of tuples ... Read More

Count the sublists containing given element in a list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:37:17

401 Views

The elements in a given list can also be present as another string in another variable. In this article we'll see how many times a given stream is present in a given list.With range and lenWe use the range and len function to keep track of the length of the list. Then use the in condition to find the number of times the string is present as an element in a list. A count variable initialized to zero keeps incrementing whenever the condition is met.Example Live DemoAlist = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] Bstring = 'Mon' # Given list print("Given ... Read More

Count the Number of matching characters in a pair of string in Python

Pradeep Elance
Updated on 04-Jun-2020 11:34:46

963 Views

We are given two strings. We need to find the count it of the characters in the first string which are also present in a second string.With setThe set function gives us unique values all the elements in a string. We also use the the & operator which finds the common elements between the two given strings.Example Live DemostrA = 'Tutorials Point' uniq_strA = set(strA) # Given String print("Given String", strA) strB = 'aeio' uniq_strB = set(strB) # Given String print("Search character strings", strB) common_chars = uniq_strA & uniq_strB print("Count of matching characters are : ", len(common_chars))OutputRunning the above code gives ... Read More

Count of elements matching particular condition in Python

Pradeep Elance
Updated on 04-Jun-2020 11:32:17

718 Views

In this article we will see how to get some selected elements out of a Python list. So we need to design some condition and only the elements satisfying that condition should be picked and their count to be printed.Wit in and sumIn this approach we use in condition to pick the elements and use some to get their count. 1 is used if the element is present else 0 is used for the result of in condition.Example Live DemoAlist = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] # Given list print("Given list:", Alist) cnt = sum(1 for i in Alist if i ... Read More

Count occurrences of an element in a list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:31:13

588 Views

In this article we are given a list and a string. We are required to find how many times the given string is present as an element in the list.With CounterThe counter function from collections module will give us the count of each element present in the list. From the counting result we can extract only that account fair the index matches with the value of the element we are searching for.Example Live Demofrom collections import Counter Alist = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] elem = 'Mon' # Given list and element print("Given list:", Alist) print("Given element:", elem) cnt = Counter(Alist) ... Read More

Count occurrences of a character in string in Python

Pradeep Elance
Updated on 04-Jun-2020 11:28:28

765 Views

We are given a string and a character. We want to find out how many times the given character is repeated in a given string.With range and lenWe design a for loop to match the character with every character present in the string which are accessed using index. The range and len function helps us determine how many times the matching has to be done when moving from left to right of the string.Example Live DemoAstr = "How do you do" char = 'o' # Given String and Character print("Given String:", Astr) print("Given Character:", char) res = 0 for i in ... Read More

Count occurrence of all elements of list in a tuple in Python

Pradeep Elance
Updated on 04-Jun-2020 11:25:26

446 Views

We have a list and tuple. We match the elements of the list with the elements of the tuple and account the number of elements in the table matching with the elements of the list.With CounterWe use the counter function from collections to get the count of every element in the tuple. Again design a for and in condition find those elements which are present in the the list and part of the counting result from the tuple.Example Live Demofrom collections import Counter Atup = ('Mon', 'Wed', 'Mon', 'Tue', 'Thu') Alist = ['Mon', 'Thu'] # Given Tuple and list print("Given tuple ... Read More

Longest Chunked Palindrome Decomposition in python

Arnab Chakraborty
Updated on 04-Jun-2020 11:23:32

284 Views

Suppose we have a text. We have to find the largest possible k such that there exists a[1], a[2], ..., a[k] such that: Each a[i] is a non-empty string; Their concatenation a[1] + a[2] + ... + a[k] is equal to the given text; For all i in range 1 to k, a[i] = a[{k+1 - i}].So, if the input is like "antaprezatepzapreanta", then the output will be 11, because we can split it like "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)".To solve this, we will follow these steps −start := 0, end := length of text - 1initialize temp1 and temp2 with empty stringsans = ... Read More

Count number of items in a dictionary value that is a list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:23:15

1K+ Views

We are given a Dictionary in which the values from the key value pair itself is a list. In this article we will see a how to count the number of items in this list which are present as values in the dictionary.With isinstanceHindi suppose we use isinstance function to find out if the value of the dictionary is a list. Then we increment a count variable whenever isinstance returns true.Example Live Demo# defining the dictionary Adict = {'Days': ["Mon", "Tue", "wed", "Thu"],    'time': "2 pm",    'Subjects':["Phy", "Chem", "Maths", "Bio"]    } print("Given dictionary:", Adict) count = 0 # ... Read More

Smallest Sufficient Team in Pyhton

Arnab Chakraborty
Updated on 04-Jun-2020 11:20:34

332 Views

Suppose for a project we have a list of required skills called req_skills, and a list of people. Here i-th people people[i] contains a list of skills that person has.Now suppose a sufficient team is defined as a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: As an example suppose team is [0, 1, 3] this represents the people with skills people[0], people[1], and people[3].We have to find the team of the smallest ... Read More

Advertisements