python - Counting groups in list -
i'm trying count number of distinct groups of text labels in list blabla
in python. e.g
for in blabla['condition'].unique(): print
the output of is:
no1 med1 48h no1 med1 72h no1 med1 96h no1 med1 120h no2 med1 48h no2 med1 72h no2 med1 96h no2 med1 120h no1 med2 48h no1 med2 72h no1 med2 96h no1 med2 120h
i want count amount of times repeat (i.e. 48h
, 72h
, 96h
, 120h
) occurs in particular list blabla
. in case 3 times.
you try:
>>> ids = set(' '.join(item.split()[:2]) ... item in blabla['condition'].unique())) # set(['no2 med1', 'no1 med1', 'no1 med2']) >>> len(ids) 3
that is, treat first 2 words of each item identifier, , count number of unique identifiers.
(it necessary convert list item.split()[:2]
string using join
, because lists cannot used set elements, required here make them unique.)
Comments
Post a Comment