3,386 Posted Topics
Re: You better not mix up sleep with gui, tkinter has after events use them [url]http://www.daniweb.com/software-development/python/code/384025[/url] | |
Re: You are not reversing integer, you are processing a string, for integer use function divmod (or // and % operators). You do not call the palindrome_generator function, so nothing will happen. | |
Re: Experiment with side and expand parameters to pack and use help frames! You must assign frame first and pack it separately to use it for other widgets. [CODE]def showAbout(): t1 = Toplevel(root) t1.geometry("430x300") t1.title('About Lemniscate') f = Frame(t1) f.pack(side=LEFT, expand=False) Label(f, text='Draw Lemniscate Curve', font=("Arial", 14)).pack(pady=30, side=TOP) # Can you … | |
Re: You are not taking dictionary as input, this is the other direction? The dictionary has not order, so that does not matter. [CODE]>>> def print_dictionary(d): print '[%s]' % (', '.join('%s' % (0 if v not in d else d[v]) for v in range(min(d), max(d)+1))) >>> print_dictionary({0: 1, 2: 1, 4: … | |
Re: You did not post error message or something like it. Looks like you are repeating almost same code, use functions. Also why you ask hours if salary is not per hour? | |
Re: If you insist on doing it yourself, the method is start[B]s[/B]with, not startwith: [CODE]data = open('contacts.vcf', 'r') name = '' number = '' if data: for line in data: if line.startswith('N;'): name = line.split(':')[1].strip(' ;:\n') if name and line.startswith('TEL'): number = line.split(':')[1].rstrip() print "%s: %s"%(name, number) name = number = … | |
![]() | Re: Here alternative without iterator, which leaves extra digits: [CODE]def add_dash(control, to_add): as_list = list(to_add) p = 0 while '-' in control[p:]: p = control.find('-', p) + 1 as_list.insert(p - 1, '-') return ''.join(as_list) s1 = "75465-54224-4" s2 = "245366556346" print add_dash(s1, s2) """my output --> 24536-65563-46 """ [/CODE] |
Re: Iterate file line by line and reverse each before printing. | |
Re: You should consider that pattern could be split on consecutive lines. [CODE]with open('seq.txt') as seqfile: tag = 'GAAT' seqs = [(s.split('\n', 1)[0], ''.join(s.split('\n', 1)[1].split()).count(tag)) for s in seqfile.read().split('>') if s] for s, count in seqs: print s, ':', count [/CODE] | |
Re: Use the split method with second argument 1 or the partition method. | |
Re: Post your code and explain your problem and what you have done to solve it. Do not forget code-tags. | |
![]() | Re: Explain what you are trying to do, and post whole relevant function. Probably you have wrong algorithm for the job. Also maybe you should be using xrange if the length of the range is big and you use Python 2. In my computer struct.pack() looks like taking around 0.88 microseconds. |
Re: If you have computer with Python you can have it show the binary and decimal versions easily: [CODE=python] Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> for a in 0x000, 0x010, 0x011, 0x040, 0x0C1, 0x200, … | |
Re: Then generate primes, produce squares of them and then check through squared_primes in loop if number - squared_prime is in squared_primes. | |
Re: [CODE]def splitWord(word, length): r = [] for count in range(0, len(word), length): r.append(word[count:count+length]) return r [/CODE] Your capitalization is idiosyncratic (probably due the site not following the [URL="http://www.python.org/dev/peps/pep-0008/"]PEP8[/URL] | |
Re: else part of try is for the case of no exception happened, there is exit function and same you can do also by raising SystemExit, using function like above is also good idea to organize code. You can raise exception in function and handle it in calling function if you … | |
![]() | Re: Of course then [CODE]>>> a[::-1] < b[::-1] True >>> [/CODE] But it is questionable if this is faster than simply unpacking them before compare: [CODE]>>> def less_packed(a,b): return unpack('<I', a) < unpack('<I', b) >>> less_packed(a,b) True [/CODE] |
Re: You have completely misindented the code and you are using input for inputting string, also all those str functions look ugly, take a look of [B]% [/B]string formatting or [B]format [/B]method for neat printing. | |
| |
Re: This does not sound to have any use but here my puny efforts: [CODE]import re st="big brown fox jumps over the lazy dog. fox chases mice, dog chases fox" print re.findall('fox', st), re.split('fox', st) #more usefull print re.findall(r'[^.,!?]+[.,!?]', st) #in Python print 'fox' in st, st.count('fox'), st.split('fox') [/CODE] Output [CODE]['fox', … | |
Re: [CODE]>>> def split_hex(n): print '%02x %02x' % divmod(n, 256) print '%c%c' % divmod(n,256) return '%c%c' % divmod(n,256) >>> split_hex(2012) 07 dc Ü '\x07\xdc' >>> [/CODE] | |
Re: Efficient way is to use min and sum on list input, at least it is generalizable. On the other hand the input is so simple, that it is possible to easily spell it out, so your solution is a valid one also. Using general function is much more complicated to … | |
Re: There is built in find method, which I think you should implement yourself as exercise. Use loop to check for every possible starting place. Initialize the result variable to -1. | |
Re: You can use modulo operator to find the last digit. | |
Re: Actually even more so: [CODE]def gen_numbers(num): return list(range(num+1))[/CODE] | |
![]() | Re: sort(mylist) if you want raising order, otherwise you should user reverse parameter. |
Re: [CODE]def isIsosceles(x, y, z): return x == y or y == z[/CODE] | |
Re: Just reset flag after writing the line out, alternatively: [CODE]save = '' with open('test.txt') as f, open('outfile.txt', 'w') as f_out: for line in f: if 'line 2' in save: f_out.write(line) save = line '''Output--> line 3 ''' [/CODE] | |
Re: [CODE]def Cel2Fah(celcius): return '%.2f' % (celcius * 9. / 5 + 32)[/CODE] | |
Re: Maybe you should use interactive prompt to get feed back of your syntax. Even you had two different ways in your arrays, both the prompt did not accept. This seemed to work: [CODE]> x <- c(10.4, 5.6, 3.1, 6.4, 21.7) > y <- c(12,5.6, 7.2, 1.0, 9.3) > plot(x,y) >[/CODE] | |
Inspired by discussion in [url]http://www.daniweb.com/tutorials/tutorial238544.html[/url] (test cases are from there) I used my magic pattern matching function transformed from numbers and adding the parts length checking loop. I like my own solution better, I do not know about you. | |
Re: I did not get same error, but you did not decode bytes. [CODE]import urllib.request import sys import webbrowser #'http://www.imdb.com/list/SuSZdwCyHzU/' def savePage(urll,filename): page=urllib.request.urlopen(urll).read() print(page) sys.argv[0]=filename with open(sys.argv[0],'w') as outfile: outfile.write(page.decode('utf8')) savePage('http://www.daniweb.com/software-development/python/threads/406620/1735920#post1735920','outIMDB.html') webbrowser.open('outIMDB.html') [/CODE] | |
Re: It is quite unclear which is the 'five lines'. You could reduce this terrible code bloat ;) by using the short function exception from PEP8 and put __init__ and __get__ definitions in single line also the example could use tertiary if handling the one word difference in printed strings. Cheers, … | |
Re: OK, take a look at [url]http://www.daniweb.com/software-development/python/threads/32007/1122938#post1122938[/url] (it has some space for improvement as code is quite old, but should do as example). | |
Re: What have you tried looks simple match betseen 'start and end tags'? | |
Re: Basic math is of course simple, but got myself mystified sometimes with exponentiation (shame on me, and I got stipend of best mathematical science student in my graduation year 1984 in my town): -1**0.5 is valid formula (-1^0.5 some languages). | |
Re: You have brittle code by using the input for numbers, so your program crashes if input letters or empty line. You are calling menu instead of returning from submenu, so when you quit after from main menu, you will end up in submenu. Good design of submenus is to have … | |
Re: Seconds resolution is too coarse for binary search, move to milliseconds: [CODE]def main(): lst = [] result, length = 0, 500000 while result <= 0.50: for i in range(length): lst.append(randint(1, length)) value = randint(1, length) t0 = time.clock() linearSearch(value, lst) result = (time.clock() - t0) * 1000 print("This LINEAR SEARCH … | |
While looking into [URL="http://www.had2know.com/academics/gaussian-prime-factorization-calculator.html?blankone=2&result="]Gaussian primes [/URL]I came to think additional ways to check for is number integer or not, what you think is best way? [CODE]epsilon = 1e-11 def is_integer(n): """ traditional abs, epsilon check for near integer """ return abs(n-round(n)) < epsilon def is_integer2(n): return (float(n)- int(n)).as_integer_ratio()[1] == 1 … | |
Re: You are typing your function to new window in IDLE, by Ctrl-N or File - New Window aren't you? Then you can just choose to run the file with F5 or choose Run - Run Module from menu. | |
Re: You could compare [URL="http://www.daniweb.com/software-development/python/code/371844"]with mine[/URL], use numpy or shedskin like me and/or do profiling | |
Re: You do not need to test all numbers neither test for prime as factorin wjile loop ensures prime. You can test for only valid prime candidates n * 6 +- 1 after testing with 2 and 3. BTW this is old solved thread and I do not know how much … | |
Re: You are missing the comma between values. | |
Re: The result should include sublists of length k which do not contain first element and lists of first element followed by all sublists from other elements of length k - 1, when length of argument is more than k. | |
Re: Use formatting string with % or newer .format string method. | |
Re: Do you have possibility to upgrade? Admin rights and not any incompatible modules you use? For example if you use psyco, you need to keep to Python 2.6, but I think most things should at least work with that. Latest Python 2 is Python 2.7.2. | |
Re: If you want to prepare package called MyModules, and the parent directory is in your PATH or PYTHONPATH, then you should create an empty file called __init___.py in the directory and import the module as MyModules.polymod. If you want to import all your modules from this directory and they are … | |
Re: Your style is bit esoteric, especially using multiline single quotes everywhere, here is maybe little more conventional way: [CODE]def grid(x, y): first = (3* ' ' + ''.join('%c'.center(7) % (ord('A') + n) for n in range(x)) + '\n ' + (6 * '_') * x + '\n') rest = (3 … |
The End.