python - Count the number of lists that starts with the same item in a list -
so have list like:
result = [["1", "1", "a", 8.2],["1", "2", "c", 6.2],["2", "1", "a", 8.2]]
i want function returns count of number of lists starts (index[0])
variable "n"
. so, if n = '1'
in case 2, if n = '2'
i 1
.
edit: i've tried few things this, can't work.
def count(list,n): result = [] value = 0 in list: if str(i[0]) == n: value = value + 1 sum.append[value] return len(sum) print count(result,1)
result = [["1", "1", "a", 8.2],["1", "2", "c", 6.2],["2", "1", "a", 8.2]] lst2 = [item[0] item in result] lst2.count("1")
result = [["1", "1", "a", 8.2],["1", "2", "c", 6.2],["2", "1", "a", 8.2]] def count(mylist,n): lst2 = [item[0] item in mylist] return lst2.count(str(n)) print count(result,1) #prints 2 print count(result,2) #prints 1
Comments
Post a Comment