12 Replies - 584 Views - Last Post: 13 October 2011 - 10:17 AM Rate Topic: -----

#1 labradorguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Make Matrix with values

Posted 12 October 2011 - 03:29 PM

2-d matrix whose axes are labeled with sorted lists of non-whitespace characters and the number of occurrences of the digram xy are M[x][y] in the file

EX: abababababaaaa

would out put

       a   b
  a   3   5
  b   5   0




this is what I have so far, I need to use lists not strings that is my first problem with my code, and help would be appreciated

import sys

if len(sys.argv) != 2:
	print "usage: python", sys.argv[0], "filename"
	sys.exit(1)
		
f = open(sys.argv[1], "r")
line = f.read()
f.close()

char = ""	
count = 0
letters = []
i = 0

for x in line.rstrip():
	if (x in letters):
		count = count
	else:
		count += 1
		letters.append(x)
	

print  count, " ", letters 


This post has been edited by labradorguy: 12 October 2011 - 03:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Make Matrix with values

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 3911
  • View blog
  • Posts: 11,448
  • Joined: 18-April 07

Re: Make Matrix with values

Posted 12 October 2011 - 03:50 PM

Well the main trick here is to first setup your grid. I would start this by first creating two lists of letters. Then in a nested loop you can pull one letter from one list, pull from another list, concatenate them together and use string.count(concatenated_string) to then find occurrences. Once you have the number, you will already be in the loop at the correct index to add it to the list of the grid.


Edit: Just so you know things like count = count really doesn't mean a darn thing.

:)

This post has been edited by Martyr2: 12 October 2011 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 labradorguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Re: Make Matrix with values

Posted 12 October 2011 - 03:55 PM

ok I think I got it but how would I remove ALL whitespace characters from a string without using the re.sub() because we haven't l;earned this yet
Thank you
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 3911
  • View blog
  • Posts: 11,448
  • Joined: 18-April 07

Re: Make Matrix with values

Posted 12 October 2011 - 03:58 PM

String.replace()? ;)
Was This Post Helpful? 0
  • +
  • -

#5 labradorguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Re: Make Matrix with values

Posted 12 October 2011 - 04:36 PM

that will only replace's the space's

EX: line = "I am \tjack\n"

line.replace(" ","") = Iam\tjack\n

I need to get rid of the \t and \n also
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 315
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Make Matrix with values

Posted 12 October 2011 - 04:41 PM

Using string.replace() only? Do it more than once and chain them?

string = string.replace("\n", "").replace("\t", "").replace(" ", "")


string.replace() returns a string, so you can do it as many times as you want :)

This post has been edited by Simown: 12 October 2011 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 3911
  • View blog
  • Posts: 11,448
  • Joined: 18-April 07

Re: Make Matrix with values

Posted 12 October 2011 - 04:41 PM

You could split it and rejoin it....

''.join("I am \tjack\n".split())



Another little trick. :)
Was This Post Helpful? 1
  • +
  • -

#8 labradorguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Re: Make Matrix with values

Posted 12 October 2011 - 06:18 PM

This is what I got now, Im not sure how to make a 2-d matrix

import sys

if len(sys.argv) != 2:
	print "usage: python", sys.argv[0], "filename"
	sys.exit(1)
		
f = open(sys.argv[1], "r")
line = f.read()
f.close()

char = ""	
count = 0
letters = []
i = 0

line = " ".join(line.split())
line = line.replace(" ","")
line = line.strip()

for x in line:
	if (x in letters):
		count = count
	else:
		count += 1
		letters.append(x)
letters.sort()	

print  count, " ", letters 


Was This Post Helpful? 0
  • +
  • -

#9 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 3911
  • View blog
  • Posts: 11,448
  • Joined: 18-April 07

Re: Make Matrix with values

Posted 12 October 2011 - 06:36 PM

Again the line count = count is useless.

Secondly, a 2d matrix is a list of lists...

matrix = [[1,2,3],[4,5,6],[7,8,9]]



The above is a 3 x 3 matrix...

[1,2,3]
[4,5,6]
[7,8,9]

That is, a list of three lists with three elements each. :)
Was This Post Helpful? 0
  • +
  • -

#10 labradorguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Re: Make Matrix with values

Posted 12 October 2011 - 07:05 PM

import sys

if len(sys.argv) != 2:
	print "usage: python", sys.argv[0], "filename"
	sys.exit(1)
		
f = open(sys.argv[1], "r")
line = f.read()
f.close()

char = ""	
count = 0
letters = []
letters2 = []
data = []
M = [[]]

line = " ".join(line.split())
line = line.replace(" ","")
line = line.strip()

data = line.replace(""," ").split()


for x in line:
	if (x not in letters):
		count += 1
		letters.append(x)
		
letters.sort()	
letters2 = letters


print  count, " ", letters , " ", letters2, " ", data, " "



This is what I got so far, but I don't know how to compare the variables and put them in a nested loop
Was This Post Helpful? 0
  • +
  • -

#11 labradorguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Re: Make Matrix with values

Posted 13 October 2011 - 09:21 AM

I am completely stuck with my last bit of code, Any help would be appreciated
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4948
  • View blog
  • Posts: 11,353
  • Joined: 16-October 07

Re: Make Matrix with values

Posted 13 October 2011 - 10:07 AM

Walk it through. Are all the pieces what you expect them to be?

import sys

if len(sys.argv) != 2:
	print "usage: python", sys.argv[0], "filename"
	sys.exit(1)

filename = sys.argv[1]
print "line=", filename

f = open(filename, "r")
line = f.read()
f.close()

print "line=", line

line = " ".join(line.split())
line = line.replace(" ","")
line = line.strip()

print "line=", line

data = line.replace(""," ").split()
print "data=", line



I can pretty much guarantee that line isn't what you expect...
Was This Post Helpful? 0
  • +
  • -

#13 labradorguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Re: Make Matrix with values

Posted 13 October 2011 - 10:17 AM

I just revised my code
import sys

if len(sys.argv) != 2:
	print "usage: python", sys.argv[0], "filename"
	sys.exit(1)
		
f = open(sys.argv[1], "r")
line = f.read()
f.close()
	
count = 0
letters = []
letters2 = []
data = []

#make line one line with no whitespace
line = line.split()



#put all charcters in a list called data
data = line.replace(""," ").split()

#put all unique letters in a list called letters and keep a count of how many unique letters
for x in line:
	if (x not in letters):
		count += 1
		letters.append(x)

M[]
for i in range(count):
	row[]
	for letters in rang(count):
		col[]
		row.append(letters)
	

#sort list of unique letters	
letters.sort()

#create second list to hold unique letters	
letters2 = letters


print  count, " ", letters , " ", letters2, " ", data, " ", M
		


You are right line was wrong before, I just need to split up the words and get digraph's from each word. I don't need to connect all the words in the file.
Also I need to make M better because
M[[0]*2]*2



is not right my prof said he told me how to do it and this is what I got, but it is not right any suggestions
M[]
for i in range(count):
	row[]
	for letters in rang(count):
		col[]
		row.append(letters)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1