Hi,
I have two arrays that are identical and contain 1s and zeros. Only the
ones are valid and I need to know where both arrays have ones in the
same position. I thought logical_and would work but this example proves
otherwise: a = [0,1,2,5,6,6] b = [5,4,1,6,4,6] Numeric.logical _and(a==6,b==6)
0 Numeric.where(a ==b,1,0)
0 Numeric.where(a ==6 and b==6,1,0)
0
The where() statement is also worhtless here. Does anyone have any
suggestion on how to do this?
Thanks in advance,
Sheldon 11 10389
Sheldon wrote: Hi,
I have two arrays that are identical and contain 1s and zeros. Only the
Obviously they aren't identical. They may be of same size.
ones are valid and I need to know where both arrays have ones in the same position. I thought logical_and would work but this example proves otherwise: a = [0,1,2,5,6,6] b = [5,4,1,6,4,6] Numeric.logical _and(a==6,b==6) 0 Numeric.where(a ==b,1,0) 0 Numeric.where(a ==6 and b==6,1,0)
0
The where() statement is also worhtless here. Does anyone have any suggestion on how to do this?
print [i for i, _ in enumerate((None for v in zip(a, b) where v == (1,1)))]
should give you the list of indices.
Diez
You are comparing a normal python list to a constant, which are
obviously unequal. Try converting your lists to arrays first
(untested):
import numeric/numpy as N
a =N.array([0,1,2,5,6,6])
b = N.array([5,4,1,6,4,6])
print a==6 and b==6
print N.where(a==6 and b==6)
hth,
Bas
Sheldon wrote: Hi,
I have two arrays that are identical and contain 1s and zeros. Only the ones are valid and I need to know where both arrays have ones in the same position. I thought logical_and would work but this example proves otherwise: a = [0,1,2,5,6,6] b = [5,4,1,6,4,6] Numeric.logical _and(a==6,b==6) 0 Numeric.where(a ==b,1,0) 0 Numeric.where(a ==6 and b==6,1,0)
0
The where() statement is also worhtless here. Does anyone have any suggestion on how to do this?
Thanks in advance, Sheldon
Diez B. Roggisch wrote: print [i for i, _ in enumerate((None for v in zip(a, b) where v == (1,1)))]
should give you the list of indices.
I musunderstood your question. Use
print [i for i, _ in enumerate((None for x, y in zip(a, b) where x == y))]
instead.
Diez
Bas wrote: You are comparing a normal python list to a constant, which are obviously unequal. Try converting your lists to arrays first (untested):
import numeric/numpy as N a =N.array([0,1,2,5,6,6]) b = N.array([5,4,1,6,4,6]) print a==6 and b==6 print N.where(a==6 and b==6)
Careful there. The "and" keyword cannot be overloaded and so neither Numeric nor
numpy does. Either N.logical_and() should be used or (since the results of a==6
and b==6 are known to be boolean arrays) the & operator works fine as well.
In [9]: import numpy as np
In [10]: a = np.array([0,1,2,5,6,6])
In [11]: b = np.array([5,4,1,6,4,6])
In [12]: (a==6) & (b==6)
Out[12]: array([False, False, False, False, False, True], dtype=bool)
In [13]: np.where((a==6) & (b==6))
Out[13]: (array([5]),)
The OP may also find that numpy questions are best handled on numpydiscussion
rather than comp.lang.pytho n . https://lists.sourceforge.net/lists/...mpydiscussion

Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
 Umberto Eco
Diez B. Roggisch skrev: Diez B. Roggisch wrote:
print [i for i, _ in enumerate((None for v in zip(a, b) where v == (1,1)))]
should give you the list of indices.
I musunderstood your question. Use
print [i for i, _ in enumerate((None for x, y in zip(a, b) where x == y))]
instead.
Diez
Hi Diez,
I wish I say that I understood what you wrote here but I can't.
Do you mind explaining a little more?
/sheldon
>> print [i for i, _ in enumerate((None for x, y in zip(a, b) where x == y))]
instead.
Diez
Hi Diez,
I wish I say that I understood what you wrote here but I can't. Do you mind explaining a little more?
I actually made a typo, instead of "where" in the above use "if". and the
whole thing won't compute what you are after. This will:
[i for i, equals in enumerate((x == y for x, y in zip(a, b))) if equals]
Sorry for that.
Its a nested listcomprehension. Actually, the outer thingy is a
listcomprehension while the inner is a generator expression. There is of
course a difference, but for now this doesn't matter too much.
a list comprehension of form
[<expression> for <variable> in <iterable> if <condition>]
is a more compact form for
result = []
for <variable> in <iterable>:
if <condition>:
result.append(< expression>)
A generator expression works the same with () instead of [].
Now let's decompose the above statment:
(x == y for x, y in zip(a, b) ))
<variable> = x, y
<iterable> = zip(a, b)
<expression> = x == y
So: the iterable is returned by the function zip. That is a builtin which
will take 2 or more lists and return a list of tuples, where the first
element is from the first list, then the second and so on. So your example
lists become:
a = [0,1,2,5,6,6]
b = [5,4,1,6,4,6]
zip(a, b) = [(0,5), (1,4), (2,1), (5,6), (6,4), (6,6)]
So iterating over this yields the pairs of (0,5) and so forth.
Next thing is that with
x, y = p
python unpacks a tuple and refers to its contents by name x for the first
one and y for the second. So overall, we loop in sync over both lists, and
getting x and y to point to the corresponding elements.
Now the expression x == y will result True if x == y  False otherwise.
So the result of the inner listcomp/genexp looks like this:
res = [False, False, False, False, False, True]
As you can see: for each pair x,y in the original lists we come up with the
result of comparing them.
Now the outer listcomp using "res" instead of the genexps for clarity reads
like this:
[i for i, equals in enumerate(res) if equals]
<variable> = i, equals
<iterable> = enumerate(res)
<expression> = i
<condition> = equals
enumerate is another builtin that takes an iterable and returns a tuple of
(pos, element)
for each element in the iterable.
So for our list, it will return:
[(0, False), (1, False), (2, False), (3, False), (4, False), (5, True)]
These tupkes values are assigened to i and equals for each element of the
above list. The condtion
equals
will then guarantee that only those expressions are evaluated where equals
is True  the last pair, so to speak. The expression then only stores the
index i. Which will give us the desired result.
Diez
Le Mardi 20 Juin 2006 12:09, Diez B. Roggisch a écrit*: [i for i, equals in enumerate((x == y for x, y in zip(a, b))) if equals]
No needs to nest comprehensions, should be :
[ i for i, v in enumerate(zip(a , b)) if v[0] == v[1] ]

