New to programming, help in sum of matrix using for loop [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Anyone pls help me in program to find out sum of elements in matrix using for loop.
This is my code.
a = [[1,2,3],[1,2,3],[1,2,3]]
total = 0
sha = np.shape(a)
for i in range(sha[0]):
for j in range(sha[1]):
total= total+a[i,j]
return total

Simply use sum
a = [[1,2,3],[1,2,3],[1,2,3]]
sum([sum(i) for i in a])

As it is nested list (list inside a list) you can refer to the following
a=[[1,2],[2,3],[4,5]]
total=0
for i in a:
total+=sum(i)
print(total)

a = [[1,2,3],[1,2,3],[1,2,3]]
Sum = 0
sha = np.shape(a)
for r in range(len(sha)):
for c in range(len(sha)):
sum = sum + a[r][c]
print(Sum)
Sum is looped and calculated for r->row and c-> column

Related

What's the most efficient way of traversing through all the adjacent cells of a particular cell in a 2D array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to find out the adjacent cells of a cell (i,j); which will be (i,j+1), (i,j-1), (i-1,j) and (i+1,j) in a 2D array.
My code to implement this:
for i in range(len(array)):
for j in range(len(array)):
if i==0:
adjacent_row=[i,i+1]
elif i==len(array)-1:
adjacent_row=[i,i-1]
else:
adjacent_row=[i-1,i,i+1]
if j==0:
adjacent_column=[j]
elif j==len(array)-1: # The column part is actually wrong, I don't know how to fix it
adjacent_column=[j-1,j]
else:
adjacent_column=[j-1,j+1]
for k in adjacent_row:
for l in adjacent_column:
#traverse through those cells
This was the idea that I had in my mind, but it's flawed.
For example you can do something like this
dx = [1,1,1,0,-1,-1,-1,0]
dy = [-1,0,1,1,1,0,-1,-1]
i = 2; j = 5 #The cell you want to have the neighbors
N = 10; M = 10 # Size of your array
neighbors = [] #Neighbors list
for x in dx:
for y in dy:
if i+x > 0 and i+x<N and j+y > 0 and j+y<M:
neighbors.append(array[i+x,j+y])
Here I consider my array to be a numpy array but it don't change a lot of things

Tuple into list to find max value and if there is a duplicate [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am creating a function that takes any quantity of numbers, and tell you what is the max value or if there is a tie for largest. I am wondering what I could do to simplify what I have.
def max_num(*args):
nums = []
nums_1 = []
nums.append(args)
i = 0
while i < len(nums[0]):
nums_1.append(nums[0][i])
i += 1
c = max(nums_1)
nums_1.remove(c)
if c in nums_1:
print("It's a tie!")
else:
print(c)
max_num(-10, 0, 10, 10)
So when I initially make a list with the arguments given, it gives me a tuple inside the list. This is why I create a new list to dissect the tuple into separate values. I have the feeling that wasn't necessary, and that there is a much simpler way to do this. Any advice would be great.
Just get the max, and count how many times it appears in your data:
def max_num(*args):
maxi = max(args)
if args.count(maxi) == 1:
print(maxi)
else:
print('Tie')
max_num(2, 5, 1)
#5
max_num(2, 5, 1, 5)
#Tie

How to form a given list of elements in python to create another set of lists from it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
List1 =['000095', '000094', '000092', '000101', '000099', '000096', '000095']
def makecycle(list, startElement):
A loop which forms the bottom list which is made from the upper one's elements!
if i pass that function the start element and the list it shoul print like this:
makecycle(list1, 000094) it should print:
['000094', '000092', '000101', '000099', '000096', '000095', '000094']
and if pass
makecycle(list1, 000101) it should print:
['000101', '000099', '000096', '000095', '000094', '000092', '000101']
and if pass
makecycle(list1, 000092) it should print:
['000092', '000101', '000099', '000096', '000095', '000094', '000092']
i know its kinda not clear enough but thats all i can point!
def makecycle(list1,startElement):
ind = list1.index(startElement)
l = len(list1)
i = ind
list2 = []
while ind < (l-1):
list2.append(list1[ind])
ind = ind + 1
for x in range(i):
if list1[x] not in list2:
list2.append(list1[x])
print(list2)

Creating lists with variable lengths Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to create a list with a length determined by a user input.
How would I do this?
Example: If the user inputs 3 I need a list with 3 indexes.
What do you want to fill the list with? If you just want a list with n indexes:
n = user_input_length
list = [None for x in range(n)]
You can take in input, then populate your list:
import random
num = int(input('How long do you want the list? ')) #5
lst = [random.randint(1, 10) for i in range(num)]
print lst #[6, 1, 2, 1, 8]

Python: Total sum of a list of numbers with the for loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm new to Python and I have this problem:
I need to program a Python function that gives me back the sum of a list of numbers using a for loop.
I just know the following:
sum = 0
for x in [1,2,3,4,5]:
sum = sum + x
print(sum)
I think what you mean is how to encapsulate that for general use, e.g. in a function:
def sum_list(l):
sum = 0
for x in l:
sum += x
return sum
Now you can apply this to any list. Examples:
l = [1, 2, 3, 4, 5]
sum_list(l)
l = list(map(int, input("Enter numbers separated by spaces: ").split()))
sum_list(l)
But note that sum is already built in!
l = [1,2,3,4,5]
sum = 0
for x in l:
sum = sum + x
And you can change l for any list you want.
x=[1,2,3,4,5]
sum=0
for s in range(0,len(x)):
sum=sum+x[s]
print sum

Categories