Re: Elastic Search
Steve D'Aprano writes: > What's elastic search? > And what does this have to do with Python? https://www.elastic.co/ (formerly elasticsearch.org). It's a Lucene-based distributed search engine, something like Solr if you're used to that. It has Python client libraries. That's the closest Python connection that I know of. I've never used it but I've used Solr. These things are good tools to have available if you need to index a lot of text. -- https://mail.python.org/mailman/listinfo/python-list
read in a list in a file to list
Hi, I am using Python 3.6 on Windows 7. I have a file called apefile.txt. apefile.txt's contents are: apes = "Home sapiens", "Pan troglodytes", "Gorilla gorilla" I have a script: apefile = open("apefile.txt") apelist = apefile.read() for ape in apelist: print("one of the apes is " + ape) apefile.close() The output from the script does not print the ape names, instead it prints each letter in the file. For example: one of the apes is a one of the apes is p one of the apes is e What should I do instead to get something like one of the apes is Home sapiens one of the apes is Pan troglodytes one of the apes is Gorilla gorilla John -- https://mail.python.org/mailman/listinfo/python-list
Re: read in a list in a file to list
On Saturday, April 8, 2017 at 7:32:52 PM UTC+1, john polo wrote: > Hi, > > I am using Python 3.6 on Windows 7. > > I have a file called apefile.txt. apefile.txt's contents are: > > apes = "Home sapiens", "Pan troglodytes", "Gorilla gorilla" > > I have a script: > > apefile = open("apefile.txt") > apelist = apefile.read() > for ape in apelist: > print("one of the apes is " + ape) > apefile.close() > > The output from the script does not print the ape names, instead it > prints each letter in the file. For example: > > one of the apes is a > one of the apes is p > one of the apes is e > > What should I do instead to get something like > > one of the apes is Home sapiens > one of the apes is Pan troglodytes > one of the apes is Gorilla gorilla > > John I'll start you off. with open("apefile.txt") as apefile: for line in apefile: doSomething(line) String methods and/or the csv module might be used here in doSomething(line), but I'll leave that to you so that you can learn. If you get stuck please ask again, we don't bite :) Kindest regards. Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list
Re: read in a list in a file to list
On Sat, Apr 8, 2017 at 3:21 PM, wrote: > On Saturday, April 8, 2017 at 7:32:52 PM UTC+1, john polo wrote: >> Hi, >> >> I am using Python 3.6 on Windows 7. >> >> I have a file called apefile.txt. apefile.txt's contents are: >> >> apes = "Home sapiens", "Pan troglodytes", "Gorilla gorilla" >> >> I have a script: >> >> apefile = open("apefile.txt") >> apelist = apefile.read() I think you misunderstand what the read() method is doing here. It does not return a list. Instead, it returns the entire file as a single string. >> for ape in apelist: So here despite the variable name you chose, you are acutally iterating over the entire file contents character by character. See Mark's answer/hint below on how to iterate over the file contents by line. You might want to look up the docs on how to use these file objects and their methods. >> print("one of the apes is " + ape) >> apefile.close() >> >> The output from the script does not print the ape names, instead it >> prints each letter in the file. For example: >> >> one of the apes is a >> one of the apes is p >> one of the apes is e >> >> What should I do instead to get something like >> >> one of the apes is Home sapiens >> one of the apes is Pan troglodytes >> one of the apes is Gorilla gorilla >> >> John > > I'll start you off. > > with open("apefile.txt") as apefile: > for line in apefile: > doSomething(line) > > String methods and/or the csv module might be used here in doSomething(line), > but I'll leave that to you so that you can learn. If you get stuck please > ask again, we don't bite :) -- boB -- https://mail.python.org/mailman/listinfo/python-list
Re: read in a list in a file to list
@John General debugging methodology dictates that when your output does not match your expectation, you must never assume anything. Here you made the fatal mistake of assuming that: (1) files are stored as list objects, or (2) Python automatically converts file data to list objects, or (3) that python can read your mind, and knowing that you wanted a list, gave you a list. But in any of those cases, you would be wrong. To discover the source of the problem, use Python's wonderful introspection capabilities. In this case, the built-in function named "type" is your friend. > apefile = open("apefile.txt") > apelist = apefile.read() print(type(apelist)) I would also make a slight quibble reguarding your choice of variable names. I would recommend "fileObj" and "fileData" respectively. Unless you are comparing the contents of two or more files (say: "apeData", "monkeyData" and "marmosetData") there is no need for the "ape" mnemonic. Use a generic name instead. -- https://mail.python.org/mailman/listinfo/python-list
Python and the need for speed
I've an idea that http://www.mos6581.org/python_need_for_speed is a week late for April Fool's but just in case I'm sure that some of you may wish to comment. Kindest regards. Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list
Need help with getting Key, Value out of dicts in lists
This is an example of the data I'm working with. The key/value pairs may come in any order. There are some keys like the 'Resource_group_id' key and the 'Name' key which will always be present, but other lists may have unique keys. alist = [[{u'Value': 'shibboleth-prd', u'Key': 'Name'}, {u'Value': 'kvmu', u'Key': 'Billing'}, {u'Value': '20179204-181622543367489', u'Key': 'Resource_group_id'}], [{u'Value': '20172857-152037106154311', u'Key': 'Resource_group_id'}, {u'Value': 'shibboleth-tst', u'Key': 'Name'}]] What I want to do is something along the lines of: for a in alist: if a['Resource_group_id'] == '01234829-2041523815431': print the Value of 'Name' print the Value of 'Billing' I've found I can do the following, to print the value of 'Name' but that only works if the 'Resource_group_id' key is the first key in the list and the 'Name' key is in the second slot. If each list contained the same keys, I could probably sort the keys and use [num] to pull back values, but they don't. for a in alist: if a[0]['Key'] == 'Resource_group_id' and a[0]['Value'] == '20172857-152037106154311': print a[1]['Value'] There has to be a way to do this but I've been pounding away at this for hours. Any help appreciated. I'm new to Python and not a programmer, so go easy on me. :) -- https://mail.python.org/mailman/listinfo/python-list
Re: Python and the need for speed
On 04/08/2017 05:20 PM, breamore...@gmail.com wrote: > I've an idea that http://www.mos6581.org/python_need_for_speed is a week late > for April Fool's but just in case I'm sure that some of you may wish to > comment. > > Kindest regards. > > Mark Lawrence. > Regarding your restricted subset of python you call turbopython, have you looked into rpython which is a restricted subset of python used by pypy to implement their jit? http://rpython.readthedocs.io/en/latest/faq.html#what-is-this-rpython-language -- https://mail.python.org/mailman/listinfo/python-list
Re: Python and the need for speed
On Sun, Apr 9, 2017 at 10:20 AM, wrote: > I've an idea that http://www.mos6581.org/python_need_for_speed is a week late > for April Fool's but just in case I'm sure that some of you may wish to > comment. > >From that page: > Other candidates for banishment from TurboPython include eval and exec. Bye bye namedtuple. And compile() is going to have to go, since you could implement eval/exec by creating a new function: >>> runme = r""" print("Hello, world!") import sys sys.stdout.write("I can import modules.\n") """ >>> type(lambda: 1)(compile("def f():" + runme, "exec", "exec").co_consts[0], >>> globals())() Hello, world! I can import modules. So if compile() goes, you also lose ast.parse, which means you lose introspection tools, plus you lose literal_eval and friends. I'm also not sure whether the import machinery would have to be rewritten, but a quick 'git grep' suggests that it would. Removing eval and exec is not as simple as removing them from the standard library. In fact, extreme dynamism is baked deep into the language. You'd have to make some fairly sweeping language changes to get any real benefits from restricting things. There are better ways to improve performance, which is why statickification isn't really pursued. These kinds of posts always come from people who haven't actually tried it. They think that, hey, it'd be easy to just make a subset of Python that's optimizable! But if you actually *do* it, you'd have to either restrict the language HUGELY, or reimplement all sorts of things in a slower way. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Need help with getting Key, Value out of dicts in lists
> On Apr 8, 2017, at 5:55 PM, Kenton Brede wrote: > > This is an example of the data I'm working with. The key/value pairs may > come in any order. There are some keys like the 'Resource_group_id' key and > the 'Name' key which will always be present, but other lists may have > unique keys. > > alist = [[{u'Value': 'shibboleth-prd', u'Key': 'Name'}, {u'Value': 'kvmu', > u'Key': 'Billing'}, >{u'Value': '20179204-181622543367489', u'Key': > 'Resource_group_id'}], > [{u'Value': '20172857-152037106154311', u'Key': > 'Resource_group_id'}, >{u'Value': 'shibboleth-tst', u'Key': 'Name'}]] > > What I want to do is something along the lines of: > > for a in alist: >if a['Resource_group_id'] == '01234829-2041523815431': >print the Value of 'Name' >print the Value of 'Billing' > > I've found I can do the following, to print the value of 'Name' but that > only works if the 'Resource_group_id' key is the first key in the list and > the 'Name' key is in the second slot. If each list contained the same > keys, I could probably sort the keys and use [num] to pull back values, but > they don't. > > for a in alist: >if a[0]['Key'] == 'Resource_group_id' and a[0]['Value'] == > '20172857-152037106154311': >print a[1]['Value'] > > There has to be a way to do this but I've been pounding away at this for > hours. Any help appreciated. I'm new to Python and not a programmer, so > go easy on me. :) > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Need help with getting Key, Value out of dicts in lists
[ Sorry, forgot the important stuff! ] What you want to do is tricky because your data structure is difficult to deal with. My guess is that it has to do with a misconception about how a Python dictionary works. Yes, it is a series of key/value pairs, but not the way you have it. It looks like you put together dictionaries where each dictionary has a 'Value' and a 'Key'. Instead, _each_ item in a dictionary is a key value pair. The key is typically a string, and the value is obviously some value associated with that key. For example, if you have the ability to rebuild your data a different way, it looks like it would be better to deal with it something like this: aList = [ {'Name':'shibboleth-prd', 'Billing':'kmvu', 'Resource_group_id': '20179204-181622543367489'}, {'Name':'shibboleth-tst', 'Resource_group_id':'20172857-152037106154311'} ] This is a list of dictionaries. However, I'm not sure what you are trying to do with this data. I'm guessing that you want to match a resource group id, and if you find it, print the name and the billing info if they exist. If so, you may want something like this (untested): def printInfo(thisGroupID): for thisDict in aList:# loop through all dictionaries in the list if thisGroupID == aList['Resource_group_id']: if 'Name' in thisDict: # if thisDict has a key called 'Name' print ('Name is', thisDict['Dict']) if 'Billing' in thisDict: # if thisDict has a key called 'Billing' print ('Billing is', thisDict['Billing']) Hope this helps, Irv > On Apr 8, 2017, at 9:04 PM, Irv Kalb wrote: > > What you want to do is tricky because your data structure is difficult to > deal with. My guess is that it has to do with a misconception about how a > Python dictionary works. Yes, it is a series of key/value pairs, but not the > way you have it. It looks like you put together dictionaries where each > dictionary has a 'Value' and a 'Key'. > > Instead, _each_ item in a dictionary is a key value pair. The key is > typically a string, and the value is obviously some value associated with > that key. For example, if you have the ability to rebuild your data a > different way, it looks like it would be better to deal with it something > like this: > > aList = [ >{'Name':'shibboleth-prd', 'Billing':'kmvu', 'Resource_group_id': > '20179204-181622543367489'}, >{'Name':'shibboleth-tst', > 'Resource_group_id':'20172857-152037106154311'} >] > > This is a list of dictionaries. However, I'm not sure what you are trying to > do with this data. I'm guessing that you want to match a resource group id, > and if you find it, print the name and the billing info if they exist. If > so, you may want something like this (untested): > > def printInfo(thisGroupID): >for thisDict in aList:# loop through all dictionaries in the list > if thisGroupID == aList['Resource_group_id']: >if 'Name' in thisDict: # if thisDict has a key called 'Name' >print ('Name is', thisDict['Dict']) >if 'Billing' in thisDict: # if thisDict has a key called > 'Billing' >print ('Billing is', thisDict['Billing']) > > Hope this helps, > > Irv > > > >> On Apr 8, 2017, at 5:55 PM, Kenton Brede wrote: >> >> This is an example of the data I'm working with. The key/value pairs may >> come in any order. There are some keys like the 'Resource_group_id' key and >> the 'Name' key which will always be present, but other lists may have >> unique keys. >> >> alist = [[{u'Value': 'shibboleth-prd', u'Key': 'Name'}, {u'Value': 'kvmu', >> u'Key': 'Billing'}, >> {u'Value': '20179204-181622543367489', u'Key': >> 'Resource_group_id'}], >> [{u'Value': '20172857-152037106154311', u'Key': >> 'Resource_group_id'}, >> {u'Value': 'shibboleth-tst', u'Key': 'Name'}]] >> >> What I want to do is something along the lines of: >> >> for a in alist: >> if a['Resource_group_id'] == '01234829-2041523815431': >> print the Value of 'Name' >> print the Value of 'Billing' >> >> I've found I can do the following, to print the value of 'Name' but that >> only works if the 'Resource_group_id' key is the first key in the list and >> the 'Name' key is in the second slot. If each list contained the same >> keys, I could probably sort the keys and use [num] to pull back values, but >> they don't. >> >> for a in alist: >> if a[0]['Key'] == 'Resource_group_id' and a[0]['Value'] == >> '20172857-152037106154311': >> print a[1]['Value'] >> >> There has to be a way to do this but I've been pounding away at this for >> hours. Any help appreciated. I'm new to Python and not a programmer, so >> go easy on me. :) >> -- >> https://mail.python.org/mailman/listinfo/python-list >> > -- https://mail.python.org/mailman/listinfo/python-list