I'm trying to create a function where a user inputs the values for a 3x4 2-D list, then the function prints the grid of numbers, along with an extra row at the bottom with the sum of each column. At the moment, I can't even get it to take any inputs without getting errors. Here's what I've got so far, without the addition of the columns at the end.
def testMatrixFunctions():
row0 = raw_input("Enter a 3-by-4 matrix row for row 0: ")
row0.split()
map(float, row0.split())
[float(c) for c in row0.split()]
row1 = raw_input("Enter a 3-by-4 matrix row for row 1: ")
row1.split()
map(float, row1.split())
[float(c) for c in row1.split()]
row2 = raw_input("Enter a 3-by-4 matrix row for row 2: ")
row2.split()
map(float, row2.split())
[float(c) for c in row2.split()]
print row0
print row1
print row2
You can use numpy to get the column sum
import sys
import numpy
ROWS = 3
COLS = 4
def main():
global ROWS
global COLS
matrixData = []
# Get input and process it
for i in range(0, ROWS):
thisRow = raw_input('Enter a 3-by-4 matrix row for row ' + str(i) + ' : ').split()
evalRow = []
[evalRow.append(eval(element)) for element in thisRow]
matrixData.append(evalRow)
numpyMatrix = numpy.array(matrixData)
colSums = []
[colSums.append(numpy.sum(numpyMatrix[:,index])) for index in range(0,COLS)]
# Print the matrix
for row in matrixData:
print row
print colSums
return
if __name__ == '__main__':
main()
sys.exit(0)
Related
I have two pandas dataframes of which shapes are "n x n" and "m x n" (m < n). For example:
df1 = pd.DataFrame([[0,1,0,1],[1,0,0,1],[0,0,0,1],[1,1,1,0]])
df2 = pd.DataFrame([[1,1,1,0],[1,1,0,1]])
I'd like to get the dataframe of a square matrix by concatenating above dataframes:
df3 = foo(df1, df2)
print df3.values
This should print like the following matrix.
[[0,1,0,1,1,1],
[1,0,0,1,1,1],
[0,0,0,1,1,0],
[1,1,1,0,0,1],
[1,1,1,0,0,0],
[1,1,0,1,0,0]]
The logic of concatination is like this:
the upper-left part of the square matrix comes from df1
the upper-right part of it comes from the transpose of df2
the bottom-left part of it comes from df2
all element of the rest of it (bottom-right part) is zero.
How do I implement the above logic (foo method)?
Here is a sample of foo:
def foo(_df1,_df2):
df1 = _df1.reset_index(drop=True) #to make sure the index is ordered
df2 = _df2.reset_index(drop=True) #to make sure the index is ordered
df2_transpose = df2.transpose().reset_index(drop=True) #reset the index to match the join below
df_upper = df1.join(df2_transpose,rsuffix="_") #add suffix for additional columns
df_upper.columns = [i for i in range(df_upper.shape[1])] #reset column names to int
df = pd.concat([df_upper,df2]) #fill the bottom left
df.fillna(0,inplace=True) #fill with 0 the bottom right
return df
The foo function:
def foo(df1_data,df2_data):
df_test = pd.concat([df1_data,df2_data])
a = np.concatenate((df2_data.values.T,np.zeros(shape = (df_test.values.shape[0] - df_test.values.shape[1],df2_data.values.shape[0]))))
final_array = np.append(df_test.values,a, axis=1).astype(int)
df3_data = pd.DataFrame(final_array)
return df3_data
df3 = foo(df1,df2)
Here m trying to extract a list of blacklist IP from a specific site and trying to make an excel sheet of the following fields :
IP , date ...... New updated code is :
import xlwt
import urllib
def Bl():
link = 'https://www.dshield.org/ipsascii.html?limit=100'
p = urllib.urlopen(link)
text = p.readlines()
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('python')
sheet.write(0,0,'Blacklist IP')
sheet.write(0,1,'Reports')
sheet.write(0,2,'abcd')
sheet.write(0,3,'date')
sheet.write(0,4,'Date')
row = 1 #counter for rows.
col = 0 #counter for columns.
x = 0 #counter for printing the n'th element in the string w.
for line in text:
li = line.strip()
w = line.split()
if not li.startswith("#"):
sheet.write(row,col,w[0])
sheet.write(row,1,w[1])
sheet.write(row,2,w[2])
sheet.write(row,3,w[3])
sheet.write(row,4,w[4])
row = row + 1
wbk.save('new.xls')
Bl()
I believe it should be this, though my python isn't great:
import xlwt
import urllib
link = 'https://www.dshield.org/ipsascii.html?limit=100'
p = urllib.urlopen(link)
text = p.readlines()
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('Blacklist IP')
row = 1 #counter for rows.
col = 0 #counter for columns.
#x = 0 #counter for printing the n'th element in the string w.
for line in text:
li = line.strip()
w = line.split()
print "The line " + line + " was split into " + len(w) + " size"
for i in range(0,len(w)-1):
if not li.startswith("#") and not li.isspace():
sheet.write(row,col,w[i])
#x = x + 1
col = col + 1
if col > 256:
print "Line of " + li
print "Row: " + row
print "Col: " + col
raise ValueError('Too many columns, can\'t insert ' + w)
row = row + 1
wbk.save('new.xls')
Reasoning is that I believe you should be changing column every time you write a value, since that appears to be causing your current error. I also believe You should be changing row after each line, not just at the end of the code which then appears superfluous
[Edit update after user feedback]
I currently have a piece of code that works in two segments. The first segment opens the existing text file from a specific path on my local drive and then arranges, based on certain indices, into a list of sub list. In the second segment I take the sub-lists I have created and group them on a similar index to simplify them (starts at def merge_subs). I am getting no error code but I am not receiving a result when I try to print the variable answer. Am I not correctly looping the original list of sub-lists? Ultimately I would like to have a variable that contains the final product from these loops so that I may write the contents of it to a new text file. Here is the code I am working with:
from itertools import groupby, chain
from operator import itemgetter
with open ("somepathname") as g:
# reads text from lines and turns them into a list sub-lists
lines = g.readlines()
for line in lines:
matrix = line.split()
JD = matrix [2]
minTime= matrix [5]
maxTime= matrix [7]
newLists = [JD,minTime,maxTime]
L = newLists
def merge_subs(L):
dates = {}
for sub in L:
date = sub[0]
if date not in dates:
dates[date] = []
dates[date].extend(sub[1:])
answer = []
for date in sorted(dates):
answer.append([date] + dates[date])
new code
def openfile(self):
filename = askopenfilename(parent=root)
self.lines = open(filename)
def simplify(self):
g = self.lines.readlines()
for line in g:
matrix = line.split()
JD = matrix[2]
minTime = matrix[5]
maxTime = matrix[7]
self.newLists = [JD, minTime, maxTime]
print(self.newLists)
dates = {}
for sub in self.newLists:
date = sub[0]
if date not in dates:
dates[date] = []
dates[date].extend(sub[1:])
answer = []
for date in sorted(dates):
print(answer.append([date] + dates[date]))
enter code here
enter code here
I create a list of dictionaries from excel. The code is listed below. What happens is the list contains the last row excel values for all the rows. I tried in python shell. It works fine. Why all the rows get updated with last row values?
d = {}
l = []
up = os.path.join(request.folder,'uploads')
workbook = xlrd.open_workbook(os.path.join(request.folder,'uploads','meas.xls'))
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
num_cells = worksheet.ncols - 1
curr_row = -1
while curr_row < num_rows:
curr_row += 1
row = worksheet.row(curr_row)
#print 'Row:', curr_row
curr_cell = -1
while curr_cell < num_cells:
curr_cell += 1
# Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blank
cell_type = worksheet.cell_type(curr_row, curr_cell)
cell_value = worksheet.cell_value(curr_row, curr_cell)
#print ' ', cell_type, ':', cell_value
if curr_cell == 0:
d['loc_of_work'] = cell_value
if curr_cell == 1:
d['n'] = cell_value
if curr_cell == 2:
d['t'] = cell_value
if curr_cell == 3:
d['l'] = cell_value
if curr_cell == 4:
d['b'] = cell_value
if curr_cell == 5:
d['d'] = cell_value
print 'dict'
print d.items()
l.append(d)
print 'len of list:'
print len(l)
print 'list:'
for i,j in enumerate(l):
print i,j
The issue is you are declaring d outside the while loop, which means within the loop you are simply overwriting the same dict with new values on every iteration. Your list simply contains multiple references to the same dict object, which contains the values from the last row because those are the last values to be written to the dict (all previous values are overwritten)
moveoing:
d = {}
inside the first while loop should fix your issue
I'm trying to build a table from user input to export via Cheetah to fill a template to use as a report. I'm having trouble separating each iteration of the loop
"for j in range(1, numErrors):" and put table row tags at the beginning and end of each concatenation.
table = ""
cells = ""
row = ""
numMeas = int(raw_input("Enter total number of measurements: "))
numMeas = numMeas + 1 #number of measurements compensated for iteration behavior
for i in range(1, numMeas):
typeMeas = raw_input("Enter type of measurement "+str(i)+": ")
numErrors = int(raw_input("Enter number of error sources: "))
numErrors = numErrors + 1
for j in range(1, numErrors): #builds dataSet from number of errors
inputData = []
inputData.append(typeMeas)
description = raw_input("Enter source of uncertainty "+str(j)+": ")
inputData.append(description)
estUncert = raw_input("Enter estimated uncertainty "+str(j)+": ")
estUncert = float(estUncert)
inputData.append(str(estUncert))
for i in inputData:
cell = "<td>"+str(i)+"</td>"
cells += cell
table = "<tr>"+cells+"</tr>"+"\n"
print table
Current output:
<tr><td>mass</td><td>scale</td><td>1.0</td><td>mass</td><td>human</td><td>2.0</td> <td>temp</td><td>room</td><td>3.0</td><td>temp</td><td>therm</td><td>4.0</td></tr>
Desired output:
<tr><td>mass</td><td>scale</td><td>1.0</td></tr>
<tr><td>mass</td><td>human</td><td>2.0</td></tr>
<tr><td>temp</td><td>room</td><td>3.0</td></tr>
<tr><td>temp</td><td>therm</td><td>4.0</td></tr>
I am guessing it probably needs to look like this:
table = ""
cells = ""
row = ""
numMeas = int(raw_input("Enter total number of measurements: "))
numMeas = numMeas + 1 #number of measurements compensated for iteration behavior
for i in range(1, numMeas):
typeMeas = raw_input("Enter type of measurement "+str(i)+": ")
numErrors = int(raw_input("Enter number of error sources: "))
numErrors = numErrors + 1
inputData = []
for j in range(1, numErrors): #builds dataSet from number of errors
inputData.append(typeMeas)
description = raw_input("Enter source of uncertainty "+str(j)+": ")
inputData.append(description)
estUncert = raw_input("Enter estimated uncertainty "+str(j)+": ")
estUncert = float(estUncert)
inputData.append(str(estUncert))
cells = ''
for i in inputData:
cell = "<td>"+str(i)+"</td>"
cells += cell
table += "<tr>"+cells+"</tr>"+"\n"
print table