0

I have more than 15200 links to websites which I need to shuffle.

If I had a small number of websites, I can enter them manually into python and use the following code to shuffle them:

from random import shuffle
x = ['website_1', 'website_2', 'website_3', 'website_4', 'website_5']
shuffle(x)

But entering more than 15200 links manually into a list is extremely time consuming because I would have to put quotation marks on them individually.

At the moment, I have the links in the following form:

website_1
website_2
website_3
.
.
.
website_15270

Is there a way I can enter the the website in the format shown above and have them shuffled and returned in the same format?

3 Answers3

1

Since the links are stored in a file:

from random import shuffle
with open(file) as f:
    links = [line.rstrip('\n') for line in f]
shuffle(links)
nneonneo
  • 171,345
  • 36
  • 312
  • 383
1

You read your file and remove the newlines from each link..

import random

links = [link.rstrip("\n") for link in open("yourFileName").readlines()]
random.shuffle(links)
Oni1
  • 1,445
  • 2
  • 19
  • 39
0

I think you can:

  1. Create an array with values from 1 to #ofSites
  2. Shuffle this array (I think is faster)
  3. Create a list of site using this How do I read a file line-by-line into a list?
  4. Use the shuffled array as index for retrieve the sites from the list.
Community
  • 1
  • 1