Get unique values in List of Lists
array = [['a','b'], ['a', 'b','c'], ['a']]
result = {x for l in array for x in l}
How to get only distinct values from a list?
This is what you needed with set()
:
>>> lst1 = ['A','A','A','B','C','C','D','D','D','B','B']
>>> list(set(lst1))
['A', 'B', 'D', 'C']
Another solution OrderedDict
to keep the order of keys during insertion.
>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys(lst1))
['A', 'B', 'C', 'D']
In case you have liberty to use pandas then try below ones..
>>> import pandas as pd
>>> drop_dups = pd.Series(lst1).drop_duplicates().tolist()
>>> drop_dups
['A', 'B', 'C', 'D']
In case you are looking for common values between two files:
$ cat getcomn_vals.py
#!/python/v3.6.1/bin/python3
def print_common_members(a, b):
"""
Given two sets, print the intersection, or "No common elements".
Remove the List construct and directly adding the elements to the set().
Hence assigned the dataset1 & dataset2 directly to set()
"""
print('\n'.join(s.strip('\n') for s in a & b) or "No common element")
with open('file1.txt') as file1, open('file2.txt') as file2:
dataset1 = set(file1)
dataset2 = set(file2)
print_common_members(dataset1, dataset2)
Finding Unique Items in a List
Here is a simple solution using nth0/4
(or select/3
as pointed out by @false):
unique(X, L) :-
nth0(_, L, X, R),
\+ member(X, R).
nth0/4
4th argument R
is the list L
with the element X
removed. We simply check then that X
is not in R
.
Better version
unique(X, L) :-
nth0(_, L, X, R),
maplist(dif(X), R).
This fixes the problem pointed out by @false, though since you are a beginner I doubt this is of much interest to you.
This has the advantage of working in situations like this one:
?- unique(b, [X, Y, a]).
X = b,
dif(Y, b) ;
Y = b,
dif(X, b) ;
false.
How to get a unique value from a list in python
Count the items, keep only those which have a count of 1:
>>> data = ['a','a','b','a']
>>> from collections import Counter
>>> [k for k,v in Counter(data).items() if v == 1]
['b']
How to get unique values from a python list of objects
Use a set
comprehension. Sets are unordered collections of unique elements, meaning that any duplicates will be removed.
cars = [...] # A list of Car objects.
models = {car.model for car in cars}
This will iterate over your list cars
and add the each car.model
value at most once, meaning it will be a unique collection.
How to make lists contain only distinct element in Python?
The simplest is to convert to a set then back to a list:
my_list = list(set(my_list))
One disadvantage with this is that it won't preserve the order. You may also want to consider if a set would be a better data structure to use in the first place, instead of a list.
Getting unique items from a list
Use a HashSet<T>
. For example:
var items = "A B A D A C".Split(' ');
var unique_items = new HashSet<string>(items);
foreach (string s in unique_items)
Console.WriteLine(s);
prints
A
B
D
C
Get all unique values in a JavaScript array (remove duplicates)
With JavaScript 1.6 / ECMAScript 5 you can use the native filter
method of an Array in the following way to get an array with unique values:
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// usage example:
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter(onlyUnique);
console.log(unique); // ['a', 1, 2, '1']
Related Topics
Mirroring Console Output to a File
Fast and Compact Object Serialization in .Net
How to Add a Delay for a 2 or 3 Seconds
How to Specify Proxy Credentials in Your Web.Config
Use Xml Includes or Config References in App.Config to Include Other Config Files' Settings
How to Determine If a File Is Binary or Text in C#
Mobile Device Detection in ASP.NET
How to Remove All White Space from the Beginning or End of a String
Why Does Resharper Want to Use 'Var' for Everything
Posting JSON Data to ASP.NET MVC
How to Programmatically Modify Wcf App.Config Endpoint Address Setting
Failed to Serialize the Response in Web API
Fastest Way to Convert Image to Byte Array
Get Property Name and Type Using Lambda Expression
How to Have an Enum Bound Combobox with Custom String Formatting for Enum Values