Python | Nested loops | Sum of Sublists - python

My goal with this is to generate the sum of each sublist separately using nested loops
This is my list called sales_data
sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
The sublist can be represented by any variable but for the purpose of this exercise, we will call it scoops_sold, which I have set to 0
scoops_sold = 0
So far I am able to run the nested loop as follows
sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0
for location in sales_data:
for element in location:
scoops_sold += element
print(scoops_sold)
This gives me 96 as the result
What I essentially want to accomplish is to return the sum of each sublist but I am not sure I might be able to do that. I thought about using slicing but that was not effective

You can easily solve it using sum():
print(list(map(sum, sales_data)))
If you insist on loops:
sums = []
for location in sales_data:
sold = 0
for element in location:
sold += element
sums.append(sold)
print(sums)

How about
[sum(sub_list) for sub_list in sales_data]
# [51, 15, 30]
However, the question is a bit confusing because you are setting scoops_sold to 0, an int, when the result you describe is a list of int.

If you want to have the sum of all subsets, you might want to use a list to store each subsets' sum, and by using the built-in python function sum, you just need to use one loop:
scoops_sold = []
for sale_data in sales_data:
salscoops_solds_sum.append(sum(sale_data))
The same result can be achieved in one line by using list comprehensions:
scoops_sold = [sum(sale_data) for sale_data in sales_data]

sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0
for location in sales_data:
print(location)
for element in location:
scoops_sold += element
print(scoops_sold)

Related

summing a column in python

I have a list of lists which consists of numerical data, a kind of matrix.
I'd like to create a function to sum up any column I later choose (1+2+9+10=?, 3+4+11+12=?, etc.)
The restraints are that I want to accomplish that by using for loops, and old-school python, no numpy, preferably without the zip function.
outside the loop I'd like to calculate an average within every column.
What would be the simplest way to accomplish that ?
Here's what I came up with thus far:
data = [[1, 3, 5, 7], [2, 4, 6, 8], [9, 11, 13, 15], [10, 12, 14, 16]]
def calc_avg(data, column):
total = 0
for row in data:
total += ....
avg = total / len(calc_avg)
later on, I would print the average for the column I choose.
Introduce a variable nr to keep count of number of rows added as you loop.
def calc_avg(data, column):
total = 0
nr = 0
for row in data:
nr += 1
total += row[column]
return total / nr
You'd probably need some counter to keep track of the "denominator" for your average -
data = [[1, 3, 5, 7], [2, 4, 6, 8], [9, 11, 13, 15], [10, 12, 14, 16]]
def calc_avg(data, column):
total = 0
counter = 0
for row in data:
total += row[column]
counter += 1
avg = total / counter
return avg
You can write a simple function to collect all column values and perform a math op.
Eg.
def get_sum_avg(chosen_column, dataset):
# filter the column values. Ignore rows with no such col
chosen_column_values = [element[chosen_column - 1] for element in dataset if len(element) >= chosen_column]
# find sum
col_sum = sum(chosen_column_values)
# find avg
average = col_sum / len(chosen_column_values) if len(chosen_column_values) > 0 else 0
return col_sum, average
data = [[1, 3, 5, 7], [2, 4, 6, 8], [9, 11, 13, 15], [10, 12, 14, 16]]
print(get_sum_avg(1, data))

How to loop through a list where data points are dependent on each other?

I have a list, a = [5, 4, 9, 3, 6, 6, 8, 2], and I essentially want to sum the first three numbers, and that sum will be the first value in the new list. The next value in the new list will be the sum of 4, 9, and 3...etc. etc. How to loop this in Python?
list slicing is a good method.
all you need to do is travese from start index to end index-2 , ( can make sum of last 3 element and when you reach second last or last ement you wont be able to take 3 elemnts)
here you can take 3 elemnts and using list slicing, slice list from the
current index to next 3 index ie a[i:i+3] where [i,i+3) is range. then on that new list performing sum operation and appending the reuslt to final list.
a = [5, 4, 9, 3, 6, 6, 8, 2]
res=[]
for i in range(len(a)-2):
res.append(sum(a[i:i+3]))
print(res)
output
[18, 16, 18, 15, 20, 16]
One liner:
list(map(sum, zip(a, a[1:], a[2:])))
Output:
[18, 16, 18, 15, 20, 16]
So you are creating the required sublists of the numbers you want to add up and then you sum each sublist using the high-order function map.
If I understood what you want:
b = [sum(a[i : i+3]) for i in range(0, len(a)-2)]

