|
Scaevolus posted:Is this a (known?) bug? I don't know or have any insight...but I would assume a tuple holds references to objects. You can't change the references, but if you can change the pointed-to objects, there's nothing stopping you.
|
# ? Mar 28, 2011 22:16 |
|
|
# ? Jun 2, 2024 16:07 |
|
Can anyone tell me what's going on with the last line in this function? I'm only really interested in the last line.pre:def multiplicationTable(n): lim=n x=1 y=0 table=[] for m in range(1,lim+1): y+=1 x=y for n in range(1,lim+1): table.append(x) x+=y return [table [i::lim] for i in range(lim)] Here's the final value of table for multiplicationTable(4) pre:[1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16] pre:[[1, 2, 3, 4], [2, 4, 6, 8], [3, 6, 9, 12], [4, 8, 12, 16]]
|
# ? Mar 28, 2011 22:31 |
|
Scaevolus posted:Is this a (known?) bug? It was filed in the CPython bugtracker yes. It's basically unfixable though, short of implementing STM in Python.
|
# ? Mar 28, 2011 23:59 |
|
baku posted:Can anyone tell me what's going on with the last line in this function? I'm only really interested in the last line. code:
code:
sequence[ start_index : stop_index : step_size ] Except you can omit any of the terms there and they get filled to a default (default start is zero, default end is the length of the sequence, default step size is one element at a time). In this case its slicing from i to the end of the sequence, stepping lim units at a time.
|
# ? Mar 29, 2011 01:17 |
|
okay I get it I was thinking 0 in range(lim) somehow corresponded to the nonexistent 0 element in my list, instead of being the index 0.
|
# ? Mar 29, 2011 01:44 |
|
As mentioned above, I'm new to programming and just doing this as a hobby. Here is my problem: I have a text file with about 1200 lines of garbage characters, with random letters spread throughout it. I want to read in a line, take any letters and put them in a string and then toss the rest of the trash characters, then move onto the next line. I've found a way to do that for a text file with one or two lines but I don't think its efficient and 1200 lines is a lot to read into memory. Any advice? *This is my first real experience with learning read/write *Ok, I'm making progress but I need to write a loop that will continue to read each line and do something with that line, until there are no more lines. What is a good way to write a loop to break when it reaches the end of the text file? Hughmoris fucked around with this message at 03:05 on Mar 29, 2011 |
# ? Mar 29, 2011 02:48 |
|
Hughmoris posted:Ok, I'm making progress but I need to write a loop that will continue to read each line and do something with that line, until there are no more lines. What is a good way to write a loop to break when it reaches the end of the text file? Python makes this very easy for you: code:
|
# ? Mar 29, 2011 03:06 |
|
Thanks for the reply. Here is what I settled on:code:
|
# ? Mar 29, 2011 04:04 |
|
Hughmoris posted:Thanks for the reply. Here is what I settled on: You don't want to read the file that way. You're reading the whole file into memory at once. What if the size of the file is greater than the amount of free memory? The example you were given doesn't have that problem. Read this for more info: http://docs.python.org/tutorial/inputoutput.html
|
# ? Mar 29, 2011 04:56 |
|
For you folks using or interested in using Python for scientific computing. A great lecture by Fernando Perez on Scientific Computing Using Python. Covers NumPy, SciPy, SAGE, matplotlib, Mayavi, and more: http://www.youtube.com/watch?v=1j_HxD4iLn8
|
# ? Mar 29, 2011 09:40 |
|
Hughmoris posted:
The point that you should avoid reading the entire file into memory has been made several times. I would like to point out that python allows you the following syntactical nicety: code:
code:
code:
|
# ? Mar 29, 2011 14:21 |
|
Python also lets you just docode:
|
# ? Mar 29, 2011 14:29 |
|
here is another question. i wrote this function to remove negative numbers from a list.pre:def removeneg(L): for i in L: if i<0: L.remove(i) print L [10, -2, 11, -9, -7, 0, 1, -12, 13] it returns [10, 11, -7, 0, 1, 13] why doesn't it remove the -7?
|
# ? Mar 29, 2011 18:25 |
|
You're mutating the list that you're iterating over, which plays merry hell with your for loop.
Haystack fucked around with this message at 23:00 on Mar 29, 2011 |
# ? Mar 29, 2011 18:29 |
|
This is where list comprehensions or filter() will be useful.
|
# ? Mar 29, 2011 18:30 |
|
It seems like if you have two negatives in a row, it removes the first and keeps the second one. For example if you feed your function the list [-1,-2,-3] it returns [-2]. My guess is that when Python removes one number, it moves the next one into the deleted number's position in the list. When the loop continues, it appears to "skip over" one number, although it's just that the number you wanted is now at an index that the loop has already passed through. (or something like that, I don't know the right technical terms for what I'm trying to say)
|
# ? Mar 29, 2011 18:41 |
|
Sneftel posted:This is where list comprehensions or filter() will be useful. code:
|
# ? Mar 29, 2011 18:55 |
|
The lazy way would be to run that loop until it finds no negatives, but that's not very clean or elegant or sane. E: Or construct a new list.
|
# ? Mar 29, 2011 19:04 |
|
Haystack posted:You're mutating a the list that you're iterating over, which plays merry hell with your for loop. I see. I tried changing the IF to WHILE but that just created an error. MaberMK posted:
Thanks, that's much better than whatever I was trying to do. I guess I still need to get the hang of assigning loops to variables like that. code:
|
# ? Mar 29, 2011 19:27 |
|
baku posted:
Note that this doesn't actually modify L, or even return the modified list. If you're actually using that as written, its name is somewhat misleading.
|
# ? Mar 29, 2011 19:44 |
|
Sailor_Spoon posted:Note that this doesn't actually modify L, or even return the modified list. If you're actually using that as written, its name is somewhat misleading. You're right, I was just making a list. It's pretty pointless Rad ROM Max fucked around with this message at 19:59 on Mar 29, 2011 |
# ? Mar 29, 2011 19:53 |
|
Dren posted:The point that you should avoid reading the entire file into memory has been made several times. I would like to point out that python allows you the following syntactical nicety: Thanks for this.
|
# ? Mar 29, 2011 20:04 |
|
How do you use linecache? I get the following error with it: edit: n/m, I realized I was giving it a file instead of a file name. duh. FoiledAgain fucked around with this message at 22:39 on Mar 29, 2011 |
# ? Mar 29, 2011 22:24 |
|
king_kilr posted:Fun times: This basically works because code:
code:
|
# ? Mar 29, 2011 22:25 |
|
Is there a way to run a python program in some sort of debugger step by step to see what exactly is executing?
|
# ? Mar 29, 2011 23:34 |
|
Gothmog1065 posted:Is there a way to run a python program in some sort of debugger step by step to see what exactly is executing? If you're still using IDLE, it has a nice little built-in debugger in the Debug menu.
|
# ? Mar 29, 2011 23:58 |
|
How do you check for another version of python on your computer? Add/remove program only shows 3.2, and I only see the folder for 3.2 on the root directory. Here's what's going on: pre:#Hangman Word Game import random HANGMAN = ( #The Hanged Man. """ <<insert 8 hangman pictures here as a tuple>> """) WRONGLIMIT = len(HANGMAN) WORDS = ("OVERUSED", "CLAM", "GUAM", "TAFFETA", "PYTHON", "COMPUTER") print(""" Welcome to the Hangman Game! You have""", WRONGLIMIT, """tries to guess the letters in the word! Please only use letters in your guesses! """) while True: word = random.choice(WORDS) tries = 0 guess = input("Please give your first guess: ") guess = guess.upper() used = [] guessedWord = "-" * len(word) while tries < WRONGLIMIT and guessedWord != word: while guess in used: print("You have already used the letter", guess) guess = input("Please enter another letter: ") guess = guess.upper() used.append(guess) if guess in word: print("Yes,", guess," is in the word!") new = "" for i in range(len(word)): if guess == word[i]: new += guess else: new += guessedWord[i] guessedWord = new print("Please enter a new letter (Current Word:", guessedWord,"): ") guess = input("") guess = guess.upper() else: print("That is an incorrect guess!\n") print(HANGMAN[tries]) print("\n\nThe word so far:", guessedWord, "\nYou have used" \ " these letters so far:\n", used) tries += 1 if guessedWord == word: print("Congratulations! You have guessed the word, ", word, "!") else: print("You were hanged and lose! The word was", word, "\n") cont = input("Would you like to contune? Y/N:") if cont.lower() == "n" or cont.lower() == "no": break pre:Welcome to the Hangman Game! You have 8 tries to guess the letters in the word! Please only use letters in your guesses! Please give your first guess: h That is an incorrect guess! ------ | | | | | | | | | ---------- The word so far: -------- You have used these letters so far: ['H\r'] You have already used the letter H Please enter another letter:
|
# ? Mar 30, 2011 00:48 |
|
Gothmog1065 posted:['H\r'] Looks like it's a known bug in python 3.2: http://bugs.python.org/issue11272 In the meantime you can clean up the guess on your own: code:
Gothmog1065 posted:Is there a way to run a python program in some sort of debugger step by step to see what exactly is executing? Have you looked at pdb? Put this near where you want to break into debug mode: code:
|
# ? Mar 30, 2011 09:44 |
|
fischtick posted:Looks like it's a known bug in python 3.2: http://bugs.python.org/issue11272 quote:In the meantime you can clean up the guess on your own: quote:Have you looked at pdb? Put this near where you want to break into debug mode:
|
# ? Mar 30, 2011 13:13 |
|
I know a lot of folks don't like it, but I've always been a fan of Pydev's debugger.
|
# ? Mar 30, 2011 15:29 |
|
VirtuaSpy posted:For you folks using or interested in using Python for scientific computing.
|
# ? Mar 30, 2011 16:08 |
|
I'm new to Python, and I'm trying to use pyserial to get data from a serial port and display it on a realtime graph. The graph works fine, but I'm having trouble with this code I tried adjusting for my purposes. It's supposed to read numeric data from the serial port and save it, but I can't even start it. The only thing that happens is that it writes "100" repeated 500 times. Here is the code: code:
|
# ? Mar 30, 2011 17:48 |
|
Sounds like it's just runningcode:
|
# ? Mar 30, 2011 17:56 |
|
BeefofAges posted:Sounds like it's just running the port I'm actually trying to access is code:
|
# ? Mar 30, 2011 18:06 |
|
I'm using Pylab to draw some figures, and I'm having trouble, mostly because I hardly ever deal with graphics and I don't really understand everything that's going on. The y-axis goes from 0-2500 for each of my plots, but it gets squashed to fit within a screen and I can't see the details very well. How do I extend the size of the plot/change the scaling on the y-axis? This is probably really easy, and I tried reading though documentation, but as I said I'm not familiar with pylab or graphic at all and I'm quickly lost. This is the relevant code I'm working with: code:
vvvv Thanks! That's perfect. FoiledAgain fucked around with this message at 20:29 on Mar 30, 2011 |
# ? Mar 30, 2011 20:05 |
|
FoiledAgain posted:I'm using Pylab to draw some figures, and I'm having trouble, mostly because I hardly ever deal with graphics and I don't really understand everything that's going on. pylab.ylim
|
# ? Mar 30, 2011 20:17 |
|
Python projects go in this thread right? For the last few months I've been busy porting Docvert from PHP to Python and what the software does is convert Office files to Docbook and clean HTML, lists are made hierarchical , any vector diagrams are converted to SVG/PNG, and so on. It needs pyuno/libreoffice but the conversion from OpenDocument to DocBook and HTML is done in Docvert.
N.Z.'s Champion fucked around with this message at 23:48 on Mar 30, 2011 |
# ? Mar 30, 2011 23:46 |
|
Ok, I am stumped. I want this function to take a value, search through a text file of dictionary words and tell me if the value is a word.code:
it spits out that it is a word. What am I missing? Here is the list of words I use: http://download.oracle.com/javase/tutorial/collections/interfaces/examples/dictionary.txt *I think I might have stumbled upon the issue. 'oklo' is in the word 'booklore', so maybe it registers that. Is there a way to make it match word specifically and not register false words like that? Hughmoris fucked around with this message at 05:02 on Mar 31, 2011 |
# ? Mar 31, 2011 04:57 |
|
Hughmoris posted:*I think I might have stumbled upon the issue. 'oklo' is in the word 'booklore', so maybe it registers that. Is there a way to make it match word specifically and not register false words like that? Since each line is only a single word, you could change your if statement to "if word == line". You could also try something more complicated with regular expressions.
|
# ? Mar 31, 2011 05:41 |
|
|
# ? Jun 2, 2024 16:07 |
|
FoiledAgain posted:Since each line is only a single word, you could change your if statement to "if word == line". You could also try something more complicated with regular expressions. Hmm. I tried that but it doesn't want to work. Nothing registers as a word. I've looked at regular expressions but it confuses the hell out of me.
|
# ? Mar 31, 2011 05:49 |