[Tutor] How to show dictionary item non present on file
Hin there!!! I have a niave question on dictionary analysis: If you have a dictionary like this: diz Out[8]: {'elenour': 1, 'frank': 1, 'jack': 1, 'ralph': 1} and you have a list and you want to know which keys are not present on my dictionary the code are simple. for i in diz.keys(): ...: if i in mitico: ...: print "NO" ...: else: ...: print i ...: NO But I havethis problem I have a file and I want to know which elements are not present on my file from dictionary. more data.tmp jack1 pippo 1 luis1 frate 1 livio 1 frank 1 with open("data.tmp") as p: for i in p: lines= i.strip("\n").split("\t") if not diz.has_key(lines[0]): : print i : pippo 1 luis1 frate 1 livio 1 The output I want is to have : ralph and 'elenour.. how can I do this? thanks in advance! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to show dictionary item non present on file
On Tue, Jul 22, 2014 at 01:10:18PM +0200, jarod...@libero.it wrote: > But I havethis problem I have a file and I want to know which elements are > not > present on my file from dictionary. > more data.tmp > jack 1 > pippo 1 > luis 1 > frate 1 > livio 1 > frank 1 > > > with open("data.tmp") as p: > for i in p: > lines= i.strip("\n").split("\t") > if not diz.has_key(lines[0]): >: print i >: > pippo 1 > luis 1 > frate 1 > livio 1 > > The output I want is to have : > ralph and 'elenour.. how can I do this? You are doing the comparison the wrong way: you are saying: for each line in the file: is the line in the dict? if no, print the line What you want is: for each key in the dict: is the key in the file? if no, print the key It is not easy to try searching the file directly, so we copy the lines from the file into a set: lines = set() with open("data.tmp") as the_file: for line in the_file: line = line.strip().split("\t")[0] lines.add(line) Here is a shorter way to do the same thing: with open("data.tmp") as the_file: lines = set([line.strip().split("\t")[0] for line in the_file]) Now you can walk through the dict: for name in diz: if name not in lines: print(name) Or, if you prefer: names = set(diz) # copy the keys from the dict into a set print(names.difference(lines)) If you want to see the other way around: print(lines.difference(names)) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to show dictionary item non present on file
jarod...@libero.it wrote: > Hin there!!! > > I have a niave question on dictionary analysis: > If you have a dictionary like this: > diz > Out[8]: {'elenour': 1, 'frank': 1, 'jack': 1, 'ralph': 1} > > and you have a list and you want to know which keys are not present on my > dictionary the code are simple. > for i in diz.keys(): >...: if i in mitico: >...: print "NO" >...: else: >...: print i >...: > NO > > But I havethis problem I have a file and I want to know which elements are > not present on my file from dictionary. > more data.tmp > jack 1 > pippo 1 > luis 1 > frate 1 > livio 1 > frank 1 > > > with open("data.tmp") as p: > for i in p: > lines= i.strip("\n").split("\t") > if not diz.has_key(lines[0]): >: print i >: > pippo 1 > > luis 1 > > frate 1 > > livio 1 > > The output I want is to have : > ralph and 'elenour.. how can I do this? > thanks in advance! You have the logic backwards. You have to iterate over the names in the dict and look them up in the file: >>> diz = {'elenour': 1, 'frank': 1, 'jack': 1, 'ralph': 1} >>> def find_name(name): ... with open("data.tmp") as f: ... for line in f: ... if name == line.split("\t")[0]: ... return True ... return False ... >>> for name in diz: ... if not find_name(name): ... print name ... ralph elenour However, this is very inefficient as you have to read the file len(diz) times. It is better to store the names in a data structure suited for fast lookup first and then to use that instead of the file. Python's dict and set types are such data structures, so as we don't care about an associated value let's use a set: >>> with open("data.tmp") as f: ... names_in_file = {line.split("\t")[0] for line in f} ... >>> for name in diz: ... if not name in names_in_file: ... print name ... ralph elenour Digging a bit deeper you'll find that you can get these names with set arithmetic: >>> set(diz) - names_in_file set(['ralph', 'elenour']) or even: >>> diz.viewkeys() - names_in_file set(['elenour', 'ralph']) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Getting a directory listing with Python to MySQL
Hello list, I'm new. I've done a few things in Python, but this one is posing problems. What I want to do is be able to parse a directory, say /Volumes/Stuff/Files/, and all the directories that might be in there, and be able to pick out file name, size, date modified, etc, and send that to a MySQL database. Any ideas? Sounds like it should be easy but.. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting a directory listing with Python to MySQL
> What I want to do is be able to parse a directory, say /Volumes/Stuff/Files/, and all the directories that might be in there, and be able to pick out file name, size, date modified, etc, Hi Eric, You might find the following helpful: http://www.diveintopython.net/file_handling/os_module.html ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting a directory listing with Python to MySQL
On Tue, Jul 22, 2014 at 04:10:02PM -0700, Eric Dannewitz wrote: > Hello list, I'm new. I've done a few things in Python, but this one is posing > problems. > > What I want to do is be able to parse a directory, say > /Volumes/Stuff/Files/, and all the directories that might be in there, > and be able to pick out file name, size, date modified, etc, and send > that to a MySQL database. Any ideas? Sounds like it should be easy > but.. os.listdir(path) returns all the entries under path (apart from '.' and '..'). You can then test whether they are files, directories or links with os.path.isdir, os.path.isfile, os.path.islink. (Remember that under Linux and Unix, there things other than files and links that can live in directories, e.g. named pipes.) But rather than manually iterate through the contents of the directory, os.walk already does that for you. Something like this ought to get you started: files = [] for dirpath, dirnames, filenames in os.walk(start_path): pathnames = [os.path.join(dirpath, name) for name in filenames] files.extend(pathnames) That gets you a list of all the files under start_path. To check their size, dates, etc. use the os.stat and os.lstat functions (os.stat follows symbolic links, os.lstat does not). The stat module has a bunch of symbolic constants which may be helpful for interpreting the results. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting a directory listing with Python to MySQL
That's close. I have been playing from glob and os.walk but I'm at a loss how to get the size, creation and modified date while running it. - Original Message - From: "Danny Yoo" To: "Eric Dannewitz" Cc: "Python Tutor Mailing List" Sent: Tuesday, July 22, 2014 6:14:55 PM Subject: Re: [Tutor] Getting a directory listing with Python to MySQL > What I want to do is be able to parse a directory, say /Volumes/Stuff/Files/, > and all the directories that might be in there, and be able to pick out file > name, size, date modified, etc, Hi Eric, You might find the following helpful: http://www.diveintopython.net/file_handling/os_module.html ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Hi again and thank-you Wolfgang and company. I wish I could give you snickers bars or something! I was able to get through the road_blocks with your help. I have been working the last few days, I am sorry to say, unsuccessfully, to calculate the mean (that's easy), split the data into sub-groups or secondary means - which are the break values between 4 classes. Create data-sets with incursive values. I can do it with brute force (copy and paste) but need to rise to the pythonic way and use a while loop and a nested if-else structure. My attempts have been lame enough that I don't even want to put them here. int_list [36, 39, 39, 45, 61, 54, 61, 93, 62, 51, 47, 72, 54, 36, 62, 50, 41, 41, 40, 62, 62, 58, 57, 54, 49, 43, 47, 50, 45, 41, 54, 57, 57, 55, 62, 51, 34, 57, 55, 63, 45, 45, 42, 44, 34, 53, 67, 58, 56, 43, 33] >>> int_list.sort() >>> int_list [33, 34, 34, 36, 36, 39, 39, 40, 41, 41, 41, 42, 43, 43, 44, 45, 45, 45, 45, 47, 47, 49, 50, 50, 51, 51, 53, 54, 54, 54, 54, 55, 55, 56, 57, 57, 57, 57, 58, 58, 61, 61, 62, 62, 62, 62, 62, 63, 67, 72, 93] >>> flo_list = [float(integral) for integral in int_list] >>> flo_list [33.0, 34.0, 34.0, 36.0, 36.0, 39.0, 39.0, 40.0, 41.0, 41.0, 41.0, 42.0, 43.0, 43.0, 44.0, 45.0, 45.0, 45.0, 45.0, 47.0, 47.0, 49.0, 50.0, 50.0, 51.0, 51.0, 53.0, 54.0, 54.0, 54.0, 54.0, 55.0, 55.0, 56.0, 57.0, 57.0, 57.0, 57.0, 58.0, 58.0, 61.0, 61.0, 62.0, 62.0, 62.0, 62.0, 62.0, 63.0, 67.0, 72.0, 93.0] >>> sum(flo_list) 2617.0 >>> totalnum = sum(flo_list) >>> len(flo_list) 51 >>> mean = sum(flo_list)/len(flo_list) >>> mean 51.31372549019608 Thank-you again, LN On Monday, July 21, 2014 2:55 AM, Wolfgang Maier wrote: On 21.07.2014 01:48, LN A-go-go wrote:> Dear Gurus, > Thank-you thank-you, one more time, thank-you. > I am over that hurdle and have the other: converting the list of > characters that was just created by the split - into integers so I can > manipulate them. I was trying to convert to a string, then to > integers. I have been scouring the web for casting integers, converting > to strings and also trying to look up the error messages: I > apologize for the mess - I was trying everything. > > Sigh, > Python in the hands of the naïve > > LN. > numlist = data_value numlist > ['36', '39', '39', '45', '61', '54', '61', '93', '62', '51', '47', '72', > '54', '36', '62', '50', '41', '41', '40', '62', '62', '58', '57', '54', > '49', '43', '47', '50', '45', '41', '54', '57', '57', '55', '62', '51', > '34', '57', '55', '63', '45', '45', '42', '44', '34', '53', '67', '58', > '56', '43', '33'] print sum(numlist) > Traceback (most recent call last): > File "", line 1, in > print sum(numlist) > TypeError: unsupported operand type(s) for +: 'int' and 'str' orange_drinkers = int(data_value) First thing you have to do in programming is to state your problem correctly: you do not want to convert your list to an integer representation. This is what you are attempting in your last line above: convert the data_value list to a single integer, and obviously, that cannot work. Instead, what you want to do is to convert every element in your list to an integer. You already know how to do this in a for loop: int_list = [] for element in data_value: int_list.append(int(element)) However, for simple transformations like this, Python has a very powerful construct called list comprehensions (read about them here: https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions , they're really great), which allow you to replace the for loop with a simple: int_list = [int(element) for element in data_value] You can even combine the comprehension with sum on one line like this: print sum([int(element) for element in data_value]) Yet, while all of the above should help you learn Python programming patterns, your original code already had the best solution: data_value = [] .. while True: line = infile.readline() if not line: break .. States, OJ = string.split(line) XNUM = int(OJ) .. data_value.append(XNUM) , i.e. converting each value to an int before adding it to the original list. Best, Wolfgang ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor