Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

2d list comprehension python

[[float(c) for c in row] for row in data]
Comment

create 2d array python list comprehension

#by taking the user input
def main():
    #write your code here
    row = int(input())
    col  = int(input()
    
    #using the list comprehensions
    matrix = [[int(input())for j in range(col)]for j in range(row)]
    #display the matrix
    for i in range(row):
        for j in range(col):
            print(matrix[i][j],end=" ")
        print()

if __name__ == "__main__":
    main()
Comment

2d list comprehension python


[int(x) for line in data for x in line.split()]

Comment

how list comprehension for 2D works

dx = [3, 4, 5, 9]
dy = [5, 3, 2, 1]
output_matrix = []
 
for dx_n in dx:
	dx_row = []
	for dy_n in dy:
		dx_row.append(dx_n * dy_n)
	output_matrix.append(dx_row)
 
# Output is [[15, 9, 6, 3], [20, 12, 8, 4], [25, 15, 10, 5], [45, 27, 18, 9]]
Comment

PREVIOUS NEXT
Code Example
Python :: plural name django 
Python :: python discord bot join voice channel 
Python :: for each digit in number python 
Python :: tkinter give button 2 commands 
Python :: python error get line 
Python :: python keylogger 
Python :: from string to time python dataframe 
Python :: How to perform run-length encoding in Python? 
Python :: install easygui 
Python :: install re package python 
Python :: show rows with a null value pandas 
Python :: how to download file from python 
Python :: how to print image with cv2 
Python :: print all keys having same value 
Python :: supprimer fichier pythpn 
Python :: pygame change logo 
Python :: count none in list python 
Python :: pretty print list python 
Python :: torch save state dict 
Python :: dns request scapy 
Python :: pandas_datareader 
Python :: python get all folders in directory 
Python :: read database pandas 
Python :: pytorch tensor change dimension order 
Python :: pandas rename column 
Python :: python average of two lists by row 
Python :: list to json python 
Python :: array for each in python 
Python :: bgr to gray opencv 
Python :: panda get rows with date range 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =