Programming Tip -How to convert python tuple into a two-dimensional table?

in #utopian-io7 years ago

Programming Tip -How to convert python tuple into a two-dimensional table?

EBOOK_PYTHON_no-name.png


Today i am going to show Programming Tip - How to convert python tuple into a two-dimensional table with simple code. I hope you will understand this.
If you have any query you can ask.
i hope you will like it.


you should use the reshape method to reshape the tuple to a multidimensional array. For example,

import numpy
data = numpy.array(range(1,10))
data.reshape([3,3])
print(data)
This will give the output:

array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
If you prefer to do it in pure python, you can use a list comprehension:

data = tuple(range(1, 10))
table = tuple(data[n:n+3] for n in xrange(0,len(data),3))
print(table)
This will give the output:

((1, 2, 3), (4, 5, 6), (7, 8, 9))



========================================================= Follow me at : https://steemit.com/@ahmadhassan


Thanks for reading and always welcome your suggestions :) =========================================================

Sort:  

Thanks for sharing this post.

This post has received a 0.94 % upvote from @booster thanks to: @ahmadhassan.