_____________
Maric Michaud
_____________
Aristote  www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
Maric Michaud wrote: Le Mardi 20 Juin 2006 12:09, Diez B. Roggisch a Ã©critÂ*: [i for i, equals in enumerate((x == y for x, y in zip(a, b))) if equals]
No needs to nest comprehensions, should be :
[ i for i, v in enumerate(zip(a , b)) if v[0] == v[1] ]
You're right, that design stemmed from my first broken version.
Diez
Diez B. Roggisch wrote: Maric Michaud wrote:
Le Mardi 20 Juin 2006 12:09, Diez B. Roggisch a Ã©crit : [i for i, equals in enumerate((x == y for x, y in zip(a, b))) if equals] No needs to nest comprehensions, should be : [ i for i, v in enumerate(zip(a , b)) if v[0] == v[1] ]
You're right, that design stemmed from my first broken version.
Or even deconstruct to avoid the (very mildly confusing) v[0], v[1]:
[i for i, (left, right) in enumerate(zip(a , b)) if left == right]

Scott David Daniels sc***********@a cm.org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics 
by: Peter 
last post by:
Hi
how can I compare two byte arrays in VB.NET
Thank
Peter

by: Iain 
last post by:
Hi
Hopefully I am missing something really simple with this question, but
here goes.
I have two Bitarrays that I would like to compare. At the moment, I am
XORing one with the other and checking to see if the result has any 1s
in it (if so, the arrays are different). This seems to be faster than
comparing each bit of the two original arrays one at a time. But I
still have to iterate over each element of the array, and I'd like to

by: Elijah Bailey 
last post by:
I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).
What is the fastest way to do this? k <= 10 usually for my application.
I tried bcmp / a loop for comparison, but it seems they are very slow
compared to comparing longs...Any ideas?
I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.

by: agent349 
last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1
== array2)). However, I've been trying to compare two arrays using
pointers with no success. Basically, I want to take three sets of
character strings from the user. Then I want to run through each
element and compare the two strings. If they match I print they
match... I'm having a bit of trouble with the actual loop through each
array using the pointers and comparing...

by: Donald Grove 
last post by:
If I have two arrays, what is a good paradigm for comparing what is in
them, to determine what elements they share, or don't share?
Specifically, each array could potentially contain the integers 1 to
9, but probably not all 9 integers.
MyArray1 = 1,2,3,5,6,7,8
MyArray2 = 1,4,6,7
I would need the answers to be arrays too:
 
by: eoghan.kenny 
last post by:
Hi,
I need to compare two timestamp columns in sql server and see which one
is greater. (i can tell if they are not equal buts not enough for this
requirement).
A timestamp value is unique in a database and always increments. A
nonnull timestamp is type Binary(8) in sql server and I pass it to a
byte array in C#. To compare the two byte arrays I use
BitConverter.ToUInt64, as this converts an 8 byte array to a 64 bit

by: Ole Nielsby 
last post by:
How does the GetHashCode() of an array object behave?
Does it combine the GetHashCode() of its elements, or does
it create a sync block for the object?
I want to use readonly arrays as dictionary keys, based on
their content, not their identity. Is this feasible using the
arrays directly, or do I need to wrap them in a struct that
handles GetHashCode and Equal? If so, is such a wrapper
present in the standard class library?

by: psmahesh 
last post by:
Hi folks,
I am comparing two arrays and removing matches from the second array
from the first array.
Can someone take a look at this code below and mention if this is okay
and perhaps if there is a better way to achieve it
for(i=0;i<arrayA.length;i++){
for(j=0;j<arrayB.length;j++){
if(arrayA==arrayB)

by: filippo nanni 
last post by:
Hello everybody, my question is this:
I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code:
//BEGIN CODE
var myGroups = ,,];
var myProducts = ,,,];
var newGroups = new Array();//Resulting Array
function main() {
for(i=0; i<myGroups.length; i++){

by: Hystou 
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...

by: Oralloy 
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bitfields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++12 std=c++20 Wnarrowing bit_field.cpp
Here is the code in...
 
by: jinu1996 
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...

by: isladogs 
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...

by: conductexam 
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...

by: TSSRALBI 
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LANtoLAN VPNs.
The last exercise I practiced was to create a LANtoLAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

by: adsilva 
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

by: muto222 
last post by:
How can i add a mobile payment intergratation into php mysql website.
 
by: bsmnconsultancy 
last post by:
In today's digital era, a welldesigned website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
 