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 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 :: get name of a file in python 
Python :: tqdm enumerate 
Python :: user input of int type in python 
Python :: openpyxl load file 
Python :: find the time of our execution in vscode 
Python :: how to open a file with python 
Python :: add a value to an existing field in pandas dataframe after checking conditions 
Python :: ImportError: dynamic module does not define module export function 
Python :: blender 2.8 python set active object 
Python :: remove first character of string python 
Python :: snakeCase 
Python :: remove first 3 columns pandas 
Python :: python code to print prime numbers 
Python :: python print odd numberrs 
Python :: python loop append to dictionary 
Python :: python dict to dataclass 
Python :: python ordereddict reverse 
Python :: create a blank image opencv 
Python :: colorbar font size python 
Python :: pandas where 
Python :: reverse range in python 
Python :: write json pythonb 
Python :: python series to list of string 
Python :: create log in python 
Python :: qtablewidget not editable python 
Python :: basic games to code in python 
Python :: how to add two numbers 
Python :: python generator comprehension 
Python :: how to create enter pressed for qlineedit in pyqt5 
Python :: python run exe 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =