I want to deal with matrices without using numpy [duplicate] - python

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 8 years ago.
I want to make my own matrix solution code. I having problem when taking values. my code is below:
ask_r = int(raw_input("How many rows: "))
ask_c = int(raw_input("How many columns: "))
"""
[1,2,3]
[3,4,5]
[7,8,9]
"""
"""
matrix = [[1,2,3],[4,5,6],[7,8,9]]
"""
col_m = [0]*ask_c
row_m = [col_m]*ask_r
for i in range(ask_r):
for j in range(ask_c):
val = input("Put in: ")
row_m[i][j] = val
print row_m
What's wrong there as the input changes the value of all the three inner lists(considered as columns).

row_m = [col_m]*ask_r creates a list of ask_r times the same reference to col_m.
You are looking for something like
row_m = [[0]*ask_c for _ in range(ask_r)]

Related

How to input values in list using input function only in python? [duplicate]

This question already has answers here:
How to add new value to a list without using 'append()' and then store the value in a newly created list?
(5 answers)
Closed 2 years ago.
I want to make inputs in a list using input command here is my code
please see it
n=int(input())
li=[]
i=0
for i in range(0,n,1):
li[i]=int(input())
li
You can use list comprehensions for this,
If the input is on the same line and space separated,
li = list(map(int, input().split()))
If the input is on new lines,
li = [int(input()) for _ in range(n)]
You can use a list comprehension:
n = 3
mylist = [int(input('Type value: ')) for i in range(n)]
You can input n number of inputs at a time:
For example: you have
input = [1,2,3,4,5]
then, you can simply take input in one line as below:
List = map(int, input.split(" "))
I hope it helped you... you have any queries post your queries.

Different output from a function with the same arguments [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 2 years ago.
Why is my function 'increment' return different values for matrix that i create by other function and different for manual matrix?
n = 2
m = 3
indices = [[0,1],[1,1]]
def matrixpopulation(n,m):
row=[]
matrix=[]
row+=(0 for _ in range(0,m))
matrix+=(row for _ in range(0,n))
return matrix
def increment(indices,matrixa):
for v,k in indices:
for i in range(3):
matrixa[v][i]+=1
for i in range(2):
matrixa[i][k]+=1
return matrixa
matrixa=matrixpopulation(n,m)
filled_matrix=increment(indices,matrixa)
print(matrixpopulation(n,m))
print(filled_matrix)
manualmatrix=[[0,0,0],[0,0,0]]
print(manualmatrix)
print(increment(indices,manualmatrix))
matrix+=(row for _ in range(0,n))
When you make matrix here you actually add the reference to the same row n-times. When you modify some element in one 'row', all other rows are modified as well. For example:
a = [1, 2]
b = [a, a]
a[0] = 3
Check b now.

Get the number of times the number "1" appears in my list, without using if statements or empty lists in for loop? [duplicate]

This question already has answers here:
How do I count the occurrences of a list item?
(29 answers)
Closed 3 years ago.
How do you get the number of times the number 1 appears in the following list:
list = [1,2,3,4,5,1,1]
I have tried the following and it works, but I want to do it without creating an empty list and the if statement, but what I really want is a count of the number of times 1 appears instead of a list with only 1s and then count them.
list = [1,2,3,4,5,1,1]
list_1s = []
for i in list:
if i == 1:
list_1s.append(i)
else:
pass
n_1s = len(list_1s)
print(n_1s)
lst = [1,2,3,4,5,1,1]
Using list comprehensions to solve it -
count_1s = len([i for i in lst if i==1])
print(count_1s)
3

Python generate a list of consecutive numbers from a list of numbers [duplicate]

This question already has answers here:
Identify groups of consecutive numbers in a list
(19 answers)
How to pick consecutive numbers from a list [duplicate]
(2 answers)
Closed 4 years ago.
I have a list like this,
List1 = [1,2,3,7,8,11,14,15,16]
And I want to use python to generate a new list that looks like,
List2 = ["1:3", "7:8", "11", "14:16"]
How can I do this, is for loop the option here.
I don't want to use For loop as my list has more than 30 000 numbers.
You can use a generator:
List1 = [1,2,3,7,8,11,14,15,16]
def groups(d):
c, start = [d[0]], d[0]
for i in d[1:]:
if abs(i-start) != 1:
yield c
c = [i]
else:
c.append(i)
start = i
yield c
results = [str(a) if not b else f'{a}:{b[-1]}' for a, *b in groups(List1)]
Output:
['1:3', '7:8', '11', '14:16']

Declaration of dynamic list using for loop in python [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 6 years ago.
I just want to declare some range of lists depending on the input.
For example, if I give the input as 4, I would be expecting 4 lists like
list1 = []
list2 = []
list3 = []
list4 = []
I have tried this
for i in range (1,4):
list(%d) %i= []
But its not working.
I agree with PM-2ring's that in most contexts a list of lists or a dict would be more appropriate.
d = {}
for i in range(1,4):
d['list%s' % i] = []
or
l = [[] for x in range(1,4)]
But to answer the question actually asked in case this is one of the very rare cases where it is the right thing to do even with all the problems associated with dynamic creation of variables (maintainability, access, sanitizing user input, etc).
You probably shouldn't do any of the below if it is new information to you.
It depends on the context a bit. If you're inserting them into an object it would be:
a = classA()
for i in range(1,4):
setattr(a, 'list%s' % i, [])
If trying to create a global var:
for i in range(1,4):
globals()['list%s' % i] = []

Categories