Related
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]
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])
I'm a total beginner, self-learner and I'm trying to solve the problem 5 from How to Think Like a Computer Scientist: Learning with Python 3. The problem looks like this:
xs = [12, 10, 32, 3, 66, 17, 42, 99, 20]
Write a loop that adds all the numbers from the list into a variable called total. You should set the total variable to have the value 0 before you start adding them up, and print the value in total after the loop has completed.
Here is what I tried to do:
for xs in [12, 10, 32, 3, 66, 17, 42, 99, 20]:
xs = [12, 10, 32, 3, 66, 17, 42, 99, 20]
total = 0
total = sum(xs)
print(total)
Should I use a for loop at all? Or should I use a sum function?
There is no need for a for loop here simply:
xs = [12, 10, 32, 3, 66, 17, 42, 99, 20]
total = sum(xs)
print(total)
If you really want to use a loop:
total = 0
xs = [12, 10, 32, 3, 66, 17, 42, 99, 20]
for i in xs:
total += i
print(total)
How can I combine the 2 lists below as in the example? len(list1+list2) = 5 but len(output)=2. Thank you for attention
List1 = [array([12, 10, 8, 5, 7, 3]),
array([24, 58, 49, 30,70,6])]
len(list1) = 2
List2 = [array([63, 15, 72, 23,89,3]),
array([27, 60, 47, 29,57,6]),
array([47, 28, 50, 35,21,8])]
len(list2) = 3
Example :
Output = [
[[12, 10, 8, 5, 7, 3], [24, 58, 49, 30,70,6]],
[[63, 15, 72, 23,89,3],[27, 60, 47, 29,57,6],[47, 28, 50, 35,21,8]]
]
len(output) = 2
Just use append command.
Output = []
Output.append(List1)
Output.append(List2)
The reason you got two is because there are two elements in the list(two lists).
If have a list you can use the extend method to add a list like this:
lis1 = [34,32,12]
lis2 = [56,21,54]
lis1.extend(lis2)
print(lis1) # [34, 32, 12, 56, 21, 54]
You need to append each variable in the list to the new list
output = []
for i in range(len(list1):
output.append(list1[i])
And than do it for the second list
If you’ll just do output.append(list1) than list 2
it will be the same as your first output the one with len = 2
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]