3,386 Posted Topics
Re: I suggest to read some beginners tutorial on functions. ![]() | |
Re: So you want example of accumulate pattern different from yours? You should accumulate string result and put space between words, because you want to return string. Normaly in Python we would use ' '.join(generate_the_words) but that is other, generator pattern. Looks like you could use little repetion of how functions … | |
Re: You would have your main program separated from utilities and have the root window defined there: [CODE]import tkinter as tk import button # no .py import entry root = tk.Tk() root.title("Import Test") e = entry.a(root) # need to define function b = button.b(root) # need to define function button.print_it() root.mainloop()[/CODE] | |
Re: Here for future version functioning version of Woooee's fast code using after-timing: [CODE]import time from Tkinter import * def change_color(canvas, ov, color): color = 'yellow' if (color != 'yellow') else 'blue' print "color =", color canvas.itemconfig(ov, fill=color) canvas.update_idletasks() canvas.after(1500, change_color, canvas, ov, color) root = Tk() root.title('Canvas') canvas = Canvas(root, … | |
Re: Value of name, Names, Workhours, Wages. EmpDOne and result of last line is not used. Capitalized name is supposed to mean object value by convention. | |
Re: Do global search and replace for listcomputers to self.listcomputers to save the value in the object. You could use lot of refactoring, I would for example store the [CODE]wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif')[/CODE] to one variabel to make it easier to change in future and to make … | |
Re: Don't use list as variable name. What is m, it is not defined before use at line 2? | |
Re: How about [code]seq[seq.index(first):].index(second)[/code] | |
Re: Line 44 sets variable to 1 and it will be incremented, that will only be made zero if it is allready zero (test in line 54, assignment line 57. Would this be what you are after little cleaner and closer to your comment? [CODE]import Tkinter from tkFileDialog import askopenfilename root … | |
Re: I think some student you know has studied Python programming, which is ideal for this. He surely want to show us his efforts of programming this :) | |
Re: Best algorithm for this is normally: [code]Select set of test cases While test cases do not pass Do next part of solution Solve test cases Debug While too slow See if you can use c-coded functions to produce same solutions for test cases. Profile [/code] | |
Re: addhex(hexdigit) = hextable[hexdigit], with hextable global variable. Change the name as it has nothing to do with addition. Input must be key in the table. | |
This handy function turns file into stream of words stripped of punctuation, whitespace and digits, but does not split for example we'd to two words. If you want that you can further process yielded words or change the definition. | |
Re: [CODE] total = float(numList[i]) total = total + numList[i] [/CODE] What are you trying to do, obviously [COLOR="Red"]numList [/COLOR]is a [B]strList[/B]. You are trying to double the value of numList[i]? why not [CODE]total = 2 * float(numList[i])[/CODE] | |
Re: You would run the test from [B]command window [/B]or you would [B]double click[/B] the file in graphical file manager. | |
I write usually many versions of the functions, sometimes writing functions from scratch again. Sometimes the parameters change from version to version, sometimes they are refering to library of functions that also has multiple versions. Has somebody advice on how to manage best this kind of situation? Should I define … | |
Re: Push numbers (or heapify inplace) in heap and use heapq.nlargest heapq.nsmallest | |
Re: For Python 3 solution this is simplest: from collections import Counter print(Counter(line.rstrip() for line in open('data.txt'))) | |
Re: For code clean up I would take also this block of code: [CODE] lst = [] for n in d: lst.append(n) lst.sort() [/CODE] Question to OP: how many times lst is sorted? I would think that instead of [CODE] for word in words: if word not in d: d[word] = … | |
Re: I would generate numbers with divmod to built in sum function. | |
Re: You should post runnable code of what you have. First the first program and remember to push **Code** before pasting. | |
Re: why you want to add new line to hours? | |
Re: What is your problem? What is purpose of overriding the file type? Why are you using count when you append to list all lines splitted? How is RoomTable initialized? You are passing file name, call the string file. You are using it as filename string and override built in type … | |
Re: Can you give description of your algorithm for red_pixel function? You should change the capital letter words to underscore words if you are allowed (PEP 8 conventions). What is your problem and tell how have you tried to debug it. Give also exact error messages. | |
Re: Please try not to give ready answers to homework questions and push Code button before pasting your code to message. | |
Re: There is an interesting alternative which you easily miss as possibility: [CODE]dictionary={'a':['A'], 'b':['B'], 'c':['C']} for k,v in dictionary.items(): [dictionary[k]] = v print dictionary [/CODE] And here is version I would use in my code: [CODE]dictionary={'a':['A'], 'b':['B'], 'c':['C']} dictionary = dict((k, v) for k,[v] in dictionary.items()) print dictionary [/CODE] And finally … | |
Re: t-one or t-el? Maybe there would exist clearer variable names? By the way have you learned about list comprehensions? | |
Re: [CODE]def index(filename, words): infile = open(filename) content = infile.readlines() infile.close() count = {} for word in words: if word in count: count[word] += 1 else: count[word] = 1 for word in count: print('{:12}{},'.format(word, count[word])) [/CODE] | |
Re: Beauty is in the eye of beholder, I have written super simple pretty printer as I did not like the result of pprint module for shallow lists. I could think that object with pretty format template attribute settable from user would not be bad thing, just remember to keep repr … | |
Re: You are comparing the generator n to integer instead of reading values from it, I do not understand. Mitä häh? | |
Re: Find the mathematical base for what you want to do and find what is this function named mystery here in standard library (the import of module left out here): from ____ import ____ as mystery x=[['a', 'b', 'c', 'd'], ['e', 'f'], ['g', 'h']] for sub in x: print list(mystery(sub,2)) print … | |
Re: Cleaner map solution as lambda is not needed: [code]a = map(int, a)[/code] | |
Re: Don't need to .split('-') but you can check directly begining by startswith. | |
Re: There is many ways to reprecent group of information. Basic choice is list for changing data or tuple for rarely changing data. Tuple is faster but any change to it generates new tuple as it is immutable. Please go through the tutorials section on types. | |
Re: I understand that as C for example is compiled the Python program is allowed to be 10 times slower. Maybe not so bad as you can prepare normally running program in 1/8 of the time and rest of time you can optimize. | |
Re: [QUOTE=griswolf;1368361]lets start easy: [CODE]lines = [] with open('test.txt', 'r') as f: for x in f: if x.strip() # lose empty lines lines.append(x.strip()) for line in lines: print(line) [/CODE]This just eliminates the blank lines, then prints out the remainder. Of course you will want to do some more work. You will … | |
Re: subtle issue: it is better not to build the list if it is not saved for future, join is happy with generator: [CODE]import re # use regex package pingPatternRe = re.compile('=([0-9.]+) ms') # compile the pattern I want to recognize with open('randomcharacters.txt') as f: # open the file so as … | |
Re: indention of lin 4 is of and are you realy looping in steps of 120? | |
Re: Or you can do: [CODE]print hangword.count(guess) [/CODE] or [CODE]>>> hangword ='Tony Veijalainen' >>> hangword = hangword.lower() >>> guesses ='a' >>> print ' '.join((letter if letter in guesses else '_') for letter in hangword) _ _ _ _ _ _ _ _ _ a _ a _ _ _ _ >>> … | |
Re: Here is my solution of finding right place guesses and wrong place guesses, result contains '*' for correct place letters and '-' for wrong place letters, other letters are from secret word: [CODE]def check(guess,word): result=['*' if c1==c2 else c2 for c1,c2 in zip(guess, word)] for index, char in enumerate(guess): if … | |
Re: I suggest reading this, I have it still it in my plan for reading as I have not yet dealt with the topic. [url]http://diveintopython.org/unit_testing/index.html[/url] | |
Re: jpeg quality setting is same in the files as checked in image editor like IrfranView? But, wait a moment the size looks right for me: 3 primary colors * 300 * 300 == 270 000 | |
Re: Additionally what happens when user inputs key 123456? Why you use global variables, not parameters. Also would be natural to return values. | |
Re: For me looks like line 4 should be [B]while[/B] not if, from the comment, so it can remove more than one trailing zero. | |
Re: [CODE]#===============E 3======================== # This allow any number you want def dvsble(x,y): for n in range(x,y): sum = 0 if n%17 ==0: x = n sum() print n else: n=n+1 print sum[/CODE] Some comments to help going forward: [LIST=1] [*]Are the parameter choice correct considering restrictions of this request [*]Line 5 … |
The End.