1

I am trying to generate two arrays, a and b, each containing ~1000 random numbers. The random number are between 1 and 5.

I want to then compare each element in a with the corresponding element of b such that if a[i] > b[i] a variable, counter, will be incremented by 1. This is considered a "success". Otherwise, if a[i] <= b[i] nothing happens (i.e counter += 0). This is considered a "fail".

However, a and b can be of variable length such that both len(a) == len(b) and len(a) != len(b) are possibilities.

In the case of the latter, if len(a) > len(b) I'd like all the "extra" elements of a to automatically be counted as "successes". If len(b) > len(a) things should proceed normally (i.e the "extra" elements of b are ignored).

For example:

If a = [1, 3, 4, 2] and b = [2, 4, 0]

Then counter = 2 because (1 < 2, 3 < 4, 4 > 0, and 2 is extra and is an automatic success`)

How would I go about doing this?

Here is some quick code, which returns the expected list index out of range... error:

import random

a = []
b = []
counter = 0

for i in range(1000):
    a += [random.randint(1, 5)]
for i in range(900):
    b += [random.randint(1, 5)]
for i in range(len(a)):
    if a[i] > b[i]:
        counter += 1

print counter

7 Answers 7

2

Just check to see if you've run off the end of b in your loop:

for i in range(len(a)):
    if (i >= len(b)) or a[i] > b[i]:
        counter += 1

You could also be a bit more clever, and do something like:

for i in range(min(len(a), len(b)):
    if a[i] > b[i]:
        counter += 1
if len(a) > len(b):
    counter += (len(a) - len(b))

(Using xrange here would be a minor improvement, as well.)

Sign up to request clarification or add additional context in comments.

1 Comment

This is a really good answer, short and easy to understand. Thanks.
2

One way of doing that would be:

len([_ for (i, j) in zip(a, b) if i > j]) + max(len(a) - len(b), 0)

3 Comments

Why not len([_ for (i, j)...; no use to actually generate the tuples, no?
It's less typing for sure :)
Great answer in any case.
1

Using izip_longest from itertools .This works for two lists which are not of equal length.

In [47]: 2>None
Out[47]: True

Since you need to check the greatest this works fine.

In [35]: from itertools import izip_longest

In [37]: lst=list(izip_longest(a,b))

In [38]: lst
Out[38]: [(1, 2), (3, 4), (4, 0), (2, None)]

In [42]: mylst=[(i>j) for (i,j) in lst]

In [44]: c=Counter(mylst)

In [45]: c[True]
Out[45]: 2

1 Comment

None may not be a good idea if the start integer is 0. Better it should be a negative number. so one should use fillvalue=-1
1

A simple oneliner using itertools.izip_longest

import random
import itertools
l1 = [random.randint(1,20) for x in range(20)]
l2 = [random.randint(1,20) for x in range(25)]
sum([1 if y < x else 0 for (x,y) in itertools.izip_longest(l1, l2, fillvalue=random.randint(1,20)])

Here in case the lengths of the lists are not identical, izip_longest would generate random numbers again to fill in remaining values (but after rereading your question - it seems more correct way is fillvalue=-1). Basically you can use any numerical value there (or anything that can be compared with an int, if the lists are of integer types).

1 Comment

I think one below is more elegant, but less explicit( It's a sum of True values - where True value is equal to 1).
0

I think you just need to add one more condition to check whether i's value is less than list B size

for i in range(len(a)):
    if i < len(b) and a[i] > b[i]:
        counter += 1

Comments

0

Use itertools.izip_longest https://docs.python.org/2/library/itertools.html#itertools.izip_longest, counting the number of situations where a is greater than b, and use a fill value for b that is less than the lowest possible a. This will result in the simplest possible control flow and most legible code. Shoot, you can ever force bools to ints with sum as here: sum(a > b for a, b in itertools.izip_longest(a_list, b_list, fillvalue=-1)).

1 Comment

Aah sorry didn't see this when I wrote the answer!
-1

You can iterate over the pairs like so:

for x, y in zip(a,b):
    # if x > y...

The rest can be solved just as you described, by checking the lengths:

counter += len(a) - len(b) if len(a) > len(b) else 0

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.