unique

Get unique values in List of Lists in python

Get unique values in List of Lists Question: I want to create a list (or set) of all unique values appearing in a list of lists in python. I have something like this: aList=[[‘a’,’b’], [‘a’, ‘b’,’c’], [‘a’]] and i would like the following: unique_values=[‘a’,’b’,’c’] I know that for a list of strings you can just …

Total answers: 6

pandas unique values multiple columns

pandas unique values multiple columns Question: df = pd.DataFrame({‘Col1’: [‘Bob’, ‘Joe’, ‘Bill’, ‘Mary’, ‘Joe’], ‘Col2’: [‘Joe’, ‘Steve’, ‘Bob’, ‘Bob’, ‘Steve’], ‘Col3′: np.random.random(5)}) What is the best way to return the unique values of ‘Col1’ and ‘Col2’? The desired output is ‘Bob’, ‘Joe’, ‘Bill’, ‘Mary’, ‘Steve’ Asked By: user2333196 || Source Answers: Non-pandas solution: using set(). …

Total answers: 10

Generate 'n' unique random numbers within a range

Generate 'n' unique random numbers within a range Question: I know how to generate a random number within a range in Python. random.randint(numLow, numHigh) And I know I can put this in a loop to generate n amount of these numbers for x in range (0, n): listOfNumbers.append(random.randint(numLow, numHigh)) However, I need to make sure …

Total answers: 4

Python sum of non duplicate int

Python sum of non duplicate int Question: I am given 3 int, a, b, c. I would like to find the sum of all three int provided that they are unique. If a, b, or c has the same values as any of the other values, then they don’t count towards the sum. Example 1: …

Total answers: 4

Find index where elements change value numpy

Find index where elements change value numpy Question: Suppose I have >>> v array([1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 3, 4, 3, 4, 3, 4, 5, 5, 5]) Is there an efficient numpy way to find each index where the value changes? For instance, I would want some result like, >>> …

Total answers: 4

Find unique rows in numpy.array

Find unique rows in numpy.array Question: I need to find unique rows in a numpy.array. For example: >>> a # I have array([[1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 1, 0]]) >>> new_a # …

Total answers: 20

Determining duplicate values in an array

Determining duplicate values in an array Question: Suppose I have an array a = np.array([1, 2, 1, 3, 3, 3, 0]) How can I (efficiently, Pythonically) find which elements of a are duplicates (i.e., non-unique values)? In this case the result would be array([1, 3, 3]) or possibly array([1, 3]) if efficient. I’ve come up …

Total answers: 11

How can I memoize a class instantiation in Python?

How can I memoize a class instantiation in Python? Question: Ok, here is the real world scenario: I’m writing an application, and I have a class that represents a certain type of files (in my case this is photographs but that detail is irrelevant to the problem). Each instance of the Photograph class should be …

Total answers: 4

Checking if all elements in a list are unique

Checking if all elements in a list are unique Question: What is the best way (best as in the conventional way) of checking whether all elements in a list are unique? My current approach using a Counter is: >>> x = [1, 1, 1, 2, 3, 4, 5, 6, 2] >>> counter = Counter(x) >>> …

Total answers: 18

Python: Uniqueness for list of lists

Uniqueness for list of lists Question: I am curious what would be an efficient way of uniquifying such data objects: testdata =[ [‘9034968’, ‘ETH’], [‘14160113’, ‘ETH’], [‘9034968’, ‘ETH’], [‘11111’, ‘NOT’], [‘9555269’, ‘NOT’], [‘15724032’, ‘ETH’], [‘15481740’, ‘ETH’], [‘15481757’, ‘ETH’], [‘15481724’, ‘ETH’], [‘10307528’, ‘ETH’], [‘15481757’, ‘ETH’], [‘15481724’, ‘ETH’], [‘15481740’, ‘ETH’], [‘15379365’, ‘ETH’], [‘11111’, ‘NOT’], [‘9555269’, ‘NOT’], [‘15379365’, …

Total answers: 7