The Numpythonic approach: (using numpy.dot
in order to get the dot product of two matrices)
In [1]: import numpy as np In [3]: np.dot([1,0,0,1,0,0], [[0,1],[1,1],[1,0],[1,0],[1,1],[0,1]]) Out[3]: array([1, 1])
The Pythonic approach:
The length of your second for
loop is len(v)
and you attempt to indexing v
based on that so you got index Error . As a more pythonic way you can use zip
function to get the columns of a list then use starmap
and mul
within a list comprehension:
In [13]: first,second=[1,0,0,1,0,0], [[0,1],[1,1],[1,0],[1,0],[1,1],[0,1]] In [14]: from itertools import starmap In [15]: from operator import mul In [16]: [sum(starmap(mul, zip(first, col))) for col in zip(*second)] Out[16]: [1, 1]
Related Posts:
- numpy matrix vector multiplication
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- Import Error: No module named numpy
- data type not understood
- Singular matrix issue with Numpy
- ‘End of statement expected’ in pycharm
- How to get element-wise matrix multiplication (Hadamard product) in numpy?
- Inverse of a matrix using numpy
- For loop and ‘numpy.float64’ object is not iterable error
- How to start from second index for for-loop
- How to fix ‘RuntimeWarning: divide by zero encountered in double_scalars’
- Root mean square of a function in python
- What are the differences between numpy arrays and matrices? Which one should I use?
- built-in range or numpy.arange: which is more efficient?
- Convert a 1D array to a 2D array in numpy
- How to install NumPy for Python 3.6
- Conditional indexing with Numpy ndarray
- SyntaxError: unexpected EOF while parsing
- How to use filter, map, and reduce in Python 3
- Use Gif Logo For Loading Screen In Kivy
- Praw & Discord.py: The bot keep sending the same meme. I want the bot to send different meme whenever it is asked
- How can I make a time delay in Python? [duplicate]
- Python – TypeError: ‘int’ object is not iterable
- How to define a two-dimensional array?
- How to open a file using the open with statement
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat)
- ImportError: DLL load failed: The specified module could not be found
- ImportError: DLL load failed: The specified module could not be found
- How to install pip with Python 3?
- What is the difference between rw+ and r+
- Python – TypeError: ‘int’ object is not iterable
- Python3 – ModuleNotFoundError: No module named ‘numpy’
- Python: ‘ModuleNotFoundError’ when trying to import module from imported package
- Relative imports – ModuleNotFoundError: No module named x
- ValueError: setting an array element with a sequence
- TypeError: a bytes-like object is required, not ‘str’ when writing to a file in Python3
- How to uninstall a package installed with pip install –user
- Relative imports in Python 3
- numpy max vs amax vs maximum
- What exactly does numpy.exp() do? [closed]
- Best way to convert string to bytes in Python 3?
- Best way to convert string to bytes in Python 3?
- What does the list() function do in Python?
- TypeError: not all arguments converted during string formatting python
- Difference between import numpy and import numpy as np
- PermissionError: [Errno 13] Permission denied
- What is setup.py?
- How is Anaconda related to Python?
- What is the purpose of meshgrid in Python / NumPy?
- why numpy.ndarray is object is not callable in my simple for python loop
- numpy division with RuntimeWarning: invalid value encountered in double_scalars
- FileNotFoundError: [WinError 2] The system cannot find the file specified:
- Numpy ValueError: setting an array element with a sequence. This message may appear without the existing of a sequence?
- numpy: Invalid value encountered in true_divide
- Invalid character in identifier
- python numpy ValueError: operands could not be broadcast together with shapes
- How to fix ‘Object arrays cannot be loaded when allow_pickle=False’ for imdb.load_data() function?
- How do I create an empty array/matrix in NumPy?
- Most efficient way to find mode in numpy array
- ValueError: Unknown label type: ‘continuous’
- Transpose/Unzip Function (inverse of zip)?
- TypeError: unhashable type: ‘numpy.ndarray’
- Using Python 3 in virtualenv
- How to fix IndexError: invalid index to scalar variable
- How to correct TypeError: Unicode-objects must be encoded before hashing?
- How to remove EOFError: EOF when reading a line?
- Could not install packages due to a “Environment error :[error 13]: permission denied : ‘usr/local/bin/f2py'”
- numpy division with RuntimeWarning: invalid value encountered in double_scalars
- How does numpy.newaxis work and when to use it?
- WinError 2 The system cannot find the file specified (Python)
- Converting list to numpy array
- How do I read CSV data into a record array in NumPy?
- hashlib.md5() TypeError: Unicode-objects must be encoded before hashing
- TypeError: cannot unpack non-iterable NoneType object
- Is there a ‘foreach’ function in Python 3?
- Python inline if statement
- How to install python3 version of package via pip on Ubuntu?
- Updating a dictionary in python
- Could not find a version that satisfies the requirement tensorflow
- How do you do natural logs (e.g. “ln()”) with numpy in Python?
- Convert bytes to a string
- How do I read CSV data into a record array in NumPy?
- Plotting a 2D heatmap with Matplotlib
- How to normalize a NumPy array to a unit vector?
- How to copy a dictionary and only edit the copy
- How to print like printf in Python3?
- Should I use np.absolute or np.abs?
- Could not find a version that satisfies the requirement tensorflow
- Local variable referenced before assignment?
- What does numpy.random.seed(0) do?
- AttributeError: ‘module’ object has no attribute ‘urlopen’
- ImportError: numpy.core.multiarray failed to import
- Factorial in numpy and scipy
- Python Error: AttributeError: __enter__ [duplicate]
- What does the c underscore expression `c_` do exactly?
- Convert pandas dataframe to NumPy array
- Can’t fix “zipimport.ZipImportError: can’t decompress data; zlib not available” when I type in “python3.6 get-pip.py”
- TypeError: ‘float’ object not iterable
- ImportError: Missing required dependencies [‘numpy’]