I have got list of lists, have to count number of unique values in one of the columns from that list. I have got only to the point to extract that one column from the list by using the following
JavaScript
x
5
1
j = 1
2
for i in range(len(table)):
3
row = table[i]
4
print(row[j])
5
how can I now count unique data in that column?
Advertisement
Answer
Use set
to find unique elements
JavaScript
1
3
1
column_index = 1 # Set which column you need
2
len(set(row[column_index] for row in table))
3
OR
JavaScript
1
4
1
columns = list(zip(*table))
2
len(set(columns[column_index])) # Number of unique items
3
sum(columns[column_index]) # Sum of column
4