Array swapping in python - python-3.x

I want to swap elements between two array starting from a particular array index value keeping other values prior to the array index intact.
import numpy as np
r = np.array([10, 20, 30, 40, 50, 60])
p = np.array([70, 80, 90, 100, 110, 120])
t = []
for i in range(len(r)):
for j in range(len(p)):
if i >= 3 and j >= 3:
t.append(p[j])
p[j] = r[i]
for k in t:
r[i] = k
The above code does the task but the values are in reverse order.
The value that I want in array p after swapping is:
[70, 80, 90, 40, 50, 60]
and the value that i want in array r after swapping is:
[10, 20, 30, 100, 110, 120]
But in array p I am getting:
[70, 80, 90, 60, 50, 40]
and in array r I am getting:
[10, 20, 30, 120, 110, 100]
I don't know what is wrong with the code.

import numpy as np
r = np.array([10, 20, 30, 40, 50, 60])
p = np.array([70, 80, 90, 100, 110, 120])
for i in range(len(r)):
if (i>=3):
p[i],r[i] = r[i],p[i]
Above code will do the work for you. You don't need to run two for loop and t array if I understand your problem right. All you want is to swap at some indexes. You can just swap at those indexes as above no need of a temporary array t.

You can achieve the same without looping:
r = np.array([10, 20, 30, 40, 50, 60])
p = np.array([70, 80, 90, 100, 110, 120])
i = 3
temp = p[i:].copy()
p[i:] = r[i:]
r[i:] = temp
Now:
>>> p
array([70, 80, 90, 40, 50, 60])
>>> r
array([ 10, 20, 30, 100, 110, 120])

You can copy a slice of one array on to the other:
In [113]: r = np.array([10, 20, 30, 40, 50, 60])
...: p = np.array([70, 80, 90, 100, 110, 120])
...:
In [114]: t = p.copy()
In [115]: t[3:]=r[3:]
In [116]: t
Out[116]: array([70, 80, 90, 40, 50, 60])
You could also join slices:
In [117]: np.concatenate((p[:3], r[3:]))
Out[117]: array([70, 80, 90, 40, 50, 60])
Those answers create a new array. I think that's clearer than doing an inplace swap. But here's how I'd do the swap
In [128]: temp = r[3:].copy()
In [129]: r[3:]=p[3:]
In [130]: p[3:]=temp
In [131]: r
Out[131]: array([ 10, 20, 30, 100, 110, 120])
In [132]: p
Out[132]: array([70, 80, 90, 40, 50, 60])
I use copy in temp because otherwise a slice produces a view, which will get modified in the next copy. That issue has come up recently when swapping rows of a 2d array.
With lists the swapping is easier - because r[3:] makes a copy.
In [139]: r=r.tolist()
In [140]: p=p.tolist()
In [141]: temp = r[3:]
In [142]: r[3:], p[3:] = p[3:], r[3:]
In [143]: r
Out[143]: [10, 20, 30, 100, 110, 120]
In [144]: p
Out[144]: [70, 80, 90, 40, 50, 60]

Related

numpy bring values in a range

Is there a more elegant way of bringing values in a numpy array in the range 0-50?
x = np.array([-5, 6, 24, 51, 50, 40])
array([-5, 6, 24, 51, 50, 40])
x = np.where(x < 0, 0, x)
x = np.where(x > 50, 50, x)
array([ 0, 6, 24, 50, 50, 40])
In [49]: x = np.array([-5, 6, 24, 51, 50, 40])
A couple of alternatives:
In [50]: np.clip(x,0,50)
Out[50]: array([ 0, 6, 24, 50, 50, 40])
In [52]: np.minimum(np.maximum(x,0),50)
Out[52]: array([ 0, 6, 24, 50, 50, 40])
Just found out there is https://numpy.org/doc/stable/reference/generated/numpy.clip.html#numpy.clip
np.clip(x, 0, 50)
array([ 0, 6, 24, 50, 50, 40])

Adding in a single 2D / Matrix with a string

How do I add element 1 in all the lists? After that, add element 2? I need to find the percentage of them.
rows = [["SOBs", 60, 80, 70, 75], ["Test1", 60, 50, 60, 65], ["Test2", 40, 30, 40, 45], ["Test3", 45, 90, 80, 85], ["CW", 40, 80, 70, 75]]
I have tried in this manner:
sum(sum(rows, [2])) - this doesn't work
print(sum(rows[0][1] + [1][1])) - also doesn't work
So the for element 1 in it would be 60+60+40+45+40 = 245
Then I take the 245/500*100 = 49%
You can try with list comprehension easily.
element = 1
rows = [["SOBs", 60, 80, 70, 75], ["Test1", 60, 50, 60, 65], ["Test2", 40, 30, 40, 45], ["Test3", 45, 90, 80, 85], ["CW", 40, 80, 70, 75]]
sum_1= sum([rows[i][element] for i in range(len(rows))])
element = 2
sum_2= sum([rows[i][element] for i in range(len(rows))])
print((sum_1*100)/(sum_1+sum_2))
Consider it like a table, and use comprehensions, to summarize columns.
Try this
rows = [["SOBs", 60, 80, 70, 75], ["Test1", 60, 50, 60, 65], ["Test2", 40, 30, 40, 45], ["Test3", 45, 90, 80, 85],
["CW", 40, 80, 70, 75]]
COL_LENGTH = 4 # set as 4, as you have only 4 colums to evaluate
# Create a list of column values for each column index
selected_cols = [[row[i] for row in rows] for i in range(1, COL_LENGTH)]
# Get sum of every column
sums = [sum(col) for col in selected_cols]
# get avg of every column
avgs = [_sum / 5 for _sum in sums]

How to match a value on a dictionary?

