I have a list that contains two lists and I am looking to randomly select one of the values within both of these lists and then multiply them by 0.5
For example, I receive a list like this:
[[-0.03680804604507722, 0.022112919584121357], [0.05806232738548797, -0.004015137642131433]]
What it sounds like you want to do is iterate through your list of lists, and at each list, randomly select an index, multiply the value at that index by 0.5 and place it back in the list.
import random
l = [[-0.03680804604507722, 0.022112919584121357], [0.05806232738548797, -0.004015137642131433]]
# for each sub list in the list
for sub_l in l:
# select a random integer between 0, and the number of elements in the sub list
rand_index = random.randrange(len(sub_l))
# and then multiply the value at that index by 0.5
# and store back in sub list
sub_l[rand_index] = sub_l[rand_index] * 0.5
You can use randint and the length of the list.
from random import randint
lst = [[-0.03680804604507722, 0.022112919584121357], [0.05806232738548797, -0.004015137642131433]]
for L in lst:
L[randint(0, len(L) - 1)] *= 0.5
Related
I am trying to choose the larger of the first and last elements of an array of len(3) and make that all the elements of the array:
def max_end3(nums):
a = max(nums[0], nums[2])
for i in nums:
i = a
return nums
So you want to return a new list that is the maximum of the first and last element?
In Python you can access the last element with [-1].
so a solution that doesn't care about the length of the list(minimum one element to work) would look like this:
def set_list_to_max_of_first_last(nums):
return [max(nums[0], nums[-1])] * len(nums)
What we do here is we use [] to create a new list with one element. Then we make the array as long as the original with identical entries.
HTH
Rather than iterating the list -
def max_end3(nums):
max_elem = max(nums[0], nums[-1])
n = len(nums)
return [max_elem] * n
>>> l = [1,5,67,100]
>>> max_end3(l)
[100, 100, 100, 100]
I need to create an List of size N and then initialize only N-1th and N-2th elements only. Which means if the size of the list is 5 then it should only contain elements in 3rd and 4th position.
i know how to do it in C++ but is there any way to implement it in Python?
for example: In C++
int *n = new int[5];
n[3] = 20
n[4] = 10
//and if we print the output it will show some garbage values in index 0, 1, 2 and will print 20 10 which is the values we initailized
How can i do it in python? or anything similar to this!
In python, list must be initialized with values.
Closest thing you can do:
N = 5
lst = [0] * (N-2) + [20, 10]
This:
Fills the N-2 elements of a list with default value 0
Sets the value for the last two elements
Concatenates the zeros and last two elements sub-lists of stages 1 & 2
In python,
array=[]
length=5
for i in range(length):
array.append(0)
array[3]=20
array[4]=10
Edit: As pointed out by kabanus, a more efficient way to do this would be-
array=[0]*length
Instead of the for loop.
I have two lists of strings, each has 260 element. I want to mix 100 items randomly between the the two lists . the 100 I want randomly without giving specific range since I am going to do that at 100 cycles.
for example : if list a is [x1,x2,x3..,x260] and b is [y1,y2,y3..',y260]
output : a [x1,y3,y5,..] b [y1,y2,x5..].
I thought to do that with shuffling but didn't know if it is possible to do random shuffling without giving a range with only a number of items
Something like this?
import random
def get_mixed_result(a: list, b: list) -> list:
items = []
both_lists = [a, b]
for i in range(100):
selected_list = random.choice(both_lists)
selected_item = random.choice(selected_list)
items.append(selected_item)
return items
i do learn Python for scientific working. At the moment i try to generate a 10x10 random Matrix with binary entries: 0 and 1. I already got a solution with numpy BUT im interested of what is the error in my own solution.
The Idea is to Access every entry of my Matrix seperately and assign a value to it by calling random.randint(0, 1 ) within two while loops. In Advance i define a dummy 10x10 Matrix called "World" and reassign ist values it in the loop. The Code Looks how follows:
import random
World=list(10*[10*[0]]) #this is my dummy matrix
i=0
j=0
while i <= 9:
while j <= 9:
World[i][j]=random.randint(0, 1) #here i want to Access a specific element of my dummy Matrix and "overwrite" it
if j == 9:
j=0 #if the counter "j" reaches 9 - the last element - it shall assign j=0 and leave the innermost while loop by "break"
break
j=j+1
i=i+1
for x in World:
print(*x)
The Problem with the Output should be obvious:
columns are equal
I am hopefully u understand what was my Intention here and can help me fix my code. I tried many many Things but i did not fix this.
I already found a 2-line short solution which i will use in my final Code but i want to run this also on my own because i am convinced this could work also well.
Many Thanks in Advance.
- Wendel
Your error is in the creation of the list.
NOTE:
[0] * m returns just a reference to a list of m zeros, but not a list.
The subsequent repeating of this element creates a list of n items
that all reference to the same list (just as well as the operation b =
a for lists does not create the new list), so all rows in the
resulting list are actually the same string.
import random
#World=list(10*[10*[0]]) #this is my dummy matrix
n = 10
World= [0] * n
for i in range(n):
World[i] = [0] * n
i=0
j=0
while i <= 9:
while j <= 9:
World[i][j]=random.randint(0, 1) #here i want to Access a specific element of my dummy Matrix and "overwrite" it
if j == 9:
j=0 #if the counter "j" reaches 9 - the last element - it shall assign j=0 and leave the innermost while loop by "break"
break
j=j+1
i=i+1
for x in World:
print(*x)
Suppose that two numbers are given: the number of rows of n and the number of columns m. You must create a list of size n×m, filled with, say, zeros.
The obvious solution appears to be wrong:
a = [[0] * m] * n
This can be easily seen if you set the value of a[0][0] to 5, and then print the value of a[1][0] — it will also be equal to 5. The reason is, [0] * m returns just a reference to a list of m zeros, but not a list. The subsequent repeating of this element creates a list of n items that all reference to the same list (just as well as the operation b = a for lists does not create the new list), so all rows in the resulting list are actually the same string.
n = 3
m = 4
a = [[0] * m] * n
a[0][0] = 5
print(a[1][0])
A possible way: you can create a list of n elements (say, of n zeros) and then make each of the elements a link to another one-dimensional list of m elements:
n = 3
m = 4
a = [0] * n
for i in range(n):
a[i] = [0] * m
Another (but similar) way: create an empty list and then append a new element to it n times (this element should be a list of length m):
n = 3
m = 4
a = []
for i in range(n):
a.append([0] * m)
But the easiest way is to use generator, creating a list of n elements, each of which is a list of m zeros:
n = 3
m = 4
a = [[0] * m for i in range(n)]
In this case each element is created independently from the others. The list [0] * m is n times consructed as the new one, and no copying of references occurs.
I want to generate a list of lists that contains a progressive number of randomly generated binary values.
How do I add a condition that tells python to add random values to a list until it reaches a specified length? In this case, the length of each new list should be a progressively larger odd number.
from random import randint
shape = []
odds = [x for x in range(100) if x % 2 == 1]
while len(shape) < 300:
for x in odds:
randy = [randint(0,1)] * x ?? # need to run this code x amount of times
shape.append(randy) # so that each len(randy) = x
*I would prefer to not use count += 1
desired output:
shape
[[0],[0,1,0],[1,1,0,1,0],[1,0,0,0,1,1,0]...etc]
You want a generator expression list comprehension:
randy = [randint(0, 1) for i in range(x)]
The problem with [someFunc()] * someNum is that Python first evaluates the inner expression, someFunc() and resolves it to some number before executing the outer expression.