Repeat each row n times in a matrix and add a column of 1*n next to each part

I want to repeat each row of my matrix and add another column next to it.
Imagine here is my matrix
A = [[11, 12], [13, 14], [15, 16], [17, 18]]
and I want repetition of 2 times for each row, then the result will be
B = [[1, 11, 12], [2, 11, 12], [1, 13, 14], [2, 13, 14], [1, 15, 16], [2, 15, 16], [1, 17, 18], [2, 17, 18]]
I already tried below code
k = 2
B = [A] * k
which gives me error of memory in my full code
I do not know how to use panda and I am using numpy.
Is there any way to use numpy in efficient way without facing memory error, in my case,
And get the correct reply?
P.S.: I didn't add my code as I am working with huge dataset plus it is just one little little piece of whole code!
You should do
k = 2
B = A * k
instead of
k = 2
B = [A] * k
To add the new index column, you could do this
for i, sub_list in B:
sub_list.insert(i, i+1) # starting from index 1 instead of 0
Since lists are mutable, there is no need to reassign B.
#Here is my reply which solved memory problem too.
#I guess memory problem was due to exceeding array limit!!!
#I do not know HOW but below code was practically correct.
A = np.repeat(A, k, axis=0)
AB = [[1], [2]]
AB = np.reshape(AB, (-1,1))
AB = np.tile(AB,((len(A)//k),1))
B = np.hstack((AB, A))

Check if second max number is duplicated (python)

I'm learning python, I want to check if the second largest number is duplicated in a list. I've tried several ways, but I couldn't. Also, I have searched on google for this issue, I have got several answers to get/print 2nd largest number from a list but I couldn't find any answer to check if the 2nd largest number is duplicated. can anyone help me, please?
Here is my sample list:
list1 = [5, 6, 9, 9, 11]
list2 = [8, 9, 13, 14, 14]
Here is a 1-liner:
>>> list1 = [5, 6, 9, 9, 11]
>>> list1.count(sorted(list1)[-2]) > 1
True
or using heapq
>>> import heapq
>>> list1 = [5, 6, 9, 9, 11]
>>> list1.count(heapq.nlargest(2, list1)[1]) > 1
True
This is a simple algorithm:
Makes values unique
Sort your list by max value
Takes the second element
Check how many occurences of this elemnt exists in list
Code:
list1 = [5, 6, 9, 9, 11]
list2 = [8, 9, 13, 14, 14]
def check(data):
# 1. Make data unique
unique = list(set(data))
# 2. Sort by value
sorted_data = sorted(unique, reverse=True)
# 3. Takes the second element
item = sorted_data[1]
# 4. Check occurences
if data.count(item) > 1:
return True
else:
return False
print(check(list1))
print(check(list2))
Ouput
True
False
collections.Counter with sorted offers one solution:
from collections import Counter
lst1 = [5, 6, 9, 9, 11]
lst2 = [8, 9, 13, 14, 14]
res1 = sorted(Counter(lst1).items(), key=lambda x: -x[0])[1] # (9, 2)
res2 = sorted(Counter(lst2).items(), key=lambda x: -x[0])[1] # (13, 1)
The result is a tuple of second largest item and its count. It is then simple to check if the item is duplicated, e.g. res1[1] > 1.
Here is my proposal
li = [5, 6, 9, 9, 11]
li_uniq = list(set(li)) # list's elements are uniquified
li_uniq_sorted = sorted(li_uniq) # sort in ascending order
second_largest = li_uniq_sorted[-2] # get the 2nd largest -> 9
li.count(second_largest) # -> 2 (duplicated if > 1)

Python 3 how to add a single value to multi dimension array column

Let say this is the original list
a = [['john', 12, 15],
['keith', 8, 20],
['jane', 18, 10]]
I want to add a value 99 to each row, the expected result will be like
a = [['john', 12, 15, 99],
['keith', 8, 20, 99],
['jane', 18, 10, 99]]
Any build in function to achieve this result?
If you want to modify your existing lists then simply loop and append:
for l in a:
l.append(99)
If you are fine with creating new lists then use the list-comprehension suggested by #languitar.
You can use list comprehension for this:
a = [x + [99] for x in a]
Btw. what you are using is a python list, not an array.

Categories