Python Forum

Full Version: What does this code fragment do?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Given: brain_live_features is an array of ints ...

    live_features = brain_live_features + 2
    live_features = list(live_features)
    live_features = [0, 1] + live_features
    live_features = np.array(live_features).astype('int32')
My question is, what does this code do? When I type the first line into interactive python it gives me an error,
Error:
can only concatenate list (not "int") to list
My best guess is that we are adding the value 2 to each element of brain_live_features to adjust these indices before we prepend two new elements [0, 1].
Seems to me that by "an array of ints" they are referring to a numpy array...
Yes, I think you're right. If I say
blf = np.array( ...
then it seems to work. That seems to use the next line,
lf = list( lf )
to convert the np.array into a list so it can be concat'd.

Thank you.