Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

random list python

import random
#random numbers 0-9 in a list of length 10
array = [random.randint(0,9) for i in range(10)]
print(array)
Comment

Random List Python

import random

mylist = ["apple", "banana", "cherry"]
random.shuffle(mylist)

print(mylist)
Comment

python random liste

import random

liste = ["world, HELLO !", "hello world !"]

N = random.choice(liste)

print(N)
Comment

random from list python

random.choice(seq)¶
Return a random element from the non-empty sequence seq.
Comment

python - Syntax to call random function from a list

my_list = [func_test_1, func_test_2, func_test_3]
random.choice(my_list)()
Comment

random list

"""Generation of unique random list of size n
"""
from random import sample
def unique_lst(n):
    return sample(range(10, 100), n) # return a sample of lst (unique lst)
    
# print(unique_lst(10))
Comment

random list

"""Generation of non-unique 1d_lst and 2d_lst
- Function takes in a list of info about the non-unique list

- You can also import randint from random. 
  - Note that randint is inclusive and randrange is exclusive.
  
- If you are looking to generate unique lsts...
  ...you might want to use random.sample() on a lst of numbers.
"""

from random import randrange

dft = [10, 10, 100, 10] # dft = default (range, low, high, row)

def rand_1d(info = dft[:-1]):
    r, low, high = info # r: range
    return [randrange(low, high) for num in range(r)]

def rand_2d(info = dft):
    *info_1d, rows = info
    return [rand_1d(info_1d) for i in range(rows)]

## Test
randlst_gen = {
    "rand_1d": rand_1d,
    "rand_2d": rand_2d
}
r1, r2 = randlst_gen.values() # get the functions

# #function 1
# print("rand_1d --------------------------------------------------")
# print(f"default: {r1()}")
# print(f"rand: {r1([5, 3, 234])}")
# print()
# # function 2
# print("rand_2d --------------------------------------------------")
# print("default:")
# for row in r2(): 
#     print(row)
# print()
# print("rand:")
# for row in r2([8, 11, 30, 5]): 
#     print(row)
Comment

PREVIOUS NEXT
Code Example
Python :: how to remove a string inside another string python 
Python :: how to run linux command in python 
Python :: hex python add 0 
Python :: how to capitalize the first letter in a list python 
Python :: make a script run itself again python 
Python :: how to ask a question in python 
Python :: python code to receive gmail 
Python :: read a file in python 
Python :: read binary image python 
Python :: failed to execute script 
Python :: plt.savefig specify dpi 
Python :: df rename columns 
Python :: turn false true column into 0 1 pandas 
Python :: how to mention a div with class in xpath 
Python :: how to append leading zeros in python 
Python :: does jupyter notebook need internet 
Python :: pandas change order of columns in multiindex 
Python :: # find out of the memory of the python object 
Python :: datetime library 
Python :: library for converting text into image in python 
Python :: python infinity 
Python :: how to get the current line number in python 
Python :: isntall packages to databricks 
Python :: numpy sort 
Python :: python max key dictionary key getter 
Python :: python web parse 
Python :: convert column series to datetime in pandas dataframe 
Python :: get random float in range python 
Python :: add fonts to matplotlib from a particular location 
Python :: dockerfile for django project 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =