I have a list of \$n\$ words, and a corresponding \$m \space x \space n\$ frequency matrix (as a NumPy array). I would like to return a list/array of strings of length \$m\$ where the \$m\$th string is comprised of each word repeated according to the frequencies in the \$m\$th row of the frequency matrix. I have managed to achieve the desired result (help from here), but the code is not particularly easy to understand at a glance. Is there a cleaner and more efficient way to perform the following operation?
import numpy as np x = ['yugoslavia', 'zealand', 'zimbabwe', 'zip', 'zone'] y = np.array([[2,1,0,0,5], [0,0,1,3,0]]) z = np.apply_along_axis(lambda b: ' '.join([ item for sublist in [[x[i]]*b[i] for i in range(len(x))] for item in sublist]),1,y) >>> z array(['yugoslavia yugoslavia zealand zone zone zone zone zone', 'zimbabwe zip zip zip'], dtype='<U54')
I am looking for solutions compatible with Python 3.5.