1

I need to write Python code that compares/relays through 2 lists of integers and then prints the smaller number of each element. I currently have this:

    x = range(0, 2**32)
    y = range(2**32, 2**64)
    def minimum(a, b):
       """print the minimum for each element of 2 lists of integers"""
       for i in (a,b):
          print(min(a, b))

I am getting an error which reads :

"'<' not supported between instances of 'range' and 'range'."

Is there another way to solve my problem without the range function?

5
  • Simply min(i). Commented Jun 15, 2017 at 14:34
  • 2
    @ForceBru My crystal ball suggest for i, j in zip(a, b): print(min(i, j)). Commented Jun 15, 2017 at 14:34
  • Side note: You don't want to iterate over ranges with 2**64 as upper boundary. It tends to take a few thousand years. Commented Jun 15, 2017 at 14:37
  • @SvenMarnach, range(2**32) will be exhausted quite a bit before the second range, so as long as you don't use itertools.zip_longest you'll be fine Commented Jun 15, 2017 at 14:56
  • @SvenMarnach That worked perfectly-I cannot thank you enough! Commented Jun 15, 2017 at 15:28

1 Answer 1

1

You can try this:

x = range(0, 2**32)
y = range(2**32, 2**64)

new_list = map(min, zip(x, y))

print(list(new_list))

new_list now stores the minimum values for every index of both lists.

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

9 Comments

In Python 3, new_list isn't actually a list.
I understand. I merely picked the name "new_list" on the fly.
@Ajax1234 thank you! Unfortunately I am now getting a memory error. I have never had this before-do you have any advice on how to fix this?
You are attempting to create a generator which, despite its efficiency, is still trying to crunch a list that for x, will be of length 4e9, and the last, which is approximately 1.8e19, which is greater than the estimated amount of grains of sand in the universe!
@H.Minear You are creating a list with 4 billion integers. This would require hundreds of gigabytes of memory (at least on a 64-bit machine).
|

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.