I have a function called "first_funtion()" that returns a list of 100 instances of a class.
Then I want to define a second function that is going to create a new list with random instances taken from the output list of first_function(), something like:
first_funtion()
def second_function(list2, N):
list2 = list2(random.choice(first_function()))
The thing is that I want list2 to be always greater than N, so if N = 5 I want the random list2 to be more than 5 instances. If N = 10 then I want the list to be more that 10 instances.
How can I do that?
You can first create the list using first_function, then draw a random integer in the range between N and the length of the list and then draw a random sample from the list.
import random
def second_function(list2, N):
len_list = len(list2)
length = random.randint(N, len_list)
return random.sample(list2, length)
You can do it by using two random function;
Use first one to choose a value of N from the range excluding N to including lenght of list1, i.e, (N, len(list1)]
x = random.randint(N+1, len(list1)
And use second one to choose x number of values from list_1;
random.choices(list1, k=x)
So, the code will look something like this:
import random
def second_function(list1, N):
x = random.randint(N+1, len(list1))
y = random.choices(list1, k=x)
print(y)
Here randint(start, stop) includes both start and stop numbers while generating random integer. It will generate a random number from the inclusive range.
Andrandom.choices () function returns total k number of random items from any list.
Note: You may get repeated values if you're using random.choices (). So, use random.sample() function when you want to choose multiple random items from a list without repetition or duplicates.
You can visit on this link and explore more related to this.
As the random.choice() function always picks one element from the list, so you can iterate through the list more than N number of times and use random.choice() each time. And you can also use random.randint() to get a random number above N, to be used in the range function for the for loop.
import random
def second_function(list2, N):
for i in range(N,random.randint(N,len( first function() ))) :
list2.append(random.choice(first_function()))
return list2
Related
import itertools
Num = 11
base = list(range(1,Num+1))
Permutations = list(itertools.permutations(base))
I'm getting a memory error trying to run this. In reality I only need to generate the 1st (Num-1)! permutations but I'm not sure how to (so if Num = 7 i would need to generate the first 6! = 720 permutations). But I would ideally like to be able to generate permutations for significantly higher values of Num so any suggestions would be great
range() and permutation() both return generators that generate items on demand. You don't need to call list() and turn them into lists. Just iterate over them directly and access the items one by one.
num = 11
base = range(1, num+1)
permutations = itertools.permutations(base)
for permutation in permutations:
# Do something with `permutation`.
(Note that a generator can only be used once. If you want to iterate over the permutations more than once you'll need to call itertools.permutations() multiple times.)
To stop after n items use itertools.islice():
for permutation in itertools.islice(permutations, n):
# Do something with `permutation`.
You can skip items at the beginning, too. This would skip the first five permutations:
for permutation in itertools.islice(permutations, 5, n):
# Do something with `permutation`.
If you want to count the permutations you can add enumerate(), which attaches an index to every entry:
for i, permutation in enumerate(itertools.islice(permutations, n)):
# Skip the fifth permutation.
if i == 4:
continue
# Do something with `permutation`.
By the way, please use lowercase for variable names. Only class names should be capitalized.
I have a numeric list. I need pick a random number in a numeric list and know position number found in list.
random.choice(name_list) just find a random number but I am at the same time in need of a random number position
generate random indexes to use instead.
import random
a = [1,2,3,4,5]
rand_num = random.randrange(len(a))
print(a[rand_num])
You could also get the index and the random number in one operation using enumerate()
index,number = random.choice(list(enumerate(a)))
This not very efficient however and, if you're doing this often or on a very big list, you should do it in two steps
index = random.randrange(len(a))
number = a[index]
import random
sample_list = [1,2,3,4,5]
# this should be work for multiple same value indexes
# EX: sample_list = [1,2,3,2,2]
# if random number is 2 than below code will print indexes 1, 3, 4
random_number = random.choice(sample_list)
for i in [i for i,x in enumerate(sample_list) if x == random_number]:
print(i) # print the indexes or random number
I'm defining a function (results) that contains a for loop whose result is a random number (a). So, if the loop runs 10 times, for example, it will generate 10 different numbers. I would like to store those numbers in a list within the loop that I could then print to see what numbers were generated.
I thought of using append, though I don't know how to do this. This is my code so far, though the print statement doesn't work (I get an error saying I didn't use append correctly).
import maths
def results():
items = []
for _ in range(1,10):
a = maths.numbers()
items.append(a)
print(items)
You could do something like this:
import maths
list_with_numbers=[]
def results():
for _ in range(1,10):
a = maths.numbers()
list_with_numbers.append(a)
print(list_with_numbers)
It is obvious, but donĀ“t forget to all the function itself.
.append needs to be called on the list, not on a. list also needs to be initialized outside of the loop in order to be able to append to it. Here's a fixed version of your method:
from random import random
def results():
# First, initialize the list so that we have a place to store the random values
items = []
for _ in range(1,10):
# Generate the next value
a = random()
# Add the new item to the end of the list
items.append(a)
# Return the list
return items
Here is some more documentation on the append() method that further explains how it works.
It's also worth noting that range generates values from the start value up to (but not including) the stop parameter. So if your intent was to generate 10 values, you should do range(0, 10) since range(1, 10) will only give you 9 values.
If you wanted to take this a step further, you could use a list comprehension to avoid needing to use append altogether, and provide a parameter to indicate how many random numbers you want:
def results(num=10):
return [random() for _ in range(0, num)]
# produces a list of 10 random numbers (by default)
foo = results()
# produces a list of 20 random numbers
bar = results(20)
append is a method you have to use on the list, so basically you would do like this: randomList.append(a) and don't forget to initialize your liste beforehand at the beginning of your function: randomList = []
You have a few minor mistakes
there is no maths module
a is the number, not the list. You should append on a list
you call print after the loop ends, not on every iteration
from random import random
def results():
numbers = []
for _ in range(1,10):
a = random()
print(a)
numbers.append(a)
return numbers
import random as rd
ListNumbers1 = []
List1 = []
for j in range(1000):
ListNumbers1 = rd.randint(1,10000)
How would i get the 50 highest numbers from ListNumbers1 and append to list1?
Something like this?
List1.extend(sorted(ListNumbers1)[-50:])
You're assigning the same value over and over in your loop, destroying your list in the process. Use append...
Better: create a list comprehension of the numbers, then use heapq.nlargest to directly get the 50 highest numbers:
import random as rd
import heapq
highest_50 = heapq.nlargest(50,[rd.randint(1,10000) for _ in range(1000)])
print(highest_50)
a result:
[9994, 9983, 9983, 9968, 9934, 9925, 9913, 9912, 9909, 9909, 9902, 9889, 9884, 9880, 9811, 9794, 9793, 9792, 9765, 9756, 9750, 9748, 9738, 9737, 9709, 9707, 9704, 9700, 9691, 9686, 9635, 9618, 9617, 9605, 9604, 9593, 9586, 9584, 9573, 9569, 9569, 9557, 9531, 9528, 9522, 9438, 9438, 9431, 9402, 9400]
Just for fun, I have a more efficient solution:
from random import randint
import heapq
# create a list of 1000 random numbers
# take the negative, so the min heap does what we want
dataset = [-randint(1, 10000) for _ in range(1000)]
# O(n) function to impose the heap invariant
heapq.heapify(dataset)
# sorting is O(n log n)
# extracting from a heap is log n per item
# therefore taking the 50 biggest is much more efficent if
# we use a heap to extract only the ones we need
top50 = [-heapq.heappop(dataset) for _ in range(50)]
print top50
This is a faster solution because the 50 you want to extract is much less than the 1000 total input size. I renamed the variables, but that's just my personal preference.
Like that (notice how assigning random number is fixed):
import random as rd
ListNumbers1 = []
List1 = []
for j in range(1000):
ListNumbers1.append(rd.randint(1,10000)) # fix: append each random element
ListNumbers1.sort() # sort the list, ascending order
List1 = ListNumbers1[-50:] # get last 50 elements of the list and assign it to List1
To search for the n biggest numbers in a list you need two procedures: one to find the biggest, the other to extract the n biggest.
You could also sort the list and take the n first, but this is not my approach because I needed to keep the original list and study it as is. For example, you could also know the offsets of each chosen number, which could be useful in some case.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
def n_biggest(lis,howmany):
#this function returns the biggest of a list with its offset
def biggest_in_list(lis):
n=0
biggest=0
offset=0
for item in lis:
n=n+1
if (item>biggest):
biggest=item
offset=n-1
return[biggest,offset]
#now this is the part where we create the descending list
image=lis#the use of an image will prevent finding twice the same number
result_list=[]
if len(lis)<howmany:#this will prevent errors if list is too small
howmany=len(lis)
for i in range(howmany):
find1=biggest_in_list(image)
result_list.append(find1[0])
image.pop(find1[1])
return result_list
print n_biggest([5,4,6,10,233,422],3)#this line was for testing the above
Hope this helps,
Regards,
import random as rd
List1 = sorted([rd.randint(1,10000) for j in range(1000)])[-50:]
Slice the last 50 elements of a sorted list comprehension, then you don't need ListNumbers1
The question is to write a function called randNumMaxFor(n, maxValue), with a for loop that generates a list of n random numbers between 0 and maxValue. I can get the random list generated with a for loop but I have no clue how to get to the next step. Thanks for any hints or tips.
import random
def randomNumbers(n):
#create an empty list
myList=[]
#While loop to create new numbers
for n in range(0,9):
#create a random integer between 0 and 9, inclusive
randomNumber=int(random.random()*10)
#add the random number to the list of random numbers using the append() method
myList.append(randomNumber)
return myList
for loop that generates a list of n random numbers between 0 and maxValue
Your loop doesn't iterate from 0 to n. Replace with this:
for i in range(0,n):
You can use randint() to generate random numbers between 0 to maxValue. It will be something like randint(0, maxValue).
You can then sort the list and return the last value which will be the max value
return sorted(myList)[len(myList) - 1]
import random
def randNumMaxFor(n, maxValue):
ret = []
for i in range(n):
ret.append(random.random() * maxValue)
return ret
>>> randNumMaxFor(5, 100)
[26.72290088458923,
59.19115828283099,
92.35251365446278,
21.0800837007157,
44.83968075845669]