dimanche 31 décembre 2017

How can I count numbers of value occurrences in list with list comprehension?

model = [random.randint(0,1) if model.count(0) < 20 else 1 for _ in range(40)]

Of course model.count(0) is wrong. Here we go for right code, but w/o list comprehension.

model = list()
for i in range(40):
   if model.count(0) < 20:
       model.append(random.randint(0,1))
   else:
       model.append(1)

I'm just love list comprehensions it makes me easier to work with NLP and massives. So it would be cool to find new features.




Aucun commentaire:

Enregistrer un commentaire