Search Results

Search found 14486 results on 580 pages for 'python idle'.

Page 178/580 | < Previous Page | 174 175 176 177 178 179 180 181 182 183 184 185  | Next Page >

  • Python: Sort a dictionary by value

    - by the empirical programmer
    I have a dictionary of values read from 2 fields in a database: a string field and a numeric field. The string field is unique so that is the key of the dictionary. I can sort on the keys, but how can I sort based on the values? Note: I have read this post 72899 and probably could change my code to have a list of dictionaries but since I do not really need a list of dictionaries I wanted to know if there a simpler solution.

    Read the article

  • bulls and cows game -- programming algorithm(python)

    - by IcyFlame
    This is a simulation of the game Cows and Bulls with three digit numbers I am trying to get the number of cows and bulls between two numbers. One of which is generated by the computer and the other is guessed by the user. I have parsed the two numbers I have so that now I have two lists with three elements each and each element is one of the digits in the number. So: 237 will give the list [2,3,7]. And I make sure that the relative indices are maintained.the general pattern is:(hundreds, tens, units). And these two lists are stored in the two lists: machine and person. ALGORITHM 1 So, I wrote the following code, The most intuitive algorithm: cows and bulls are initialized to 0 before the start of this loop. for x in person: if x in machine: if machine.index(x) == person.index(x): bulls += 1 print x,' in correct place' else: print x,' in wrong place' cows += 1 And I started testing this with different type of numbers guessed by the computer. Quite randomly, I decided on 277. And I guessed 447. Here, I got the first clue that this algorithm may not work. I got 1 cow and 0 bulls. Whereas I should have got 1 bull and 1 cow. This is a table of outputs with the first algorithm: Guess Output Expected Output 447 0 bull, 1 cow 1 bull, 1 cow 477 2 bulls, 0 cows 2 bulls, 0 cows 777 0 bulls, 3 cows 2 bulls, 0 cows So obviously this algorithm was not working when there are repeated digits in the number randomly selected by the computer. I tried to understand why these errors are taking place, But I could not. I have tried a lot but I just could not see any mistake in the algorithm(probably because I wrote it!) ALGORITHM 2 On thinking about this for a few days I tried this: cows and bulls are initialized to 0 before the start of this loop. for x in range(3): for y in range(3): if x == y and machine[x] == person[y]: bulls += 1 if not (x == y) and machine[x] == person[y]: cows += 1 I was more hopeful about this one. But when I tested this, this is what I got: Guess Output Expected Output 447 1 bull, 1 cow 1 bull, 1 cow 477 2 bulls, 2 cows 2 bulls, 0 cows 777 2 bulls, 4 cows 2 bulls, 0 cows The mistake I am making is quite clear here, I understood that the numbers were being counted again and again. i.e.: 277 versus 477 When you count for bulls then the 2 bulls come up and thats alright. But when you count for cows: the 7 in 277 at units place is matched with the 7 in 477 in tens place and thus a cow is generated. the 7 in 277 at tens place is matched with the 7 in 477 in units place and thus a cow is generated.' Here the matching is exactly right as I have written the code as per that. But this is not what I want. And I have no idea whatsoever on what to do after this. Furthermore... I would like to stress that both the algorithms work perfectly, if there are no repeated digits in the number selected by the computer. Please help me with this issue. P.S.: I have been thinking about this for over a week, But I could not post a question earlier as my account was blocked(from asking questions) because I asked a foolish question. And did not delete it even though I got 2 downvotes immediately after posting the question.

    Read the article

  • Python 2.7 creating a multidimensional list

    - by poop
    I don't know why I am having so much trouble creating a 3 dimensional list. I need the program to create an empty n by n list. So for n = 4: x = [[[],[],[],[]],[[],[],[],[]],[[],[],[],[]],[[],[],[],[]]] I've tried using: y = [n*[n*[]]] y = [[[]]* n for i in range(n)] Which both appear to be creating copies of a reference. I've also tried naieve application of the list builder with little success: y = [[[]* n for i in range(n)]* n for i in range(n)] y = [[[]* n for i in range(1)]* n for i in range(n)] I've also tried building up the array iteratively using loops, with no success. In my rapid flurry of attempts to not post something stupidly easy to SO, I came upon a solution: y = [] for i in range(0,n): y.append([[]*n for i in range(n)]) Is there an easier/ more intuitive way of doing this?

    Read the article

  • Python how to convert this for loop into a while loop

    - by user1690198
    I have this for a for loop which I made I was wondering how I would write so it would work with a while loop. def scrollList(myList): negativeIndices=[] for i in range(0,len(myList)): if myList[i]<0: negativeIndices.append(i) return negativeIndices So far I have this def scrollList2(myList): negativeIndices=[] i= 0 length= len(myList) while i != length: if myList[i]<0: negativeIndices.append(i) i=i+1 return negativeIndices

    Read the article

  • python check value not in unicode list

    - by John
    Hi, I have a list and a value and want to check if the value is not in the list. list = [u'first record', u'second record'] value = 'first record' if value not in list: do something however this is not working and I think it has something to do with the list values having a u at the start, how can I fix this? And before someone suggests the list is returned from Django queryset so I can't just take the u out of the code :) Thanks

    Read the article

  • Invoking a function of library libfprint in Python

    - by Ivanelson
    I need help to call a function(Struct C) that detects the devices, so I'm calling the function this way: from ctypes import * fp = CDLL('./libfprint.so.0') fp.fp_discover_devs.argtypes = None fp.fp_discover_devs.restype = c_char_p ret = fp.fp_discover_devs() print ret # is "0" That is not detected any device, because the return is "0". See the documentation of the function: I'm using Ubuntu and I downloaded the "fprint_demo" and works perfectly. Did you install any package missing? Thanks.

    Read the article

  • good practice for string.partition in python

    - by user1544915
    some case i write code like these: a,temp,b = s.partition('-') i just need to pick the first and 3rd element. temp would never be used. is there a better way to do this? the common case is ,a better way to pick separted element to make a new list? for example i want to make a new list use old list 0,1,3,7 element code would be this: newlist = [oldlist[0],oldlist[1],oldlist[3],oldlist[7]] it's pretty ugly,isn't it?

    Read the article

  • Parsing groupings of strings (Python)

    - by j00niner
    I have a string that looks something like this: [["Name1","ID1","DDY1", "CALL1", "WHEN1"], ["Name2","ID2","DDY2", "CALL2", "WHEN2"],...]; This string was taking from a website. Their can be any amount of groupings. How could I parse this string and print just the Name variables of each grouping?

    Read the article

  • check if a process is running in python

    - by shash
    I am trying to find if the process is running based on process id. The code is as follows based on one of the post on the forum. I cannot consider process name as there are more than one process running with the same name. def findProcess( processId ): ps= subprocess.Popen("ps -ef | grep "+processId, shell=True, stdout=subprocess.PIPE) output = ps.stdout.read() ps.stdout.close() ps.wait() return output def isProcessRunning( processId): output = findProcess( processId ) if re.search(processId, output) is None: return true else: return False Output : 1111 72312 72311 0 0:00.00 ttys000 0:00.00 /bin/sh -c ps -ef | grep 71676 1111 72314 72312 0 0:00.00 ttys000 0:00.00 grep 71676 It always return true as it can find the process id in the output string. Any suggestions? Thanks for any help.

    Read the article

  • (Python) Extracting Text from Source Code?

    - by zhuyxn
    Currently have a large webpage whose source code is ~200,000 lines of almost all (if not all) HTML. More specifically, it is a webpage whose content is a few thousand blocks of paragraphs separated by line breaks (though a line break does not specifically mean there is a separation in content) My main objective is to extract text from the source code as if I were copying/pasting the webpage into a text editor. There is another parsing function I would like to use, which originally took in copied/pasted text rather than the source code. To do this, I'm currently using urllib2, and calling .get_text() in Beautiful Soup. The problem is, Beautiful Soup is leaving tremendous amounts of white space in my code, and it is difficult to pass the result into the second "text" parser. I have done quite a bit of research on parsing HTMLs, but I'm frankly not sure how to solve this problem easily. Furthermore, I'm a bit confused on how to use imports like lxml to extract text as if I were to simply copy and paste?

    Read the article

  • Python if statement not working as expected

    - by Chris Esposito
    I'm searching for a string in a website and checking to see if the location of this string is in the expected location. I know the string starts at the 182nd character, and if I print temp it will even tell me that it is 182, however, the if statement says 182 is not 182. Some code f = urllib.urlopen(link) #store page contents in 's' s = f.read() f.close() temp = s.find('lettersandnumbers') if (htmlsize == "197"): #if ((s.find('lettersandnumbers')) == "182"): if (temp=="182"): print "Glorious" doStuff() else: print "HTML not correct. Aborting." else: print htmlsize print "File size is incorrect. Aborting."

    Read the article

  • how to recieve mail using python>

    - by user600950
    Hey everyone, I am trying to make a program on my computer at home that will constantly check a certain gmail address. The purpose being the only email this adress recieves is from me. I would just like to be able to 1.Check for mail 2.Download mail(presumably to a string, though a file is acceptable) and 3. delete the mail from the web server but keep it on my computer. Thta is all i need to know right now, however my long term goal is to set up kind of a remote terminal over email, so that wherever i have email i have a certain amount of control over my computer.

    Read the article

  • A way to use Python which I don't know

    - by Konie
    In this quicksort function: def qsort2(list): if list == []: return [] else: pivot = list[0] # can't understand the following line lesser, equal, greater = partition(list[1:], [], [pivot], []) return qsort2(lesser) + equal + qsort2(greater) def partition(list, l, e, g): if list == []: return (l, e, g) else: head = list[0] if head < e[0]: return partition(list[1:], l + [head], e, g) elif head > e[0]: return partition(list[1:], l, e, g + [head]) else: return partition(list[1:], l, e + [head], g) I don't understand the sentence below the comment. Can someone tell me what is the meaning of this sentence here?

    Read the article

  • Python: Closing a for loop by reading stdout

    - by user1732102
    import os dictionaryfile = "/root/john.txt" pgpencryptedfile = "helloworld.txt.gpg" array = open(dictionaryfile).readlines() for x in array: x = x.rstrip('\n') newstring = "echo " + x + " | gpg --passphrase-fd 0 " + pgpencryptedfile os.popen(newstring) I need to create something inside the for loop that will read gpg's output. When gpg outputs this string gpg: WARNING: message was not integrity protected, I need the loop to close and print Success! How can I do this, and what is the reasoning behind it? Thanks Everyone!

    Read the article

  • Python: Determine whether list of lists contains a defined sequence

    - by duhaime
    I have a list of sublists, and I want to see if any of the integer values from the first sublist plus one are contained in the second sublist. For all such values, I want to see if that value plus one is contained in the third sublist, and so on, proceeding in this fashion across all sublists. If there is a way of proceeding in this fashion from the first sublist to the last sublist, I wish to return True; otherwise I wish to return False. In other words, for each value in sublist one, for each "step" in a "walk" across all sublists read left to right, if that value + n (where n = number of steps taken) is contained in the current sublist, the function should return True; otherwise it should return False. (Sorry for the clumsy phrasing--I'm not sure how to clean up my language without using many more words.) Here's what I wrote. a = [ [1,3],[2,4],[3,5],[6],[7] ] def find_list_traversing_walk(l): for i in l[0]: index_position = 0 first_pass = 1 walking_current_path = 1 while walking_current_path == 1: if first_pass == 1: first_pass = 0 walking_value = i if walking_value+1 in l[index_position + 1]: index_position += 1 walking_value += 1 if index_position+1 == len(l): print "There is a walk across the sublists for initial value ", walking_value - index_position return True else: walking_current_path = 0 return False print find_list_traversing_walk(a) My question is: Have I overlooked something simple here, or will this function return True for all true positives and False for all true negatives? Are there easier ways to accomplish the intended task? I would be grateful for any feedback others can offer!

    Read the article

  • Problem with a Python function

    - by the-ifl
    Well I have a little problem. I want to get the sum of all numbers below to 1000000, and who has 4 divisors... I try, but i have a problem because the GetTheSum(n) function always returns the number "6"... This is my Code : http://pastebin.com/bhiDb5fe

    Read the article

  • No such file or directory python in linux only (coming from windows)

    - by user1804633
    I have the same exact directory structure within a folder in Windows & in Linux (Debian) - where the script is along the static + dataoutput folders How come the following code works fine in Windows, but gives a no such file or directory path error in linux? @app.route('/_getdataoutputfilelisting') def getdataoutputfilelisting(): listoffilesindataouput = getfiles('static/dataoutput') return jsonify(listoffiles = listoffilesindataouput) def getfiles(dirpath): a = [s for s in os.listdir(dirpath) if os.path.isfile(os.path.join(dirpath, s))] a.sort(key=lambda s: os.path.getmtime(os.path.join(dirpath, s))) a.reverse() return a Is there a way to make it universal such that it works in both OSs? Thanks

    Read the article

  • Divide a array into multiple (individual) arrays based on a bin size in python

    - by user1492449
    I have an array like this: -0.68285 -6.919616 -0.7876 -14.521115 -0.64072 -43.428411 -0.05368 -11.561341 -0.43144 -34.768892 -0.23268 -10.793603 -0.22216 -50.341101 -0.41152 -90.083377 -0.01288 -84.265557 -0.3524 -24.253145 How do i split this array into individual arrays based on the value in column 1 with a bin width of 0.1? i want my output something like this: array1=[[-0.05368, -11.561341],[-0.01288, -84.265557]] array2=[[-0.23268, -10.79360] ,[-0.22216, -50.341101]] array3=[[-0.3524, -24.253145]] array4=[[-0.43144, -34.768892], [-0.41152, -90.083377]] array5=[[-0.68285, -6.919616],[-0.64072, -43.428411]] array6=[[-0.7876, -14.521115]]

    Read the article

  • Python - Submit Information on a Website to Extract Data from Resulting Page

    - by bloodstorm17
    So I am trying to figure out how to post on a website that uses a drop down menu which is holding the values like this (based on the page source): <td valign="top" align="right"><span class="emphasis">Select Item Option : </span></td> <td align="left"> <span class="notranslate"> <select name="ItemOption1"> <option value="">Select Item Option</option> <option value="321_cba">Item Option 1</option> <option value="123_abcd">Item Option 2</option> ... Now there are two of these drop down menus on top of each other. I want to be able to select an item from drop down menu 1 and drop down menu 2 and then submit the page. Now based on the code it submits the information using the following code: <td colspan="2" align="center"> <input type="submit" value="View Result" onclick="return check()"> </td> </tr> </table> <input type="hidden" name="ItemOption1" value=""> <input type="hidden" name="ItemOption2" value=""> I have no idea how to select the items in the drop down menu and then submit the page and capture the information on the resulting page into a text file. Can someone please help me with this?

    Read the article

  • Python iterate object with a list of objects

    - by nerd
    First time poster, long time reader. Is it possible to iterate though an object that contains a list of objects. For example, I have the following class Class Page(object) def __init__(self, name): self.name = name self.pages = [] I then create a new Page object and add other page objects to it. page = Page('FirstPage') apagepage = Page('FirstChild') anotherpagepage = Page('SecondChild') apagepage.pages.append(Page('FirstChildChild')) apagepage.pages.append(Page('SecondChildChild')) page.pages.append(apagepage) page.pages.append(anotherpagepage) What I would like to do is for thispage in page: print thispage.name And get the following output FirstPage FirstChild SecondChild FirstChildChild SecondChildChild So I get all the 1st level, then the 2nd, then the 3rd. However, the following output would be find as well FirstPage FirstChild FirstChildChild SecondChildChild SecondChild

    Read the article

  • Gitosis-init returns "Fatal Python error: <stdin> is a directory", why is this?

    - by Jasper Kennis
    I'm trying to get gitosis installed because I want to use Indefero and I need a deamon for the git:// protocol. However, following the instructions in the Git Pro book (http://progit.org/book/ch4-7.html) I run into trouble pretty soon. This is what happens: [x@x gitosis]# sudo -H -u git gitosis-init < /tmp/id_dsa.pub Fatal Python error: <stdin> is a directory Aborted The error is really vague to me and I didn't find anything helpful around, except that I think stdin is somehow part of C, which confuses me even more since the error is Python. I really don't understand what's going on, or where to look for clues, so I hope someone can tell me where to look next for more info on the problem. Tnx.

    Read the article

< Previous Page | 174 175 176 177 178 179 180 181 182 183 184 185  | Next Page >