Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
repeating for loop
#1
Hello guys,
Is there any command for repeating for cycle? I need to have done list of numbers that do not repeat...
z=[]
for i in range(15):
    number=random.randint(0,99)
    if number not in z:
        z.append(number)
...but in my code when two numbers are same I get only 14 numbers in list and throws error "list index out of range"

Thank you!
Reply
#2
one way - don't use for loop. use while loop instead

z=[]
while len(z) < 15:
    number=random.randint(0,99)
    if number not in z:
        z.append(number)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
If you want the numbers from 0 to n in random order without repeats, use shuffle:

numbers = list(range(n + 1))
random.shuffle(numbers)
I don't see how you could get an IndexError, since there are no list indexes in your code. If the above does not solve your problem, please post the full text of the error.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
First, we start with a generator that produces an infinite stream of random numbers:
>>> import random
>>> def random_numbers(min_val, max_val):
...     while True:
...         yield random.randint(min_val, max_val)
...
Then, we write a function that takes a stream, and filters out duplicate values:
>>> def unique_values(sequence):
...     seen_before = set()
...     for item in sequence:
...         if item not in seen_before:
...             seen_before.add(item)
...             yield item
...
Then, we write another function which takes a stream of some kind, and takes only the first few items off of it:
>>> def take(num, sequence):
...     sequence = iter(sequence)
...     for _ in range(num):
...         yield next(sequence)
...
Putting all those together, we can generate any number of uniquely random numbers:
>>> list(take(5, unique_values(random_numbers(0, 7))))
[6, 5, 1, 0, 7]
Reply
#5
@ichabood - I guess they have extra code. Also they want list with len 15.
@Kaldesyvon: to extend what ichabood suggests:
>>> import random
>>> nums = list(range(100))
>>> random.shuffle(nums)
>>> z = nums[:15]
>>> z
[15, 34, 29, 20, 24, 31, 86, 62, 41, 38, 11, 72, 60, 85, 13]
>>> 
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Dec-06-2018, 07:56 PM)buran Wrote: I guess they have extra code. Also they want list with len 15.

I was not clear to me what they wanted. I saw the 15 loop, but I wasn't sure if that was because they wanted 15 numbers or not.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 852 Dec-12-2023, 09:09 AM
Last Post: buran
  repeating a user_input astral_travel 17 2,621 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  if else repeating Frankduc 12 2,828 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  factorial, repeating Aldiyar 4 2,938 Sep-01-2020, 05:22 PM
Last Post: DPaul
  number repeating twice in loop JonnyEnglish 3 3,438 Nov-24-2019, 09:23 AM
Last Post: ThomasL
  Repeating equations Tbot100 2 3,377 May-29-2019, 02:38 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020