Programming Articles - Page 1963 of 3366

Find frequency of given character at every position in list of lists in Python

Pradeep Elance
Updated on 05-May-2020 10:19:58

271 Views

Lets consider a scenario where you have a list which is made of lists as its elements. We are interested in finding the frequency of one character at different positions of the inner lists. Below example will clarify the requirement.Consider a list of lists given below.listA = [['a', 'a', 'b'], ['a', 'c', 'b'], ['c', 'a', 'b'], ['c', 'a', 'a']]In the abobe list we have elements which are lists with 3 elements. If I consider the first inner list tit has a, a, b at positions 0, 1, 2. Similarly for the 3rd list it is c, a, b at 0, ... Read More

Find elements of a list by indices in Python

Pradeep Elance
Updated on 05-May-2020 10:17:19

6K+ Views

Consider two lists. The elements in the second list are numbers which needs to be considered as index position for elements of the first list. For this scenario we have the below python programs.With map and getitemWe can use the getitem magic method is used to access the list items. We can use it along with the map function, so that we get the result from first list which takes the elements from second list as its indics.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] listB = [0, 1, 3] print("Given list A:", listA) print("Given list B:", listB) ... Read More

New 21 Game in C++

Arnab Chakraborty
Updated on 05-May-2020 10:19:03

392 Views

Suppose Rima plays the following game, that is loosely based on the card game "21". So Rima starts with 0 points, and draws numbers while she has less than K points. Now, during each draw, she gains an integer number of points randomly from the range [1, W], where W is given, and that is an integer. Now each draw is independent and the outcomes have equal probabilities. Rima stops drawing numbers when she gets K or more points. We have to find the probability that she has N or less points?So if N = 6, K is 1 and ... Read More

Extract only characters from given string in Python

Pradeep Elance
Updated on 05-May-2020 10:16:10

3K+ Views

A piece of data may contain letters, numbers as well as special characters. If we are interested in extracting only the letters form this string of data, then we can use various options available in python.With isalphaThe isalpha function will check if the given character is an alphabet or not. We will use this inside a for loop which will fetch each character from the given string and check if it is an alphabet. The join method will capture only the valid characters into the result.Example Live DemostringA = "Qwer34^&t%y" # Given string print("Given string : ", stringA) # ... Read More

Extract numbers from list of strings in Python

Pradeep Elance
Updated on 05-May-2020 10:14:34

1K+ Views

While using python for data manipulation, we may come across lists whose elements are a mix of letters and numbers with a fixed pattern. In this article we will see how to separate the numbers form letters which can be used for future calculations.With splitThe split functions splits a string by help of a character that is treated as a separator. In the program below the list elements have hyphen as their separator between letters and text. We will use that along with a for loop to capture eachExample Live DemolistA = ['Mon-2', 'Wed-8', 'Thu-2', 'Fri-7'] # Given list print("Given ... Read More

Find And Replace in String in C++

Arnab Chakraborty
Updated on 05-May-2020 10:15:24

2K+ Views

Suppose we have a string S, we will perform some replacement operations that replace groups of letters with new ones. In each replacement operation there are 3 parameters − a starting index i, a source word x and a target word y. Now the rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. otherwise, we do nothing.So as an example, consider, if we have S = "abcd" and we have some replacement operation i = 2, x = "cd", y = "ffff", then because "cd" ... Read More

Equate two list index elements in Python

Pradeep Elance
Updated on 05-May-2020 10:13:02

591 Views

During data manipulation with Python , we may need to bring two lists together and equate the elements in each of them pair wise. Which means the element at index 0 from list 1 will be equated with element from index 0 of list2 and so on.With tupleThe tuple function will be leveraged to take elements form each list in sequence and matching them up. We first store the result in a temp string which has the pattern in which the output of matching up of the values form lists will be displayed.Example Live DemolistA = ['day1', 'day2', 'day3'] listB = ... Read More

Friends Of Appropriate Ages in C++

Arnab Chakraborty
Updated on 05-May-2020 10:12:39

261 Views

Suppose some people will make friend requests. We know their ages, these are stored in ages[i]. So this indicates that the age of the ith person. Now a A will NOT friend request person B (B != A) if any of the following conditions are true −age[B] age[A]age[B] > 100 && age[A] < 100Otherwise, A will friend request B. You can consider that if A requests B, B does not necessarily request A. And also, people will not friend request themselves. So we have to find how many total friend requests are made?Suppose if the age array is like ... Read More

Element with largest frequency in list in Python

Pradeep Elance
Updated on 05-May-2020 10:09:02

526 Views

A lot of statistical data analysis tries to find the values which have maximum frequency in a given list of values. Python provides multiple approaches using which we can find such value form a given list. Below are the approaches.Using CounterThe Counter function from collections module has a options which can directly find the most common element in a given list. We have the most_common function to which we pass a parameter 1 for only one element with highest frequency and pass 2 if we need two elements which have highest frequency.Example Live Demofrom collections import Counter # Given list ... Read More

Element repetition in list in Python

Pradeep Elance
Updated on 05-May-2020 10:08:10

520 Views

There are scenarios when we need to repeat the values in a list. This duplication of values can be achived in python in the following ways.Using nested for loopIt is a straight forward approach in which pick each element, take through a inner for loop to create its duplicate and then pass both of them to an outer for loop.Example Live Demo# Given list listA = ['Mon', 'Tue', 9, 3, 3] print("Given list : ", listA) # Adding another element for each element Newlist = [i for i in listA for n in (0, 1)] # Result print("New ... Read More

Advertisements