[re.finditer] Getting all occurences in one go?

Paul Rubin http
Mon Apr 23 00:33:37 CEST 2007


Gilles Ganault <nospam at nospam.com> writes:
> for match in matches:
> 	if mytable[item]=="":
> 		mytable[item]= match.group(1)
> 	else:
> 		mytable[item]= mytable[item] + "," + match.group(1) #
> ----------- END
> =======================
> 
> Can the lines between BEGIN/END be simplified so I can copy all the
> items into the mytable[] dictionary in one go, instead of getting each
> one in a row, and append them with a comma, eg.

Yes, look at the string.join method and generator expressions.  You'd
say:

  mytable[item] = ','.join(m.group(1) for m in matches)



More information about the Python-list mailing list