0

I have a list that content 5000 numbers. and it looks like this

y = [1,2,3,4,5,6]

I want to multiple this matrix with another matrix. Therefore, I want to convert it to a column.

y' =[[1],[2],[3],[4],[5],[6]]

I try to use zip(*y). But an error occurs "zip argument #1 must support iteration" thank you so much for helping.

1
  • If you're doing mathematics forget about lists, use numpy. Trust me, I do a lot of that ;) Commented Dec 29, 2013 at 22:02

4 Answers 4

3

You can use list comprehensions:

>>> y = [1,2,3,4,5,6]
>>> [[i] for i in y]
[[1], [2], [3], [4], [5], [6]]
Sign up to request clarification or add additional context in comments.

Comments

2

Consider switching to numpy for efficient vector and matrix operations.

In [1]: import numpy as np

In [2]: %paste
y = [1,2,3,4,5,6]

## -- End pasted text --

In [3]: a = np.array(y)

In [4]: a
Out[4]: array([1, 2, 3, 4, 5, 6])

In [5]: np.dot(a, a)
Out[5]: 91

With a matrix:

In [7]: M = np.matrix(np.arange(0, 15, 0.5).reshape((6, 5)))

In [8]: M
Out[8]:
matrix([[  0. ,   0.5,   1. ,   1.5,   2. ],
        [  2.5,   3. ,   3.5,   4. ,   4.5],
        [  5. ,   5.5,   6. ,   6.5,   7. ],
        [  7.5,   8. ,   8.5,   9. ,   9.5],
        [ 10. ,  10.5,  11. ,  11.5,  12. ],
        [ 12.5,  13. ,  13.5,  14. ,  14.5]])

In [9]: a * M
Out[9]: matrix([[ 175. ,  185.5,  196. ,  206.5,  217. ]])

or simply:

In [10]: M = np.arange(0, 15, 0.5).reshape((6, 5))

In [11]: np.dot(a, M)
Out[11]: array([ 175. ,  185.5,  196. ,  206.5,  217. ])

Comments

0
y' = [[i] for i in y]

would do that

Comments

0

I think what you're trying would work with a 2D array:

 >>> z = [[1,2,3,4,5,6]]
 >>> zip(*z)
 [(1,), (2,), (3,), (4,), (5,), (6,)]

Although, now you have a list of tuples, instead of a list of lists.

As I understand it, zip exists to take sets of iterables (irritables(?)) and return a list of tuples, where the nth tuple contains the nth items from each iterable.

When you took a 1D list and applied '*' to it, you unpacked the list before passing it to zip. So,

  y = [1,2,3,4,5,6]
  zip(*y)

Was the same as

  y = [1,2,3,4,5,6]
  zip(1,2,3,4,5,6)

which, as the error stated, was asking zip to work with arguments that don't support iteration.

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.