Re: [Tutor] Hello World in Python without space
On 7/10/2011 4:12 AM Robert H said... Dear all, I have Python 3.2 installed on Windows 7. I am a complete beginner playing around with the basic functions. My problem is the following script: name="world" print("Hello", name,"!") print("Hello", name+"!") Alan mentioned using concatenation as well and "".join() is generally preferred, particularly when many strings are involved. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] List methods inside a dictionary of list
Hello, I am playing lists and dictionaries and I came across this counter-intuitive result. >>> d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) >>>d Out: {'a': [0], 'b': [0], 'c': [0], 'd': [0], 'e': [0], 'g': [0], 'j': [0], 'q': [0]} >>> d['a'].__setitem__(0,4) >>> d Out: {'a': [4], 'b': [4], 'c': [4], 'd': [4], 'e': [4], 'g': [4], 'j': [4], 'q': [4]} I was not expecting all the keys to be updated. Is there any documentation I could read on how different datatypes' methods and operators interact differently when inside a dictionary? I would also like to find a way of being able to use list methods in side a dictionary so that >>> d['a'][0] = 5 Does not return {'a': [5], 'b': [5], 'c': [5], 'd': [5], 'e': [5], 'g': [5], 'j': [5], 'q': [5]} But rather {'a': [5], 'b': [0], 'c': [0], 'd': [0], 'e': [0], 'g': [0], 'j': [0], 'q': [0]} Where d is made by d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) Thanks a bunch, Rafael ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 89, Issue 22
It just name+"!", this is string concatenation tutor-requ...@python.org wrote: >Send Tutor mailing list submissions to > tutor@python.org > >To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor >or, via email, send a message with subject or body 'help' to > tutor-requ...@python.org > >You can reach the person managing the list at > tutor-ow...@python.org > >When replying, please edit your Subject line so it is more specific >than "Re: Contents of Tutor digest..." >Today's Topics: > > 1. Hello World in Python without space (Robert H) > 2. Re: Hello World in Python without space (Izz ad-Din Ruhulessin) > 3. Re: Hello World in Python without space (Peter Otten) > 4. Re: Hello World in Python without space (Alan Gauld) > 5. Re: broken script - curiouser and curiouser (Lisi) > >Dear all, > > >I have Python 3.2 installed on Windows 7. I am a complete beginner >playing around with the basic functions. My problem is the following >script: > > >name="world" >print("Hello", name,"!") > > >The result is: >Hello world ! > > >However, I don't want the space before the exclamation mark. I want >this: >Hello world! > > >I tried to solve the problem with e.g.: >print("Hello",name.strip(),"!") >but the result is the same. > > >Can anyone out there help me? Thank you. > > >Regards, >Robert > Sending args to the print command > always puts spaces between >them. > >Try: >print("Hello {name}!".format(name=name)) > > > > > >2011/7/10 Robert H > >> Dear all, >> >> >> I have Python 3.2 installed on Windows 7. I am a complete beginner >playing >> around with the basic functions. My problem is the following script: >> >> >> name="world" >> print("Hello", name,"!") >> >> >> The result is: >> Hello world ! >> >> >> However, I don't want the space before the exclamation mark. I want >this: >> Hello world! >> >> >> I tried to solve the problem with e.g.: >> print("Hello",name.strip(),"!") >> but the result is the same. >> >> >> Can anyone out there help me? Thank you. >> >> >> Regards, >> Robert >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> >Robert H wrote: > >> I have Python 3.2 installed on Windows 7. I am a complete beginner >playing >> around with the basic functions. My problem is the following script: >> >> >> name="world" >> print("Hello", name,"!") >> >> >> The result is: >> Hello world ! >> >> >> However, I don't want the space before the exclamation mark. I want >this: >> Hello world! >> >> >> I tried to solve the problem with e.g.: >> print("Hello",name.strip(),"!") >> but the result is the same. > > >print() by default inserts a space between its arguments. You can avoid >that >by specifying a separator explicitly with the "sep" keyword. Let me >show it >in the interactive interpreter which is generally a good place to >experiment >with small snippets of code: > name = "Robert" print("Hello ", name, "!", sep="") # Note the explicit " " after >"Hello" >Hello Robert! > >Another goodie is that you can easily get useful information about >modules, >classes, keywords, and functions, e. g. > help(print) > >shows > >print(...) >print(value, ..., sep=' ', end='\n', file=sys.stdout) > >Prints the values to a stream, or to sys.stdout by default. >Optional keyword arguments: > file: a file-like object (stream); defaults to the current sys.stdout. >sep: string inserted between values, default a space. >end: string appended after the last value, default a newline. > >Use help() without argument to learn more about the interactive help. > > > >"Robert H" wrote > >> name="world" >> print("Hello", name,"!") >> Hello world ! >> >> However, I don't want the space before the exclamation >> mark. I want this: >> Hello world! > >> Can anyone out there help me? Thank you. > >I see you've already had two answers, a third is >to construct the string before printing it. There >are various ways to do that: > >The simplest: > >output = "Hello " + name + "!" > >An alternative which is more efficient for >larger numbers of substruings is: > >output = "".join(["Hello ",name,"!"]) # thats an empty string to >start > >The thirs is to use a formatstring, but thats >what Izz did in his print call. > >Whichever method you use you then use > >print(output) > >Lots of options. As Peter said, use the >>> prompt to >experiment to find which works best for you. > > >-- >Alan Gauld >Author of the Learn to Program web site >http://www.alan-g.me.uk/ > > > > > > >On Thursday 07 July 2011 15:36:06 Michael M Mason wrote: >> Maybe you've been editing ex25 and then executing ex26. That'd get >you the >> same error in the same place over and over again. > >Doh! They do say that there is one born every minute. > >Thanks, >Lisi > >__
Re: [Tutor] List methods inside a dictionary of list
Hi, On 11 July 2011 14:26, Rafael Turner wrote: > > >>> d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) > >>>d > Out: > {'a': [0], > 'b': [0], > 'c': [0], > 'd': [0], > 'e': [0], > 'g': [0], > 'j': [0], > 'q': [0]} > > >>> d['a'].__setitem__(0,4) > >>> d > Out: > {'a': [4], > 'b': [4], > 'c': [4], > 'd': [4], > 'e': [4], > 'g': [4], > 'j': [4], > 'q': [4]} > > I was not expecting all the keys to be updated. Is there any > documentation I could read on how different datatypes' methods and > operators interact differently when inside a dictionary? I would also > like to find a way of being able to use list methods in side a > dictionary so that > There's no funny interaction based on datatypes as you imply. The thing you're missing is that when you do 8*[[0]], you're actually creating a a list of references to another **single list** (which happens to contain a single value, 0.) Thus, when you then update that single value 0 in that single list, being pointed to by the several locations in the outer list, you predictably end up seeing the change from all the references in the outer list. To illustrate, do the following: >>> l=[0] >>> l2=[l] >>> l2 [[0]] >>> l3=8*l2 >>> l3 [[0], [0], [0], [0], [0], [0], [0], [0]] >>> l[0]=1 >>> l3 [[1], [1], [1], [1], [1], [1], [1], [1]] >>> Describing the above: l is a *single *list object, containing a single value, 0. l2 is another single list containing this single list l. l3 is a third list, constructed to by contatenating the* contents* of l2 8 times. This means l3 effectively ends up containing list l (which is the contents of l2) 8 times in successtion. *The contents of list l2 is a reference to list l.* So consequently in other words each entry in l3 is actually a back reference to the ***same original list l***. That is the key bit to understand, in order to understand why you're seeing what you're seeing in your example. Now to illustrate this, we change the original single list l's first element to something else, namely 1. And, as expected, when we then view the apparent contents of l3, as before, it reflects the contents of list l 8 times, because again, every entry in l3 is actually a reference to the same original list l, which now contains 1 instead of 0. So, depending on what you're tring to do, you probably want to change your data structure in your example in some way to ensure you have new seperate sublists for each entry in your outer list. Regards Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List methods inside a dictionary of list
On Mon, Jul 11, 2011 at 9:26 AM, Rafael Turner wrote: > I am playing lists and dictionaries and I came across this > counter-intuitive result. > d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) ... d['a'].__setitem__(0,4) ... > > I was not expecting all the keys to be updated. Is there any > documentation I could read on how different datatypes' methods and > operators interact differently when inside a dictionary? I would also > like to find a way of being able to use list methods in side a > dictionary so that As has been mentioned, this isn't the dictionary doing anything weird, this is that "8*[[0]]" gives you a list of 8 references to the same list. You can play with just that part and see that that's the source of your issue. To achieve what you are trying, try this instead: d = dict([(x,[0]) for x in ['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j']]) Can you understand how this behaves differently than 8*[[0]] ? Check the Python docs for array multiplication if you're confused, but the basic idea is that "[0]" isn't getting evaluated freshly for every piece in the array for 8*[[0]], but in a list comprehension it is. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List methods inside a dictionary of list
I did not understand the behavior of array multiplication. In fact, I just now learned what it was called thanks to your email. Best wishes, Rafael On Mon, Jul 11, 2011 at 9:33 AM, Brett Ritter wrote: > On Mon, Jul 11, 2011 at 9:26 AM, Rafael Turner > wrote: >> I am playing lists and dictionaries and I came across this >> counter-intuitive result. >> > d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) > ... > d['a'].__setitem__(0,4) > ... >> >> I was not expecting all the keys to be updated. Is there any >> documentation I could read on how different datatypes' methods and >> operators interact differently when inside a dictionary? I would also >> like to find a way of being able to use list methods in side a >> dictionary so that > > As has been mentioned, this isn't the dictionary doing anything weird, > this is that "8*[[0]]" gives you a list of 8 references to the same > list. You can play with just that part and see that that's the source > of your issue. > > To achieve what you are trying, try this instead: > > d = dict([(x,[0]) for x in ['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j']]) > > Can you understand how this behaves differently than 8*[[0]] ? Check > the Python docs for array multiplication if you're confused, but the > basic idea is that "[0]" isn't getting evaluated freshly for every > piece in the array for 8*[[0]], but in a list comprehension it is. > -- > Brett Ritter / SwiftOne > swift...@swiftone.org > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] compare and arrange file
hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| X | 0.00| 90443.29| X | 88115.39| 0.00| X | 0.00| 88335.39| X | 90453.29| 0.00| X | 88335.39| 0.00| X | 90443.29| 0.00| now i need re-arrange the file in this way: X | 0.00| 88115.39| X | 88115.39| 0.00| X | 0.00| 90453.29| X | 90453.29| 0.00| etc i try this http://pastebin.com/2mvxn5GY but without look maybe somebody can give some hint , i know that this is maybe not a directly python question but if somebody can help me I will appreciate it Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 7/11/2011 3:16 PM Edgar Almonte said... hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| X | 0.00| 90443.29| X | 88115.39| 0.00| X | 0.00| 88335.39| X | 90453.29| 0.00| X | 88335.39| 0.00| X | 90443.29| 0.00| now i need re-arrange the file in this way: X | 0.00| 88115.39| X | 88115.39| 0.00| X | 0.00| 90453.29| X | 90453.29| 0.00| etc It's not obvious to me for your sample what you want. For example, the 2nd value 90453.29 from the re-arranged group doesn't appear in the top sample. If I venture a guess, it seems to me that you want the debits and corresponding offsetting credits listed in sequence. In pseudo-code, that might me done as: read lines from file for each line in lines set flag to D or C based on values set sortkey to value+flag append sortkey and line to decorated list sort decorated list for key,line in decorated list print line HTH, Emile i try this http://pastebin.com/2mvxn5GY but without look maybe somebody can give some hint , i know that this is maybe not a directly python question but if somebody can help me I will appreciate it Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 07/11/2011 06:39 PM, Emile van Sebille wrote: On 7/11/2011 3:16 PM Edgar Almonte said... hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| X | 0.00| 90443.29| X | 88115.39| 0.00| X | 0.00| 88335.39| X | 90453.29| 0.00| X | 88335.39| 0.00| X | 90443.29| 0.00| now i need re-arrange the file in this way: X | 0.00| 88115.39| X | 88115.39| 0.00| X | 0.00| 90453.29| X | 90453.29| 0.00| etc It's not obvious to me for your sample what you want. For example, the 2nd value 90453.29 from the re-arranged group doesn't appear in the top sample. If I venture a guess, it seems to me that you want the debits and corresponding offsetting credits listed in sequence. In pseudo-code, that might me done as: read lines from file for each line in lines set flag to D or C based on values set sortkey to value+flag append sortkey and line to decorated list sort decorated list for key,line in decorated list print line HTH, Emile i try this http://pastebin.com/2mvxn5GY but without look I also can't see any pattern in the data to give a clue what kind of filtering you're trying to do. A more specific spec would be useful. I can comment on your pastebin code, however. You should have pasted it in your message, since it's short. 1. def splitline(line, z): 2. if z == 0: 3. pass 4. 5. fields = line.split('|') 6. nlist = [] 7. for field in fields: 8. nlist.append(field) 9. 10. return nlist[z] The whole loop with nlist is a waste of energy, as you already got a list from split(). You could replace the function with def splitline(line, z): return line.split('|')[z] The if z==0 doesn't do anything either. Perhaps you meant to do some error checking in case the line doesn't have at least z fields. But the real problem in your code is the you posted for loop. The inner loop takes multiple passes through the orig1 file, but the second time won't get anything, since the file is already positioned at the end. I'd simply move the open statement inside the outer loop. There may be other problems, but that could get you going. DaveA -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
Thanks for the hints , what i want accomplish is sort the line by the same value in the column 2 and 3 i mean the line with the same value in the 2 get together with the line in the same value in column 3 emile and david thanks again let me check the hint that your give me, i will feedback the code if everything workout or else :D On Mon, Jul 11, 2011 at 7:35 PM, Dave Angel wrote: > On 07/11/2011 06:39 PM, Emile van Sebille wrote: > > On 7/11/2011 3:16 PM Edgar Almonte said... > > hello , i have a file this a structure like this > X | 0.00| 88115.39| > X | 90453.29| 0.00| > X | 0.00| 90443.29| > X | 88115.39| 0.00| > X | 0.00| 88335.39| > X | 90453.29| 0.00| > X | 88335.39| 0.00| > X | 90443.29| 0.00| > > now i need re-arrange the file in this way: > X | 0.00| 88115.39| > X | 88115.39| 0.00| > X | 0.00| 90453.29| > X | 90453.29| 0.00| > > etc > > It's not obvious to me for your sample what you want. For example, the 2nd > value 90453.29 from the re-arranged group doesn't appear in the top > sample. > > If I venture a guess, it seems to me that you want the debits and > corresponding offsetting credits listed in sequence. > > In pseudo-code, that might me done as: > > read lines from file > for each line in lines > set flag to D or C based on values > set sortkey to value+flag > append sortkey and line to decorated list > sort decorated list > for key,line in decorated list > print line > > > HTH, > > Emile > > > > > > i try this > http://pastebin.com/2mvxn5GY > but without look > > I also can't see any pattern in the data to give a clue what kind of > filtering you're trying to do. A more specific spec would be useful. > > I can comment on your pastebin code, however. You should have pasted it in > your message, since it's short. > > def splitline(line, z): > if z == 0: > pass > > fields = line.split('|') > nlist = [] > for field in fields: > nlist.append(field) > > return nlist[z] > > The whole loop with nlist is a waste of energy, as you already got a list > from split(). You could replace the function with > def splitline(line, z): > return line.split('|')[z] > > The if z==0 doesn't do anything either. Perhaps you meant to do some error > checking in case the line doesn't have at least z fields. > > But the real problem in your code is the you posted for loop. The inner > loop takes multiple passes through the orig1 file, but the second time won't > get anything, since the file is already positioned at the end. I'd simply > move the open statement inside the outer loop. > > There may be other problems, but that could get you going. > > DaveA > > > > > > > > -- > > DaveA > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
back again, yes that is the idea: "> If I venture a guess, it seems to me that you want the debits and > corresponding offsetting credits listed in sequence." but not sure if i get you pseudo code , you mean some how flag the line when is D or C ( credit of debit ) and then sort by what ? On Mon, Jul 11, 2011 at 6:39 PM, Emile van Sebille wrote: > On 7/11/2011 3:16 PM Edgar Almonte said... >> >> hello , i have a file this a structure like this >> X | 0.00| 88115.39| >> X | 90453.29| 0.00| >> X | 0.00| 90443.29| >> X | 88115.39| 0.00| >> X | 0.00| 88335.39| >> X | 90453.29| 0.00| >> X | 88335.39| 0.00| >> X | 90443.29| 0.00| >> >> now i need re-arrange the file in this way: >> X | 0.00| 88115.39| >> X | 88115.39| 0.00| >> X | 0.00| 90453.29| >> X | 90453.29| 0.00| >> >> etc > > It's not obvious to me for your sample what you want. For example, the 2nd > value 90453.29 from the re-arranged group doesn't appear in the top > sample. > offsetting credits listed in sequence > > In pseudo-code, that might me done as: > > read lines from file > for each line in lines > set flag to D or C based on values > set sortkey to value+flag > append sortkey and line to decorated list > sort decorated list > for key,line in decorated list > print line > > > HTH, > > Emile > > > >> >> >> i try this >> http://pastebin.com/2mvxn5GY >> but without look >> >> maybe somebody can give some hint , i know that this is maybe not a >> directly python question but if somebody can help me I will appreciate >> it >> >> Thanks >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
back again david i do the for because the line is delimited by pipeline so and i need get the field number 2 and 3 of the line if i understand well the split('|')[z] will just split till there ( z value ) so if i do split('|')[2] i will get: X , 0.00 and i just want the number value part of the line i try putting the read of orig1 inside the fist loop but that don't see work because the thing is that for some reason the fist loop is just passing one time ( the first one if you see in my code i put a print line1 in the first loop and a print value in the second one and i get something line "line1---" "value1" "value1" "value1" "value1" "value1" "value1" etc. pd: sorry for my bad english On Mon, Jul 11, 2011 at 7:35 PM, Dave Angel wrote: > On 07/11/2011 06:39 PM, Emile van Sebille wrote: > > On 7/11/2011 3:16 PM Edgar Almonte said... > > hello , i have a file this a structure like this > X | 0.00| 88115.39| > X | 90453.29| 0.00| > X | 0.00| 90443.29| > X | 88115.39| 0.00| > X | 0.00| 88335.39| > X | 90453.29| 0.00| > X | 88335.39| 0.00| > X | 90443.29| 0.00| > > now i need re-arrange the file in this way: > X | 0.00| 88115.39| > X | 88115.39| 0.00| > X | 0.00| 90453.29| > X | 90453.29| 0.00| > > etc > > It's not obvious to me for your sample what you want. For example, the 2nd > value 90453.29 from the re-arranged group doesn't appear in the top > sample. > > If I venture a guess, it seems to me that you want the debits and > corresponding offsetting credits listed in sequence. > > In pseudo-code, that might me done as: > > read lines from file > for each line in lines > set flag to D or C based on values > set sortkey to value+flag > append sortkey and line to decorated list > sort decorated list > for key,line in decorated list > print line > > > HTH, > > Emile > > > > > > i try this > http://pastebin.com/2mvxn5GY > but without look > > I also can't see any pattern in the data to give a clue what kind of > filtering you're trying to do. A more specific spec would be useful. > > I can comment on your pastebin code, however. You should have pasted it in > your message, since it's short. > > def splitline(line, z): > if z == 0: > pass > > fields = line.split('|') > nlist = [] > for field in fields: > nlist.append(field) > > return nlist[z] > > The whole loop with nlist is a waste of energy, as you already got a list > from split(). You could replace the function with > def splitline(line, z): > return line.split('|')[z] > > The if z==0 doesn't do anything either. Perhaps you meant to do some error > checking in case the line doesn't have at least z fields. > > But the real problem in your code is the you posted for loop. The inner > loop takes multiple passes through the orig1 file, but the second time won't > get anything, since the file is already positioned at the end. I'd simply > move the open statement inside the outer loop. > > There may be other problems, but that could get you going. > > DaveA > > > > > > > > -- > > DaveA > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 11-Jul-11 16:50, Edgar Almonte wrote: Thanks for the hints , what i want accomplish is sort the line by the same value in the column 2 and 3 i mean the line with the same value in the 2 get together with the line in the same value in column 3 What if the same value appears more than once? Does it matter which ones you match up? If so, how do you decide? -- Steve Willoughby / st...@alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
this is just one time thing and the value don't get repeat On Mon, Jul 11, 2011 at 7:55 PM, Steve Willoughby wrote: > On 11-Jul-11 16:50, Edgar Almonte wrote: >> >> Thanks for the hints , what i want accomplish is sort the line by the >> same value in the column 2 and 3 >> >> i mean the line with the same value in the 2 get together with the >> line in the same value in column 3 > > What if the same value appears more than once? Does it matter which ones > you match up? If so, how do you decide? > > -- > Steve Willoughby / st...@alchemy.com > "A ship in harbor is safe, but that is not what ships are built for." > PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 11-Jul-11 17:18, Edgar Almonte wrote: this is just one time thing and the value don't get repeat Then you could make a single loop over the input lines, building two dictionaries as you go: * one that maps column 2's value to the rest of that line's data * and one that does this for column 3's value. Now run through the column 2 data you saved, print that data row, then look up the value in the other dictionary and print that after it. On Mon, Jul 11, 2011 at 7:55 PM, Steve Willoughby wrote: On 11-Jul-11 16:50, Edgar Almonte wrote: Thanks for the hints , what i want accomplish is sort the line by the same value in the column 2 and 3 i mean the line with the same value in the 2 get together with the line in the same value in column 3 What if the same value appears more than once? Does it matter which ones you match up? If so, how do you decide? -- Steve Willoughby / st...@alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby / st...@alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
i not too smart steve , can you show me with code ? On Mon, Jul 11, 2011 at 8:22 PM, Steve Willoughby wrote: > On 11-Jul-11 17:18, Edgar Almonte wrote: >> >> this is just one time thing and the value don't get repeat > > Then you could make a single loop over the input lines, building two > dictionaries as you go: > * one that maps column 2's value to the rest of that line's data > * and one that does this for column 3's value. > > Now run through the column 2 data you saved, print that data row, > then look up the value in the other dictionary and print that after it. > >> >> On Mon, Jul 11, 2011 at 7:55 PM, Steve Willoughby >> wrote: >>> >>> On 11-Jul-11 16:50, Edgar Almonte wrote: Thanks for the hints , what i want accomplish is sort the line by the same value in the column 2 and 3 i mean the line with the same value in the 2 get together with the line in the same value in column 3 >>> >>> What if the same value appears more than once? Does it matter which ones >>> you match up? If so, how do you decide? >>> >>> -- >>> Steve Willoughby / st...@alchemy.com >>> "A ship in harbor is safe, but that is not what ships are built for." >>> PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C >>> ___ >>> Tutor maillist - Tutor@python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> > > > -- > Steve Willoughby / st...@alchemy.com > "A ship in harbor is safe, but that is not what ships are built for." > PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 7/11/2011 5:02 PM Edgar Almonte said... back again, yes that is the idea: "> If I venture a guess, it seems to me that you want the debits and corresponding offsetting credits listed in sequence." but not sure if i get you pseudo code , you mean some how flag the line when is D or C ( credit of debit ) and then sort by what ? When you sort a list of tuple pairs, it sort on the first item, so set sortkey to value+flag means the first tuple in the list of tuples to sort should end up with as value+flag, where the flag is set based on the non-zero value in your line. For example, XXXs,Dval,Cval = line.split("|") then, assuming a consistent valid file structure, if int(Dval): key="D"+Dval else: key="C"+Cval then, append (key,line) to your decorated list for each line, and finally sort and print the lines from your decorated list. Emile hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| In pseudo-code, that might me done as: read lines from file for each line in lines set flag to D or C based on values set sortkey to value+flag append sortkey and line to decorated list sort decorated list for key,line in decorated list print line ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
thanks emile i understand exactly what you explain me but i was unable to accomplish it ( too noob in python ) but i solved the problem with this code http://pastebin.com/4A6Jz4wZ i will try do what you suggest me anyway but that will tomorrow , tonight i done and i feel good :D Thanks all for the help On Mon, Jul 11, 2011 at 11:01 PM, Emile van Sebille wrote: > On 7/11/2011 5:02 PM Edgar Almonte said... >> >> back again, >> yes that is the idea: >> "> If I venture a guess, it seems to me that you want the debits and >>> >>> corresponding offsetting credits listed in sequence." >> >> >> but not sure if i get you pseudo code , you mean >> some how flag the line when is D or C ( credit of debit ) >> and then sort by what ? >> > > When you sort a list of tuple pairs, it sort on the first item, so > > set sortkey to value+flag > > means the first tuple in the list of tuples to sort should end up with as > value+flag, where the flag is set based on the non-zero value in your line. > > For example, > > XXXs,Dval,Cval = line.split("|") > > then, assuming a consistent valid file structure, > > if int(Dval): key="D"+Dval > else: key="C"+Cval > > then, append (key,line) to your decorated list for each line, and finally > sort and print the lines from your decorated list. > > Emile > > hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| > > > >>> In pseudo-code, that might me done as: >>> >>> read lines from file >>> for each line in lines >>> set flag to D or C based on values >>> set sortkey to value+flag >>> append sortkey and line to decorated list >>> sort decorated list >>> for key,line in decorated list >>> print line > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor