Home > database >  How to get Array one by one Randomly in array order in Python
How to get Array one by one Randomly in array order in Python

Time:10-22

I have automated to send the numbers from the numbers array one by one to text box as shown below, while the numbers are get it from the array and at the same time how to get the mail id randomly one by one from the email array?

numbers = [2589001,2589002,2589003,2589004,2589005,2589006,2589007,2589008,2589009,2589010] 
email = ["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"] 

c = 0
def vs () :

    for nm in numbers :
        
        #Number Input
        WebDriverWait(browser, 2500).until(EC.presence_of_element_located((By.NAME, "Number"))).send_keys(nw)
        time.sleep(1)
        
        #I need code here to get email id one by one randomly from array


w = 0     
while w < 50 :
    vs ()
    w  = 1

CodePudding user response:

If you want a random item from list and don't care if you get the same one later, use random.choice(list)

If you want all items from a list in random order only once, use random.shuffle(list)

example:

import random

numbers = [2589001,2589002,2589003,2589004,2589005,2589006,2589007,2589008,2589009,2589010] 
emails = ["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"] 

def vs(number, email):
    # each number only once from list
    WebDriverWait(browser, 2500).until(EC.presence_of_element_located((By.NAME, "Number"))).send_keys(number)
    time.sleep(1)

    # random email from list
    WebDriverWait(browser, 2500).until(EC.presence_of_element_located((By.NAME, "Email"))).send_keys(email)
    time.sleep(1)

for number in random.shuffle(numbers):
    vs(number, random.choice(emails))

If number and email are a pair, use for number, email in random.shuffle(zip(numbers, emails)):

If you want randomized pair of number and email from list that occur only once, use for number, email in zip(random.shuffle(numbers), random.shuffle(emails)):

CodePudding user response:

I've read your comment, and what you are describing there is not what you are asking for in the original post/question. What you want is to iterate over the same list of emails (not want to select a random email).

import time

numbers = [2589001,2589002,2589003,2589004,2589005,2589006,2589007,2589008,2589009,2589010] 
email = ["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"] 

c = 0
def vs () :
    for idx, nm in enumerate(numbers) :
        
        #Number Input
        WebDriverWait(browser, 2500).until(EC.presence_of_element_located((By.NAME, "Number"))).send_keys(nw)
        time.sleep(1)
        
        #I need code here to get email id one by one randomly from array   <--- Fix you wording. You don't actually want them one by one randomly
        email_from_list = email[idx%len(email)]
        


w = 0     
while w < 50 :
    vs ()
    w  = 1
  • Related