Replace numbers in array for specific places only - python

There are 32 numbers in array c and all the number are 21. I want to replace the numbers in position 0,5,10,18,21,24,28 with 63. How can I do that?
a =32
b = [0,5,10,18,21,24,28]
c = []
for i in range(a):
c.append(21)

Setup:
More efficient creation of c, per Barmar
a = 32
b = [0, 5, 10, 18, 21, 24, 28]
c = [21] * a
Replace values at given indices:
list[index] allows for retrieval or assignment of a value to a specific index location in a list
for x in b:
c[x] = 63
Resources:
Python Lists

Related

How do I convert a list into an array?

I have a list and a (2 by 2) array.
I want to multiply the first two values of the list with this array 'k' shown below,
(of course by converting these two values into a (2 by 1) array).
Then I want to do the same procedure with the next two values( i.e 2nd and 3rd values).
its like [1,2] , then [2,3] ...[3,4] [4,5] ...( these each couple of lists must be in 2 by 1 array so that I can further multiply it with k (2 by 2 array) mentioned below )
The array 'k' in all the cases is the same.
How do I make this loop?
my_List = [1, 2, 3, 4, 5, 6, 7, 8,9, 10, 11, 12, 13,14, 15, 16, 17 ,18 ,19, 20, 21, 22, 23, 24, 25, 26,27 ,28 ,29, 30, 31 ,32, 33, 34,35 ,36, 37, 38, 39]
E = 1
I = 1
l = 1
k = np.array( [ [4*E*I/ l, 2*E*I/ l ],
[2*E*I/ l, 4*E*I/ l ] ] )
Is this the solution you are looking for? If not add some expected output so that this can be edited accordingly.
import numpy as np
# Turn my_List into list of (2 by 1) arrays
twosArray = [np.array([my_List[i], my_List[i+1]]) for i in range(0, len(my_List)-1, 2]
# Multiply all (2 by 1) arrays by k
for array in twosArray:
prod = k.dot(array) # you can collect prod into a list if you want to use it later
print(prod)
Test:
# I'm using simpler values for my_List. You can test with your own values
my_List = [2,2,-1,-1]
# k is the same as the one in your problem
Outputs of print:
[12. 12.]
[-6. -6.]

Take the mean values of a number of integers in one list, based on the integers in another list

I have one list containing a large number of integers, and then another list also containing integers that (if added together) sums up to the total amount of integers in the first list. I wish to create a function which iterates over the second list, and for each number in the second list, takes the mean of the values in the first list and repeats for all integers in the second list...making one final third list containing the desired mean-values.
For example: A small portion of my two lists look like this: [20, 15, 20, 30, 40, 20, 10, 8], [2, 3, 1, 2]
So since 2 is the first number in my second list, I want to take the mean of the first two integers in my first list, and then the 3 next, and so on, and add these into a third list.
Here is a brief idea of what I am thinking, but its obviously not complete.
def mean_values(list_of_numbers, numbers_for_meanvalue):
list_of_means = []
for i in numbers_for_meanvalue:
mean = sum(list_of_numbers[0]+...+list_of_numbers[i])/numbers_for_meanvalue[i]
list_of_means.append[mean]
return list_of_means
you can take a slice of a list: list[start : end].
def mean_values(list_of_numbers, numbers_for_meanvalue):
list_of_means = []
last_start = 0
for num in numbers_for_meanvalue:
mean = sum(list_of_numbers[last_start:last_start + num]) / len(list_of_numbers[last_start:last_start + num])
last_start += num
list_of_means.append[mean]
return list_of_means
I don't if I understand you correctly, but:
>>> a = [20, 15, 20, 30, 40, 20, 10, 8]
>>> b = [2, 3, 1, 2]
>>> n = 0
>>> for e in b:
... sl = a[n:n+e]
... print(sl,(sum(sl) / e))
... n += e
...
[20, 15] 17.5
[20, 30, 40] 30.0
[20] 20.0
[10, 8] 9.0

How do you compare and return duplicates in two different 2d list?

I would like to return the duplicates in two different 2d lists. But I'm having trouble figuring out what code to write. For example I would like variable "a" to compare to variable "b" and return the duplicates. Here are my two 2d list below.
a = [[2,3,6,8],[4,5,7,8,10],[15,17,21,22],[12,13,14,23,25]]
b = [[4,5,6],[15,17,21,22],[2,3,4],[2,3,6,8],[5,7,8,12,15],[7,12,14,17,32],[5,6,7,12,14]]
I would like my results to be:
c = [[2,3,6,8],[15,17,21,22]]
You just need to check if a list in a is also in b or not.
a = [[2,3,6,8],[4,5,7,8,10],[15,17,21,22],[12,13,14,23,25]]
b = [[4,5,6],[15,17,21,22],[2,3,4],[2,3,6,8],[5,7,8,12,15],[7,12,14,17,32],[5,6,7,12,14]]
c=[]
for i in a:
if i in b:
c.append(i)
print(c)
Output:
[[2, 3, 6, 8], [15, 17, 21, 22]]
This should work, it should get you started -
import itertools
#Input lists
a = [[2,3,6,8],[4,5,7,8,10],[15,17,21,22],[12,13,14,23,25]]
b = [[4,5,6],[15,17,21,22],[2,3,4],[2,3,6,8],[5,7,8,12,15],[7,12,14,17,32],[5,6,7,12,14]]
#Take a product of both the lists ( a X b )
z = itertools.product(a,b)
#Uncomment the following to see what itertools.product does
#[i for i in z]
#Return only the elements which the pair of the same element repeats (using string match)
[i[0] for i in z if str(i[0])==str(i[1])]
[[2, 3, 6, 8], [15, 17, 21, 22]]
one liner list comprehension approach:
dups = [i for i in a if i in b]
output:
[[2, 3, 6, 8], [15, 17, 21, 22]]
Try this:
a = [[2,3,6,8],[4,5,7,8,10],[15,17,21,22],[12,13,14,23,25]]
b = [[4,5,6],[15,17,21,22],[2,3,4],[2,3,6,8],[5,7,8,12,15],[7,12,14,17,32],[5,6,7,12,14]]
c = []
for i in a + b:
if (a + b).count(i) > 1 and i not in c:
c.append(i)
#mulaixi's answer is OK but in output list you may see duplicates.

How to keep specific element indexs in an array

Suppose I have two arrays:
a = [1, 4, 9, 16, 25, 36, 49, 64, 81]
b = [2,5,7]
And I want to keep the elements indexed in a that are listed in b so the 2nd, 5th and 7th index of a:
a_new = [4, 25, 49]
I will then plot b against a_new / perform analysis on it.
For my application, a is a long series of simulated data, and b is a time series of when I want to sample from this data.
Thanks
First remember that the first element of an array (or in this case a list) is number 0 and not number 1:
a = [1, 4, 9, 16, 25, 36, 49, 64, 81]
a[0] = 1 # How to adress the first element
a[1] = 4 # How to adress the second element
a[2] = 9 # ...
So the elements you want (as specified in the array b) are:
a[1] = 4 # a[1] is the same as a[2 - 1] (note that b[0] = 2)
a[4] = 25 # a[4] is the same as a[5 - 1] (note that b[1] = 5)
a[6] = 49 # a[6] is the same as a[7 - 1] (note that b[2] = 7)
So you can also access the elements this way:
a[ b[0] - 1 ] = 4 # Same as a[1] which is the second element
a[ b[1] - 1 ] = 25 # Same as a[4] which is the fifth element
a[ b[2] - 1 ] = 49 # Same as a[6] which is the seventh element
This can be wrapped up in a for-loop:
a_new = [] # Start with an empty list
for index in b: # index in b are all elements in b, thus: b[0] = 2, b[1] = 5 and b[2] = 7
a_new.append(a[ index - 1])
This loop will put the elements a[2 - 1] (4), a[5 - 1] (25) and a[7 - 1] (49) into the lista_new.
But there is a shorter way to write that loop:
a_new = [ a[ index - 1] for index in b ]
Basically, you say a_new = [ ... ], so a_new is a list, and the ... inside will specify, what the list will be filled with. In this case, it will be the elements that the for-loop produces, note that a[ index - 1] for index in b is the same for-loop as in the first example, written in a compact way.
What if you get an list index out of range error?
Your lista contains 9 elements, so the first element is a[0] and the last is a[8]. If you try to access any other element in a list, for example a[12], you will get a "list index out of range" error.
That means: the list b should only contain numbers between 1 and 9 (the length of the list, which you can find out this way len[a] = 9).
I would recommend, that you change your list b to b = [1, 4, 6], since the fifth element of an array is actually adressed like a[4] and not a[5].
The code will be a bit easier:
a_new = [ a[index] for index in b ]
If you don't want errors to happen, the values in b should then be between 0 and 8 (which is len(a) - 1), since a[0] is the first and a[8] is the last element, and only elements between that exist!
There are two possible problems that you may be encountering, both of which have been somewhat mentioned in the comments. From what I see, you either have a problem reading the or you have an invalid index in b.
For the former, you may actually want
a = [1, 4, 9, 16, 25, 36, 49, 64, 81]
b = [2,5,7]
To produce:
a_new = [9, 36, 64]
Since you always count starting from zero:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
0 1 2 3 4 5 6 7 8
Hence, leading to an XY problem, where you try to solve a problem your way which is the wrong way. Therefore, it wastes our time to try to fix a problem that doesn't work since it is actually something else.
However, for the latter, you may have an anomaly in your b list. The way to index the list (given in the comments) as you wanted is using list comprehension:
a_new = [a[i-1] for i in b]
What this does is:
a_new = []
for i in b:
a_new.append(a[i-1])
Hence, when i is larger than or equal to len(a), it evaluates to an invalid index:
>>> a = [1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> len(a)
9
>>> a[9]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range

Iterating through multiple lists with same index position in python

I am new to python and I was just trying some list manipulations. I have three lists
A = [10,20,30,40,50,60]
B = [22,44,66,88,12,10]
C = [2,4,6,8,10,20]
I want to iterate over these three lists and for each value of C I want to add half of that value to the corresponding values of A and B.
For example for the 1st iteration -
half = 2/2= 1
So A = 10 + 1 and B = 22+1
So the final lists should look something like this
A = [11,22,33,44,55,70]
B = [23,46,69,92,17,20]
C = [2,4,6,8,10,20]
As long as the lists all have the same lengths, you can iterate with enumerate() function:
for i, n in enumerate(C):
A[i] += n/2
B[i] += n/2
>>> A
[11, 22, 33, 44, 55, 70]
>>> B
[23, 46, 69, 92, 17, 20]
>>> A, B = zip(*((a + c/2, b + c/2) for a, b, c in zip(A, B, C)))
ITs best to use Numpy arrays.
import numpy as np
A, B, C = map(np.array, [A, B, C])
A, B = A - C/2, B - C/2

Categories