Note that there are some explanatory texts on larger screens.

plurals
  1. POtables in python - having some errors - not sure why
    primarykey
    data
    text
    <p>I have some questions about "technical" and basic functions in python.</p> <p>I have a table like this:</p> <pre><code>Name,Gender,Age,Salary,Height Menny, M, 1, 1, 1 James, J, 2, 2, 2 Sami, S, 3, 3, 3 class Table: def __init__(self,path,sep): try: f = open(path, "r") read_file = f.read() f.close() except: print "cannot create a table from this file" return table = read_file.split("\n") for i in range (len(table)): table[i] = table[i].split(sep) if len(table) &gt; 0: for i in range(len(table[0])): if table[0][i] in table[0][0:i]: raise ValueError row_names = [] for i in range(1,len(table)): if len(table[i]) != len(table[0]): raise ValueError if table[i][0] in row_names: raise ValueError row_names.append(table[i][0]) </code></pre> <p>Now I want to use functions: 1. to know how many cells there are. here I have 12 cells. The height of the table is len(table). Then the width is len(table[0]). The number of cells is height*width. so:</p> <pre><code> def len(self): height = len(table) width = len(table[0]) return height * width </code></pre> <p>and if I tried this:</p> <pre><code>def len(self): len(self.nestedList)*len(self.nestedList[0]) </code></pre> <p>I get "None"</p> <ol> <li><p>If in the shell I write the name Menny, Sami etc, then print the rest of the line (age, salary etc).... So I thought about it:</p> <pre><code>def the_row (self, rowname): rows_checking = [] for i in range(1, len(table)): rows_checking.append(table[i]) if rowname in rows_checking: table[i].remove(table[0:0]) return table[i] </code></pre> <ol> <li>almost the same thing like in the second task, but this time the function will print the value that is common to 2 thing. For example - the code will print "1" if I write Menny and Age. Again, I think I'll do it almost the same as I did in the pre task, but this time:</li> </ol> <p>get_the_value(self,rowname,colname)</p></li> </ol> <p>So far seems to be good ideas, I hope so... but I get errors:</p> <pre><code>AttributeError: Table instance has no attribute '__len__' </code></pre> <p>or</p> <pre><code>AttributeError: Table instance has no attribute 'len' </code></pre> <p>Probably because I didn't used "self" here, right? So what I can do? You don't have to feed me by the spoon and tell me the codes as it should be, but just give me advices as possible as you can, please. </p> <p>edited code:</p> <pre><code>class Table: def __init__(self,path,sep): self.path=path self.sep=sep self.g=[] self.count=0 self.headlines=[] self.matrix=[] self.headrows=[] self.postionrow=0 self.postioncolmn=0 try: f=open(self.path,'r') read_file=f.read() split_file=read_file.split() for line in split_file: list_the_line=line.split(self.sep) self.g.append(list_the_line) self.count=0 for z in range (len(self.g[0])): self.count=0 for d in range(len(self.g[0])): if self.g[0][z]==self.g[0][d]: self.count+=1 if self.count&gt;=2: raise ValueError num_first_line=len(self.g[0]) for k in range (len(self.g)): if len(self.g[k])!= num_first_line: raise ValueError self.headlines=self.g[0] self.g.remove(self.g[0]) self.count=0 for row_name1 in range (len(self.g)): self.count=0 for row_name2 in range(len(self.g)): if self.g[row_name1][0]==self.g[row_name2][0]: self.count+=1 if self.count&gt;=2: raise ValueError for i in range (len(self.g)): self.headrows.append(self.g[i][0]) self.g[i].remove(self.g[i][0]) ezer=[] for op in range (len(self.g)): ezer=[] for od in range (len(self.g[0])): ezer.append(self.g[od][op]) self.matrix.append(ezer) f.close() except : print "cannot creat a table object from this file" return def len(self): num_rows=len(self.g) num_cols=len(self.g[0]) return num_rows*num_cols def get_row(self,rowname): for i in range (len(self.headlines)): if rowname==self.headrows[i]: self.postionrow=i return self.g[i] if not rowname in self.headrows : raise ValueError def get_column(self,colname): for i in range (len(self.headlines)): if colname==self.headlines[i]: self.postioncolmn=i-1 return self.matrix[i-1] if not colname in self.headlines : raise ValueError def get_value(self,rowname,colname): self.get_row(rowname) self.get_column(colname) if not rowname in self.headrows : raise ValueError if not colname in self.headlines : raise ValueError return self.g[self.postionrow][self.postioncolmn] def get_row_name_with_max_value(self,colname): if not colname in self.headlines : raise ValueError max_colmn=max(self.get_column(colname)) for i in range (len(self.matrix)): if max_colmn == self.g[i][self.postioncolmn]: return self.headrows[i] </code></pre> <p>and what should be the result:</p> <pre><code>&gt;&gt;&gt; table = Table("table_examp1111111","\t") cannot create a table from this file &gt;&gt;&gt; table = Table("table_example1.txt","\t") &gt;&gt;&gt; print table.len() 12 &gt;&gt;&gt; print table.get_row("Menny") ['M', '1', '1', '1'] &gt;&gt;&gt; print table.get_column("Height") ['1', '2', '3'] &gt;&gt;&gt; print table.get_value("Sami","Age") 3 &gt;&gt;&gt; print table.get_row_name_with_max_value("Height") Sami &gt;&gt;&gt; print table.get_row_name_with_max_value("Salary") Sami </code></pre> <p><strong>This code works but I want to make it more pythonic. Please don't change the form, don't add or remove function just fix my syntex.</strong> Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload