comparing a string 1-d numpy array elementwise

2024/7/2 15:14:43

I have two 1-d arrays (a and b) containing strings, which I want to compare element wise to get output c like shown below. I tried converting it to set and comparing, however that does not give the correct solution. Also logical_xor does not work for string. I can write a loop to do this but then it defeats the purpose of using arrays, What can be the best way to do this without a loop?

  >>  aarray(['S', 'S', 'D', 'S', 'N', 'S', 'A', 'S', 'M'], dtype='|S1')>>  barray(['T', 'I', 'D', 'N', 'G', 'B', 'A', 'J', 'M'], dtype='|S1')>> c array([False, False, True, False, False, False, True, False, True], dtype=bool)
Answer

Just use the ndarray's __eq__ method, i.e. ==

>>> a = array(['S', 'S', 'D', 'S', 'N', 'S', 'A', 'S', 'M'], dtype='|S1')
>>> b = array(['T', 'I', 'D', 'N', 'G', 'B', 'A', 'J', 'M'], dtype='|S1')
>>> a == b
array([False, False,  True, False, False, False,  True, False,  True], dtype=bool)
https://en.xdnf.cn/q/73366.html

Related Q&A

Can Atom work with Python virtualenvwrapper

I want to start a Flask app. I installed virtualenvwrapper to manage the packages but I cant let Atom know that the current project should use the virtualenvs python binary.from flask import Flask, ren…

Concatenation of tuples

Normal text:Im having some problems with coding on python 3.2.1. Actually Im taking online lectures that are on python 2.5.Here is the code:x = 100 divisors = () for i in range(1,x):if x%i == 0:divisor…

How do I create a new data table in Orange?

I am using Orange (in Python) for some data mining tasks. More specifically, for clustering. Although I have gone through the tutorial and read most of the documentation, I still have a problem. All th…

How to turn off MySQL query cache while using SQLAlchemy?

I am working with a fairly large MySQL database via the SQLAlchemy library, and Id love to turn off MySQLs query caching to debug performance issues on a per-session basis. Its difficult to debug slow …

Storing an inverted index

I am working on a project on Info Retrieval. I have made a Full Inverted Index using Hadoop/Python. Hadoop outputs the index as (word,documentlist) pairs which are written on the file. For a quick acc…

How to determine whether java is installed on a system through python?

Using Python, I want to know whether Java is installed.

How should I save the model of PyTorch if I want it loadable by OpenCV dnn module

I train a simple classification model by PyTorch and load it by opencv3.3, but it throw exception and sayOpenCV Error: The function/feature is not implemented (Unsupported Lua type) in readObject, file…

Apache Spark ALS - how to perform Live Recommendations / fold-in anonym user

I am using Apache Spark (Pyspark API for Python) ALS MLLIB to develop a service that performs live recommendations for anonym users (users not in the training set) in my site. In my usecase I train th…

python JIRA connection with proxy

Im trying to connect via python-jira using a proxy:server = {"server": "https://ip:port/jira",proxies: {"http": "http://ip:port", "https": "http:/…

How can I iterate over only the first variable of a tuple

In python, when you have a list of tuples, you can iterate over them. For example when you have 3d points then:for x,y,z in points:pass# do something with x y or zWhat if you only want to use the first…