0Dear all I have this code below: (I do know it's incorrect, by the way):
list1 = ([33, 37], [38, 45], [46, 54], [55, 62], [63, 74], [75, 79], [80, 90], [91, 95], [96, 110], [111, 112], [113, 125], [126, 147], [148, 159], [160, 185])
list2 = [100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 400, 450, 500]
new_dict = {k: v for k, v in zip(list2, list1)}
print(new_dict)
weight = input("enter weight :")
for key, value in new_dict.items():
value = int(new_dict[value])
if weight <= [value] or weight >= [value]:
dose = k
print(dose)
Basically what I am trying to come up with is a program that dose bands a med for you. I'll explain, the drug in question is infliximab and I am using a dose of 3mg/kg, so if I was to have a patient that was 36kg, i'd get a dose of 108mg, which I'd like my program to go through the values in the dictionary (list1) find out that 36 is between 33 and 37 and return a dose of 100mg (key) instead.
I am quite new to python and I do know there is a simple way but I currently lack the wherewithal and would appreciate any help.
Thanks
You are on the right track, but rather than zip the lists together making them a little harder to work with for this problem, let's find the proper range and it's index and use that index to find the dose.
list1 = ([33, 37], [38, 45], [46, 54], [55, 62], [63, 74], [75, 79], [80, 90], [91, 95], [96, 110], [111, 112], [113, 125], [126, 147], [148, 159], [160, 185])
list2 = [100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 400, 450, 500]
weight = float(input("enter weight :"))
dose = "n/a"
for index, bracket in enumerate(list1):
if bracket[0] <= weight <= bracket[1]:
dose = list2[index]
break
print(dose)
Alternatively, if you are keen on using zip() then you might:
list1 = ([33, 37], [38, 45], [46, 54], [55, 62], [63, 74], [75, 79], [80, 90], [91, 95], [96, 110], [111, 112], [113, 125], [126, 147], [148, 159], [160, 185])
list2 = [100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 400, 450, 500]
weight = float(input("enter weight :"))
dose = [amt for bracket, amt in zip(list1, list2) if bracket[0] <= weight <= bracket[1]]
if dose:
print(dose[0])

Sorting of lists in number ranges

list = [1,2,,3,4,5,6,1,2,56,78,45,90,34]
range = ["0-25","25-50","50-75","75-100"]
I am coding in python. I want to sort a list of integers in range of numbers and store them in differrent lists.How can i do it?
I have specified my ranges in the the range list.
Create a dictionary with max-value of each bin as key. Iterate through your numbers and append them to the list that's the value of each bin-key:
l = [1,2,3,4,5,6,1,2,56,78,45,90,34]
# your range covers 25 a piece - and share start/endvalues.
# I presume [0-25[ ranges
def inRanges(data,maxValues):
"""Sorts elements of data into bins that have a max-value. Max-values are
given by the list maxValues which holds the exclusive upper bound of the bins."""
d = {k:[] for k in maxValues} # init all keys to empty lists
for n in data:
key = min(x for x in maxValues if x>n) # get key
d[key].append(n) # add number
return d
sortEm = inRanges(l,[25,50,75,100])
print(sortEm)
print([ x for x in sortEm.values()])
Output:
{25: [1, 2, 3, 4, 5, 6, 1, 2], 50: [25, 45, 34],
75: [56], 100: [78, 90]}
[[1, 2, 3, 4, 5, 6, 1, 2], [25, 45, 34], [56], [78, 90]]
Another stable bin approach for your special case (regular intervaled bins) would be to use a calculated key - this would get rid of the key-search in each step.
Stable search means the order of numbers in the list is the same as in the input data:
def inRegularIntervals(data, interval):
"""Sorts elements of data into bins of regular sizes.
The size of each bin is given by 'interval'."""
# init dict so keys are ordered - collection.defaultdict(list)
# would be faster - but this works for lists of a couple of
# thousand numbers if you have a quarter up to one second ...
# if random key order is ok, shorten this to d = {}
d = {k:[] for k in range(0, max(data), interval)}
for n in data:
key = n // interval # get key
key *= interval
d.setdefault(key, [])
d[key ].append(n) # add number
return d
Use on random data:
from random import choices
data = choices(range(100), k = 50)
data.append(135) # add a bigger value to see the gapped keys
binned = inRegularIntervals(data, 25)
print(binned)
Output (\n and spaces added):
{ 0: [19, 9, 1, 0, 15, 22, 4, 9, 12, 7, 12, 9, 16, 2, 7],
25: [25, 31, 37, 45, 30, 48, 44, 44, 31, 39, 27, 36],
50: [50, 50, 58, 60, 70, 69, 53, 53, 67, 59, 52, 64],
75: [86, 93, 78, 93, 99, 98, 95, 75, 88, 82, 79],
100: [],
125: [135], }
To sort the binned lists in place, use
for k in binned:
binned[k].sort()
to get:
{ 0: [0, 1, 2, 4, 7, 7, 9, 9, 9, 12, 12, 15, 16, 19, 22],
25: [25, 27, 30, 31, 31, 36, 37, 39, 44, 44, 45, 48],
50: [50, 50, 52, 53, 53, 58, 59, 60, 64, 67, 69, 70],
75: [75, 78, 79, 82, 86, 88, 93, 93, 95, 98, 99],
100: [],
125: [135]}

Picking the First string from a List

how i can Pick only the First pick in a List in this example?
List = [[110, 10, 20 , 30], [120, 40, 50, 60], [130, 70, 80, 90]]
The Output should be:
Times = [110, 120, 130]
Thank you
Come on
[l[0] for l in my_list]
Try this new_list = old_list[0].
Wouldnt this just Show the Output
[110, 10, 20, 30]

Resources