0

I'm trying to compare the Gen-Eds needed to graduate at various colleges I'm considering. I want to specifically count anything that contains a key term(ex: ENG), but I can only get it to work for the whole string.

My current code looks like this

lis_UofM= ['LINGUS','MATH','NatSCI','LINGUS', 'ART', 'SocSCI']
lis_IIT=['MATH','NatSCI','ENG','MATH','PhysSCI','CompSCI']
lis_UCSD=['ENG','MATH','NatSCI','ENG','SocSCI','ART']

UofM_ENG=lis_UofM.count('ENG')+lis_UofM.count('LINGUS')
IIT_ENG=lis_IIT.count('ENG')
UCSD_ENG=lis_UCSD.count('ENG')

chart=pygal.StackedBar()
chart.add('UofM-Twin Cities',[UofM_MATH,UofM_ENG])
chart.add('IIT',[IIT_MATH, IIT_ENG])
chart.add('UCSD',[UCSD_MATH,UCSD_ENG])
chart.render()

I would like to count classes like 'ENG103' and 'ENG 202' or count all classes containing 'SCI' without making separate count functions for each of them. Is that possible?

Asocia
  • 5,935
  • 2
  • 21
  • 46

1 Answers1

1

I think you can use something like this

sum('ENG' in s for s in lis_IIT)

This other post has some additional info and techniques

Count occurrences of a substring in a list of strings

xanatos
  • 170
  • 1
  • 8