Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a list of tuples like:
a = [(1,2,3), (4,5)] // np.shape = (2,)
And i want to covert it into an array like structure, but of fixed shape, ie
a = [(1,2,3), (4,5,0)] // np.shape = (2,3)
In [69]: maxlen=max(len(i) for i in a) #get the max length of all tuples In [70]: [i+(0,)*(maxlen-len(i)) for i in a] #fill each tuple with extra zeros Out[70]: [(1, 2, 3), (4, 5, 0)]
Add a comment
Functional way to do this would be
a = [(1,2,3), (4,5), (6, 7, 8, 9)] from itertools import izip_longest print zip(*izip_longest(*a, fillvalue = 0)) # [(1, 2, 3, 0), (4, 5, 0, 0), (6, 7, 8, 9)]
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.