Re: [Tutor] Count for loops
Small correction. file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt" with open(file_path) as a: b = a.read() get_year = input("What year were you born? ") count = 0 b= '3'+b[2:] n = len(b) for i in range(n-3): if b[i:i+4] == get_year: count += 1 print("Your birth date occurs %s times in PI!" % (count)) regards, Sarma. On Tue, Apr 4, 2017 at 5:07 AM, D.V.N.Sarma డి.వి.ఎన్.శర్మ < dvnsa...@gmail.com> wrote: > I will go for this modification of the original code. > > file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt" > with open(file_path) as a: > b = a.read() > > get_year = input("What year were you born? ") > > count = 0 > b= '3'+b[2:] > n = len(b) > for i in range(n-4): > if b[i:i+4] == get_year: > count += 1 > print("Your birth date occurs %s times in PI!" % (count)) > > regards, > Sarma. > > On Tue, Apr 4, 2017 at 12:54 AM, Mats Wichmann wrote: > >> On 04/03/2017 10:16 AM, Alan Gauld via Tutor wrote: >> > On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: >> >> Sorry. That was stupid of me. The loop does nothing. >> > >> > Let me rewrite the code with some different variable names... >> > >> with open(file_path) as a: >> b = a.read() >> > >> > with open (file_path) as PI_text: >> > PI_as_a_long_string = PI_text.read() >> > >> for year in b: >> > >> > for each_char in PI_as_a_long_string: >> > >> if get_year in b: >> count += 1 >> > >> > if get_year in PI_as_a_long_string: >> > count += 1 >> > >> print("Your birth date occurs %s times in PI!" % (count)) >> > >> > if count: >> >print("Your birth date occurs in PI, I checked, count, "times!") >> > >> > >> > Aren't meaningful variable names great? We should all do >> > them more often. >> >> >> So the takeaways here are: >> >> in the first ("non-counting") sample, there's no need to use a loop, >> because you're going to quit after the outcome "in" or "not in" right >> away - there's no loop behavior at all. >> >> for year in b: >> if get_year in b: >> print("Your year of birth occurs in PI!") >> break >> else: >> print("Your year of birth does not occur in PI.") >> break >> >> for the counting version you could do something that searches for the >> year repeatedly, avoiding starting over from the beginning so you're >> actually finding fresh instances, not the same one (hint, hint). There >> are several ways to do this, including slicing, indexing, etc. Alan's >> suggestion looks as good as any. >> >> Or, if you don't care about overlapping cases, the count method of a >> string will do just fine: >> >> PI_as_a_long_string.count(year) >> >> If you're talking about 4-digit year numbers using a Western calendar in >> digits of PI, the overlap effect seems unlikely to matter - let's say >> the year is 1919, do we think PI contains the sequence 191919? count >> would report back one instead of two in that case. In other cases it >> might matter; count is written specifically to not care about overlaps: >> "Return the number of (non-overlapping) occurrences" So that's worth >> keeping in mind when you think about what you need from >> substrings-in-strings cases. >> >> >> >> ___ >> 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
Re: [Tutor] Count for loops
On 04/04/17 00:37, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > I will go for this modification of the original code. > count = 0 > b= '3'+b[2:] > n = len(b) > for i in range(n-4): > if b[i:i+4] == get_year: > count += 1 While I think this works OK, I would probably suggest that this is one of the rare valid cases for using a regex(*) with a simple string. The findall() method with a suitable pattern and flags should catch overlaps. And is probably faster being written in C. (*)Assuming you want to include overlapping cases, otherwise just use b.count()... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Count for loops
I will go for this modification of the original code. file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt" with open(file_path) as a: b = a.read() get_year = input("What year were you born? ") count = 0 b= '3'+b[2:] n = len(b) for i in range(n-4): if b[i:i+4] == get_year: count += 1 print("Your birth date occurs %s times in PI!" % (count)) regards, Sarma. On Tue, Apr 4, 2017 at 12:54 AM, Mats Wichmann wrote: > On 04/03/2017 10:16 AM, Alan Gauld via Tutor wrote: > > On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > >> Sorry. That was stupid of me. The loop does nothing. > > > > Let me rewrite the code with some different variable names... > > > with open(file_path) as a: > b = a.read() > > > > with open (file_path) as PI_text: > > PI_as_a_long_string = PI_text.read() > > > for year in b: > > > > for each_char in PI_as_a_long_string: > > > if get_year in b: > count += 1 > > > > if get_year in PI_as_a_long_string: > > count += 1 > > > print("Your birth date occurs %s times in PI!" % (count)) > > > > if count: > >print("Your birth date occurs in PI, I checked, count, "times!") > > > > > > Aren't meaningful variable names great? We should all do > > them more often. > > > So the takeaways here are: > > in the first ("non-counting") sample, there's no need to use a loop, > because you're going to quit after the outcome "in" or "not in" right > away - there's no loop behavior at all. > > for year in b: > if get_year in b: > print("Your year of birth occurs in PI!") > break > else: > print("Your year of birth does not occur in PI.") > break > > for the counting version you could do something that searches for the > year repeatedly, avoiding starting over from the beginning so you're > actually finding fresh instances, not the same one (hint, hint). There > are several ways to do this, including slicing, indexing, etc. Alan's > suggestion looks as good as any. > > Or, if you don't care about overlapping cases, the count method of a > string will do just fine: > > PI_as_a_long_string.count(year) > > If you're talking about 4-digit year numbers using a Western calendar in > digits of PI, the overlap effect seems unlikely to matter - let's say > the year is 1919, do we think PI contains the sequence 191919? count > would report back one instead of two in that case. In other cases it > might matter; count is written specifically to not care about overlaps: > "Return the number of (non-overlapping) occurrences" So that's worth > keeping in mind when you think about what you need from > substrings-in-strings cases. > > > > ___ > 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
Re: [Tutor] Validating String contains IP address
On 03/04/17 19:43, Ed Manning wrote: > untrust_ip_address = raw_input('\nEnter the untrust IP ''"Example > 172.20.2.3/28"'':') > while not ipaddress.ip_network untrust_ip_address: Doesn't that give a syntax error? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Validating String contains IP address
Hello there, >what am I going wrong here? i need to validate this is an IP or ask >the question again > >untrust_ip_address = raw_input('\nEnter the untrust IP ''"Example >172.20.2.3/28"'':') This is a representation of an IP address along with the mask length for the prefix: 172.20.2.3/28 That is not, strictly speaking an IP, because there is ancillary information included. This corresponds to: 172.20.2.3 IP address 172.20.2.0/28 prefix The form you are showing is found on some systems, for example in the output from `ip address show` on Linux systems, but it is a commonly understood form. Look further at the library that I recommended last week, and I think you will find a solution. >while not ipaddress.ip_network untrust_ip_address: > untrust_ip_address = raw_input('\nEnter the untrust IP ''"Example > 172.20.2.3/28"'':') You might try using the ipaddress library in the following way: >>> i = ipaddress.ip_interface(u'172.20.2.3/28') >>> i.ip IPv4Address(u'172.20.2.3') >>> i.network IPv4Network(u'172.20.2.0/28') >>> i.with_prefixlen u'172.20.2.3/28' Good luck, -Martin -- Martin A. Brown http://linux-ip.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] asking about run the python 3 script in terminal
Quang nguyen writes: > I would like to open the script in Terminal with several extra > pictures which be called inside the script. I think by “Terminal” you mean the text-only terminal emulator. By definition, then, a text-only terminal is not going to display graphic images. So, can you explain what you mean by “open the script […] with several extra pictures”? > I tried to open in Terminal, but it gave me trouble about where is the > location of my pictures. If you can give a very simple example that shows what you're trying to do – enough that we can try it too, and try to see what you're seeing when you do it – then we may be able to help. -- \ “To have the choice between proprietary software packages, is | `\ being able to choose your master. Freedom means not having a | _o__)master.” —Richard M. Stallman, 2007-05-16 | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] asking about run the python 3 script in terminal
Hi guys, I would like to open the script in Terminal with several extra pictures which be called inside the script. I tried to open in Terminal, but it gave me trouble about where is the location of my pictures. Thank you for time. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Euclidean Distances between Atoms in a Molecule.
On 04/03/2017 05:36 AM, Peter Otten wrote: Stephen P. Molnar wrote: I am trying to port a program that I wrote in FORTRAN twenty years ago into Python 3 and am having a hard time trying to calculate the Euclidean distance between each atom in the molecule and every other atom in the molecule. Here is a typical table of coordinates: MASS X Y Z 0 12.011 -3.265636 0.198894 0.090858 1 12.011 -1.307161 1.522212 1.003463 2 12.011 1.213336 0.948208 -0.033373 3 14.007 3.238650 1.041523 1.301322 4 12.011 -5.954489 0.650878 0.803379 5 12.011 5.654476 0.480066 0.013757 6 12.011 6.372043 2.731713 -1.662411 7 12.011 7.655753 0.168393 2.096802 8 12.011 5.563051 -1.990203 -1.511875 91.008 -2.939469 -1.327967 -1.247635 10 1.008 -1.460475 2.993912 2.415410 11 1.008 1.218042 0.451815 -2.057439 12 1.008 -6.255901 2.575035 1.496984 13 1.008 -6.560562 -0.695722 2.248982 14 1.008 -7.152500 0.390758 -0.864115 15 1.008 4.959548 3.061356 -3.139100 16 1.008 8.197613 2.429073 -2.588339 17 1.008 6.503322 4.471092 -0.543939 18 1.008 7.845274 1.892126 3.227577 19 1.008 9.512371 -0.273198 1.291080 20 1.008 7.147039 -1.365346 3.393778 21 1.008 4.191488 -1.928466 -3.057804 22 1.008 5.061650 -3.595015 -0.302810 23 1.008 7.402586 -2.392148 -2.374554 What I need for further calculation is a matrix of the Euclidean distances between the atoms. So far in searching the Python literature I have only managed to confuse myself and would greatly appreciate any pointers towards a solution. Thanks in advance. Stitched together with heavy use of a search engine: $ cat data.txt MASS X Y Z 0 12.011 -3.265636 0.198894 0.090858 1 12.011 -1.307161 1.522212 1.003463 2 12.011 1.213336 0.948208 -0.033373 3 14.007 3.238650 1.041523 1.301322 4 12.011 -5.954489 0.650878 0.803379 5 12.011 5.654476 0.480066 0.013757 6 12.011 6.372043 2.731713 -1.662411 7 12.011 7.655753 0.168393 2.096802 8 12.011 5.563051 -1.990203 -1.511875 91.008 -2.939469 -1.327967 -1.247635 10 1.008 -1.460475 2.993912 2.415410 11 1.008 1.218042 0.451815 -2.057439 12 1.008 -6.255901 2.575035 1.496984 13 1.008 -6.560562 -0.695722 2.248982 14 1.008 -7.152500 0.390758 -0.864115 15 1.008 4.959548 3.061356 -3.139100 16 1.008 8.197613 2.429073 -2.588339 17 1.008 6.503322 4.471092 -0.543939 18 1.008 7.845274 1.892126 3.227577 19 1.008 9.512371 -0.273198 1.291080 20 1.008 7.147039 -1.365346 3.393778 21 1.008 4.191488 -1.928466 -3.057804 22 1.008 5.061650 -3.595015 -0.302810 23 1.008 7.402586 -2.392148 -2.374554 $ python3 Python 3.4.3 (default, Nov 17 2016, 01:08:31) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. import numpy, pandas, scipy.spatial.distance as dist df = pandas.read_table("data.txt", sep=" ", skipinitialspace=True) a = numpy.array(df[["X", "Y", "Z"]]) dist.squareform(dist.pdist(a, "euclidean")) Here's an example with just the first 4 atoms: dist.squareform(dist.pdist(a[:4], "euclidean")) array([[ 0., 2.53370139, 4.54291701, 6.6694065 ], [ 2.53370139, 0., 2.78521357, 4.58084922], [ 4.54291701, 2.78521357, 0., 2.42734737], [ 6.6694065 , 4.58084922, 2.42734737, 0.]]) See https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html There may be a way to do this with pandas.pivot_table(), but I didn't manage to find that. As Alan says, this is not the appropriate forum for the topic you are belabouring. Work your way through a Python tutorial to pick up the basics (we can help you with this), then go straight to where the (numpy/scipy) experts are. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor Thanks to everyone who answered. The problem has been solved. -- Stephen P. Molnar, Ph.D.Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Count for loops
On 04/03/2017 10:16 AM, Alan Gauld via Tutor wrote: > On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: >> Sorry. That was stupid of me. The loop does nothing. > > Let me rewrite the code with some different variable names... > with open(file_path) as a: b = a.read() > > with open (file_path) as PI_text: > PI_as_a_long_string = PI_text.read() > for year in b: > > for each_char in PI_as_a_long_string: > if get_year in b: count += 1 > > if get_year in PI_as_a_long_string: > count += 1 > print("Your birth date occurs %s times in PI!" % (count)) > > if count: >print("Your birth date occurs in PI, I checked, count, "times!") > > > Aren't meaningful variable names great? We should all do > them more often. So the takeaways here are: in the first ("non-counting") sample, there's no need to use a loop, because you're going to quit after the outcome "in" or "not in" right away - there's no loop behavior at all. for year in b: if get_year in b: print("Your year of birth occurs in PI!") break else: print("Your year of birth does not occur in PI.") break for the counting version you could do something that searches for the year repeatedly, avoiding starting over from the beginning so you're actually finding fresh instances, not the same one (hint, hint). There are several ways to do this, including slicing, indexing, etc. Alan's suggestion looks as good as any. Or, if you don't care about overlapping cases, the count method of a string will do just fine: PI_as_a_long_string.count(year) If you're talking about 4-digit year numbers using a Western calendar in digits of PI, the overlap effect seems unlikely to matter - let's say the year is 1919, do we think PI contains the sequence 191919? count would report back one instead of two in that case. In other cases it might matter; count is written specifically to not care about overlaps: "Return the number of (non-overlapping) occurrences" So that's worth keeping in mind when you think about what you need from substrings-in-strings cases. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Validating String contains IP address
Hello what am I going wrong here? i need to validate this is an IP or ask the question again untrust_ip_address = raw_input('\nEnter the untrust IP ''"Example 172.20.2.3/28"'':') while not ipaddress.ip_network untrust_ip_address: untrust_ip_address = raw_input('\nEnter the untrust IP ''"Example 172.20.2.3/28"'':') > On Apr 1, 2017, at 12:29 PM, Alex Kleider wrote: > > On 2017-03-31 18:01, Mats Wichmann wrote: >> On 03/31/2017 06:44 PM, Alex Kleider wrote: >>> On 2017-03-31 16:35, Ed Manning wrote: What's the best way to validate a string contains a IP address Sent from my iPad ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor >>> The re module perhaps? > >> This assumes "an IP address" is the four dotted numbers characteristic >> of IPv4. These days that's a bad assumption unless you're absolutely >> sure an IPv6 address can never appear. Can you? > > Good point! I hadn't considered IPV6 and didn't know about the ipaddress > module. > Live and learn. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Asking about Run python script at Startup
On 04/03/2017 04:20 AM, Alan Gauld via Tutor wrote: > On 03/04/17 05:34, Quang nguyen wrote: > >> I do not know how to run my python 3 script after my PI2 finished startup. > > This might be a better question for a PI forum since it doesn't > seem to have anything directly to do with Python. > >> the easy way to run at startup with escape plan?. > > You will need to define what you want more clearly. > Do you want your script to start as soon as the PI boots > up - before you login? Or do you want it to start as soon > as you login? Or do you want to start it manually as soon > as you finish logging in? > > Also, if you are running a GUI then you must decide if > you want the script to start before or after the GUI > launches. > > (BTW I have no idea what you mean by an escape plan? > Do you mean stopping the script when you hit Escape? > Ctl-C is the usual means of doing that) > Indeed, this doesn't even reveal what the RPi is running, since there are many many options. Presumably it's a linux variant (like Raspbian which I think is still the "default", been a while since I looked); there's plenty of information on "initscripts" (older way) and "systemd" (newer way) if you're looking for a system-level startup script for Linux, as well as ways to run things at a user level (when you log in, or when your desktop environment is instantiated if you run one). Rapsberry PI forums and Linux forums are good places to look. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Count for loops
On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > Sorry. That was stupid of me. The loop does nothing. Let me rewrite the code with some different variable names... >>> with open(file_path) as a: >>> b = a.read() with open (file_path) as PI_text: PI_as_a_long_string = PI_text.read() >>> for year in b: for each_char in PI_as_a_long_string: >>> if get_year in b: >>> count += 1 if get_year in PI_as_a_long_string: count += 1 >>> print("Your birth date occurs %s times in PI!" % (count)) if count: print("Your birth date occurs in PI, I checked, count, "times!") Aren't meaningful variable names great? We should all do them more often. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Count for loops
On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > Sorry. That was stupid of me. The loop does nothing. > On the contrary, the loop does an awful lot, just not what the OP was expecting. >>> with open(file_path) as a: >>> b = a.read() >>> for year in b: >>> if get_year in b: >>> count += 1 -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Count for loops
Sorry. That was stupid of me. The loop does nothing. regards, Sarma. On Mon, Apr 3, 2017 at 8:44 PM, Alan Gauld via Tutor wrote: > On 03/04/17 16:07, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > > Modifying the code as shown below may work. > > I doubt it. > > > with open(file_path) as a: > > b = a.read() > > > > get_year = input("What year were you born? ") > > > > count = 0 > > for year in b: > > Once more I ask, what does this loop do? > > > if get_year in b: > > count += 1 > > print("Your birth date occurs %s times in PI!" % (count)) > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > 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
Re: [Tutor] Count for loops
On 03/04/17 16:07, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > Modifying the code as shown below may work. I doubt it. > with open(file_path) as a: > b = a.read() > > get_year = input("What year were you born? ") > > count = 0 > for year in b: Once more I ask, what does this loop do? > if get_year in b: > count += 1 > print("Your birth date occurs %s times in PI!" % (count)) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Count for loops
Modifying the code as shown below may work. file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt" with open(file_path) as a: b = a.read() get_year = input("What year were you born? ") count = 0 for year in b: if get_year in b: count += 1 print("Your birth date occurs %s times in PI!" % (count)) regards, Sarma. On Mon, Apr 3, 2017 at 8:22 PM, Alan Gauld via Tutor wrote: > On 03/04/17 13:22, Rafael Knuth wrote: > > > with open (file_path) as a: > > b = a.read() > > > > get_year = input("What year were you born? ") > > > > for year in b: > > Can you explain what you think this loop line is doing? > I'm pretty sure it's not doing what you expect. > > > if get_year in b: > > print("Your year of birth occurs in PI!") > > break > > else: > > print("Your year of birth does not occur in PI.") > > break > > > > As a next challenge, I wanted to check how often a person's birth year > > occurs in PI. Unfortunately, I wasn't able to figure out how to use > > the loop count properly. > > What loop count? > There is none, its a for loop, no counter needed. > (OK I just spotted your code below...) > > But there is a count() method on a string object that should help. > > > > count = 0 > > for year in b: > > if get_year in b: > > count += 1 > > else: > > print("Your birth date does not occur in PI.") > > break > > > sum_count = sum(count) > > sum() sums a sequence, but count is an integer. You have been > incrementing it as you go, the final value is already there. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > 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
Re: [Tutor] Count for loops
On 03/04/17 13:22, Rafael Knuth wrote: > with open (file_path) as a: > b = a.read() > > get_year = input("What year were you born? ") > > for year in b: Can you explain what you think this loop line is doing? I'm pretty sure it's not doing what you expect. > if get_year in b: > print("Your year of birth occurs in PI!") > break > else: > print("Your year of birth does not occur in PI.") > break > > As a next challenge, I wanted to check how often a person's birth year > occurs in PI. Unfortunately, I wasn't able to figure out how to use > the loop count properly. What loop count? There is none, its a for loop, no counter needed. (OK I just spotted your code below...) But there is a count() method on a string object that should help. > count = 0 > for year in b: > if get_year in b: > count += 1 > else: > print("Your birth date does not occur in PI.") > break > sum_count = sum(count) sum() sums a sequence, but count is an integer. You have been incrementing it as you go, the final value is already there. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Count for loops
I wrote a program which checks if PI (first one million digits) contains a person's birth year. file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt" with open (file_path) as a: b = a.read() get_year = input("What year were you born? ") for year in b: if get_year in b: print("Your year of birth occurs in PI!") break else: print("Your year of birth does not occur in PI.") break As a next challenge, I wanted to check how often a person's birth year occurs in PI. Unfortunately, I wasn't able to figure out how to use the loop count properly. Can anyone help? Thanks! file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt" with open(file_path) as a: b = a.read() get_year = input("What year were you born? ") count = 0 for year in b: if get_year in b: count += 1 else: print("Your birth date does not occur in PI.") break sum_count = sum(count) print("Your birth date occurs %s times in PI!" % (sum_count)) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Asking about Run python script at Startup
On 03/04/17 05:34, Quang nguyen wrote: > I do not know how to run my python 3 script after my PI2 finished startup. This might be a better question for a PI forum since it doesn't seem to have anything directly to do with Python. > the easy way to run at startup with escape plan?. You will need to define what you want more clearly. Do you want your script to start as soon as the PI boots up - before you login? Or do you want it to start as soon as you login? Or do you want to start it manually as soon as you finish logging in? Also, if you are running a GUI then you must decide if you want the script to start before or after the GUI launches. (BTW I have no idea what you mean by an escape plan? Do you mean stopping the script when you hit Escape? Ctl-C is the usual means of doing that) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Euclidean Distances between Atoms in a Molecule.
Stephen P. Molnar wrote: > I am trying to port a program that I wrote in FORTRAN twenty years ago > into Python 3 and am having a hard time trying to calculate the > Euclidean distance between each atom in the molecule and every other > atom in the molecule. > > Here is a typical table of coordinates: > > >MASS X Y Z > 0 12.011 -3.265636 0.198894 0.090858 > 1 12.011 -1.307161 1.522212 1.003463 > 2 12.011 1.213336 0.948208 -0.033373 > 3 14.007 3.238650 1.041523 1.301322 > 4 12.011 -5.954489 0.650878 0.803379 > 5 12.011 5.654476 0.480066 0.013757 > 6 12.011 6.372043 2.731713 -1.662411 > 7 12.011 7.655753 0.168393 2.096802 > 8 12.011 5.563051 -1.990203 -1.511875 > 91.008 -2.939469 -1.327967 -1.247635 > 10 1.008 -1.460475 2.993912 2.415410 > 11 1.008 1.218042 0.451815 -2.057439 > 12 1.008 -6.255901 2.575035 1.496984 > 13 1.008 -6.560562 -0.695722 2.248982 > 14 1.008 -7.152500 0.390758 -0.864115 > 15 1.008 4.959548 3.061356 -3.139100 > 16 1.008 8.197613 2.429073 -2.588339 > 17 1.008 6.503322 4.471092 -0.543939 > 18 1.008 7.845274 1.892126 3.227577 > 19 1.008 9.512371 -0.273198 1.291080 > 20 1.008 7.147039 -1.365346 3.393778 > 21 1.008 4.191488 -1.928466 -3.057804 > 22 1.008 5.061650 -3.595015 -0.302810 > 23 1.008 7.402586 -2.392148 -2.374554 > > What I need for further calculation is a matrix of the Euclidean > distances between the atoms. > > So far in searching the Python literature I have only managed to confuse > myself and would greatly appreciate any pointers towards a solution. > > Thanks in advance. > Stitched together with heavy use of a search engine: $ cat data.txt MASS X Y Z 0 12.011 -3.265636 0.198894 0.090858 1 12.011 -1.307161 1.522212 1.003463 2 12.011 1.213336 0.948208 -0.033373 3 14.007 3.238650 1.041523 1.301322 4 12.011 -5.954489 0.650878 0.803379 5 12.011 5.654476 0.480066 0.013757 6 12.011 6.372043 2.731713 -1.662411 7 12.011 7.655753 0.168393 2.096802 8 12.011 5.563051 -1.990203 -1.511875 91.008 -2.939469 -1.327967 -1.247635 10 1.008 -1.460475 2.993912 2.415410 11 1.008 1.218042 0.451815 -2.057439 12 1.008 -6.255901 2.575035 1.496984 13 1.008 -6.560562 -0.695722 2.248982 14 1.008 -7.152500 0.390758 -0.864115 15 1.008 4.959548 3.061356 -3.139100 16 1.008 8.197613 2.429073 -2.588339 17 1.008 6.503322 4.471092 -0.543939 18 1.008 7.845274 1.892126 3.227577 19 1.008 9.512371 -0.273198 1.291080 20 1.008 7.147039 -1.365346 3.393778 21 1.008 4.191488 -1.928466 -3.057804 22 1.008 5.061650 -3.595015 -0.302810 23 1.008 7.402586 -2.392148 -2.374554 $ python3 Python 3.4.3 (default, Nov 17 2016, 01:08:31) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import numpy, pandas, scipy.spatial.distance as dist >>> df = pandas.read_table("data.txt", sep=" ", skipinitialspace=True) >>> a = numpy.array(df[["X", "Y", "Z"]]) >>> dist.squareform(dist.pdist(a, "euclidean")) Here's an example with just the first 4 atoms: >>> dist.squareform(dist.pdist(a[:4], "euclidean")) array([[ 0., 2.53370139, 4.54291701, 6.6694065 ], [ 2.53370139, 0., 2.78521357, 4.58084922], [ 4.54291701, 2.78521357, 0., 2.42734737], [ 6.6694065 , 4.58084922, 2.42734737, 0.]]) See https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html There may be a way to do this with pandas.pivot_table(), but I didn't manage to find that. As Alan says, this is not the appropriate forum for the topic you are belabouring. Work your way through a Python tutorial to pick up the basics (we can help you with this), then go straight to where the (numpy/scipy) experts are. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Asking about Run python script at Startup
Hi guys, I do not know how to run my python 3 script after my PI2 finished startup. I searched about it on some websites, and I feel so confused. Anyone know the easy way to run at startup with escape plan?. I am afraid that it will be a looping trap if I do not have escape plan :) Thank you ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Euclidean Distances between Atoms in a Molecule.
Dr. Molnar: This might be your solution: >>> import numpy >>> a = numpy.array((-3.265636, 0.198894, 0.090858)) >>> b = numpy.array((-1.307161, 1.522212, 1.003463)) >>> dist = numpy.linalg.norm(a-b) >>> dist 2.5337013913983633 >>> Qiao Qiao > On Apr 2, 2017, at 1:41 PM, Stephen P. Molnar wrote: > > I am trying to port a program that I wrote in FORTRAN twenty years ago into > Python 3 and am having a hard time trying to calculate the Euclidean distance > between each atom in the molecule and every other atom in the molecule. > > Here is a typical table of coordinates: > > > MASS X Y Z > 0 12.011 -3.265636 0.198894 0.090858 > 1 12.011 -1.307161 1.522212 1.003463 > 2 12.011 1.213336 0.948208 -0.033373 > 3 14.007 3.238650 1.041523 1.301322 > 4 12.011 -5.954489 0.650878 0.803379 > 5 12.011 5.654476 0.480066 0.013757 > 6 12.011 6.372043 2.731713 -1.662411 > 7 12.011 7.655753 0.168393 2.096802 > 8 12.011 5.563051 -1.990203 -1.511875 > 91.008 -2.939469 -1.327967 -1.247635 > 10 1.008 -1.460475 2.993912 2.415410 > 11 1.008 1.218042 0.451815 -2.057439 > 12 1.008 -6.255901 2.575035 1.496984 > 13 1.008 -6.560562 -0.695722 2.248982 > 14 1.008 -7.152500 0.390758 -0.864115 > 15 1.008 4.959548 3.061356 -3.139100 > 16 1.008 8.197613 2.429073 -2.588339 > 17 1.008 6.503322 4.471092 -0.543939 > 18 1.008 7.845274 1.892126 3.227577 > 19 1.008 9.512371 -0.273198 1.291080 > 20 1.008 7.147039 -1.365346 3.393778 > 21 1.008 4.191488 -1.928466 -3.057804 > 22 1.008 5.061650 -3.595015 -0.302810 > 23 1.008 7.402586 -2.392148 -2.374554 > > What I need for further calculation is a matrix of the Euclidean distances > between the atoms. > > So far in searching the Python literature I have only managed to confuse > myself and would greatly appreciate any pointers towards a solution. > > Thanks in advance. > > -- > Stephen P. Molnar, Ph.D. Life is a fuzzy set > www.molecular-modeling.netStochastic and multivariate > (614)312-7528 (c) > Skype: smolnar1 > ___ > 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
Re: [Tutor] Euclidean Distances between Atoms in a Molecule.
Hi Stephen- The `scipy.spatial.distance` module (part of the SciPy package) contains what you will need -- specifically, the `scipy.spatial.distance.pdist` function, which takes a matrix of m observations in n-dimensional space, and returns a condensed distance matrix as described in https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html . This condensed distance matrix can be expanded into a full m by m matrix with `scipy.spatial.distance.squareform` as follows: """ In [1]: import pandas as pd In [2]: from io import StringIO In [3]: s = StringIO(''' ...: MASS X Y Z ...: 0 12.011 -3.265636 0.198894 0.090858 ...: 1 12.011 -1.307161 1.522212 1.003463 ...: 2 12.011 1.213336 0.948208 -0.033373 ...: 3 14.007 3.238650 1.041523 1.301322 ...: 4 12.011 -5.954489 0.650878 0.803379 ...: 5 12.011 5.654476 0.480066 0.013757 ...: 6 12.011 6.372043 2.731713 -1.662411 ...: 7 12.011 7.655753 0.168393 2.096802 ...: 8 12.011 5.563051 -1.990203 -1.511875 ...: 91.008 -2.939469 -1.327967 -1.247635 ...: 10 1.008 -1.460475 2.993912 2.415410 ...: 11 1.008 1.218042 0.451815 -2.057439 ...: 12 1.008 -6.255901 2.575035 1.496984 ...: 13 1.008 -6.560562 -0.695722 2.248982 ...: 14 1.008 -7.152500 0.390758 -0.864115 ...: 15 1.008 4.959548 3.061356 -3.139100 ...: 16 1.008 8.197613 2.429073 -2.588339 ...: 17 1.008 6.503322 4.471092 -0.543939 ...: 18 1.008 7.845274 1.892126 3.227577 ...: 19 1.008 9.512371 -0.273198 1.291080 ...: 20 1.008 7.147039 -1.365346 3.393778 ...: 21 1.008 4.191488 -1.928466 -3.057804 ...: 22 1.008 5.061650 -3.595015 -0.302810 ...: 23 1.008 7.402586 -2.392148 -2.374554 ...: ''') In [4]: d = pd.read_table(s, sep='\\s+', index_col=0) In [5]: d.head() Out[5]: MASS X Y Z 0 12.011 -3.265636 0.198894 0.090858 1 12.011 -1.307161 1.522212 1.003463 2 12.011 1.213336 0.948208 -0.033373 3 14.007 3.238650 1.041523 1.301322 4 12.011 -5.954489 0.650878 0.803379 In [6]: points = d.loc[:, ['X', 'Y', 'Z']] In [7]: import scipy.spatial.distance In [8]: distances = scipy.spatial.distance.pdist(points) In [9]: distances.shape Out[9]: (276,) In [10]: distances Out[10]: array([ 2.53370139, 4.54291701, 6.6694065 , 2.81813878, 8.92487537, 10.11800281, 11.10411993, 9.23615791, 2.05651475, 4.0588513 , 4.97820424, 4.0700026 , 4.03910564, 4.0070559 , 9.28870116, 11.98156386, 10.68116021, 11.66869152, 12.84293061, 11.03539433, 8.36949409, 9.15928011, 11.25178722, 2.78521357, 4.58084922, 4.73253781, 7.10844399, 8.21826934, 9.13028167, 8.11565138, 3.98188296, 2.04523847, In [11]: scipy.spatial.distance.squareform(distances) Out[11]: array([[ 0., 2.53370139, 4.54291701, 6.6694065 , 2.81813878, 8.92487537, 10.11800281, 11.10411993, 9.23615791, 2.05651475, 4.0588513 , 4.97820424, 4.0700026 , 4.03910564, 4.0070559 , 9.28870116, 11.98156386, 10.68116021, 11.66869152, 12.84293061, 11.03539433, 8.36949409, 9.15928011, 11.25178722], [ 2.53370139, 0., 2.78521357, 4.58084922, 4.73253781, 7.10844399, 8.21826934, 9.13028167, 8.11565138, 3.98188296, 2.04523847, 4.10992956, 5.08350537, 5.83684597, 6.2398737 , 7.66820932, 10.2011846 , 8.49081803, 9.42605887, 10.9712576 , 9.24797787, 7.65742836, 8.27370019, 10.12881562], """ MMR... On 2017-04-02 13:41, Stephen P. Molnar wrote: > I am trying to port a program that I wrote in FORTRAN twenty years ago > into Python 3 and am having a hard time trying to calculate the > Euclidean distance between each atom in the molecule and every other > atom in the molecule. > > Here is a typical table of coordinates: > > > MASS X Y Z > 0 12.011 -3.265636 0.198894 0.090858 > 1 12.011 -1.307161 1.522212 1.003463 > 2 12.011 1.213336 0.948208 -0.033373 > 3 14.007 3.238650 1.041523 1.301322 > 4 12.011 -5.954489 0.650878 0.803379 > 5 12.011 5.654476 0.480066 0.013757 > 6 12.011 6.372043 2.731713 -1.662411 > 7 12.011 7.655753 0.168393 2.096802 > 8 12.011 5.563051 -1.990203 -1.511875 > 91.008 -2.939469 -1.327967 -1.247635 > 10 1.008 -1.460475 2.993912 2.415410 > 11 1.008 1.218042 0.451815 -2.057439 > 12 1.008 -6.255901 2.575035 1.496984 > 13 1.008 -6.560562 -0.695722 2.248982 > 14 1.008 -7.152500 0.390758 -0.864115 > 15 1.008 4.959548 3.061356 -3.139100 > 16 1.008 8.197613 2.429073 -2.588339 > 17 1.008 6.503322 4.471092 -0.543939 > 18 1.008 7.845274 1.892126 3.227577 > 19 1.008 9.512371 -0.273198 1.291080 > 20 1.008 7.147039 -1.365346 3.393778 > 21 1