0

I have a list which I want to convert into matrix. But when using numpy I am not able to remove inverted commas.

List -

[['b', 'd', 'a', 'c'], ['b1', 'd1', 'a1', 'c1'], ['b2', 'd2', 'a2', 'c2']]

Using Numpy -

[['b' 'd' 'a' 'c']
 ['b1' 'd1' 'a1' 'c1']
 ['b2' 'd2' 'a2' 'c2']]

What I need -

b  d  a  c
b1 d1 a1 c1
b2 d2 a2 c2

Thanks

3
  • The "Inverted commas" you say are the single quotes - " ' " ? Commented Nov 2, 2016 at 12:52
  • What do you mean by 'I need:'? Do you need to print? Commented Nov 2, 2016 at 12:53
  • Is this just a print display issue, or do you need to do something in numpy with the matrix of strings? Commented Nov 2, 2016 at 16:56

3 Answers 3

2

No need to numpy you can make it pure python.

a = [['b', 'd', 'a', 'c'], ['b1', 'd1', 'a1', 'c1'], ['b2', 'd2', 'a2', 'c2']]
for b in a:
    print ' '.join(b)
Sign up to request clarification or add additional context in comments.

Comments

2

Quotes with the text means that you have the object of str type. You need not worry about them because when you do print, these will be removed. For example:

>>> my_list = [['b', 'd', 'a', 'c'], ['b1', 'd1', 'a1', 'c1'], ['b2', 'd2', 'a2', 'c2']]
>>> my_list[0][0]  # without print, shows content with quotes
'b'
>>> print my_list[0][0]  # with print, content without quotes
b
>>>

In order to print the content in the format you mentioned, you may do:

>>> for sub_list in my_list:
...     print ' '.join(sub_list)  # join to make single of object in sub_list
...
b d a c
b1 d1 a1 c1
b2 d2 a2 c2

where my_list is the list mentioned in the question.

Comments

2

If all you want is to print it such that cells are space separated and rows are line-break separated :

print ('\n'.join([' '.join(i) for i in lst]))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.