Python Forum
Using an Array in a While Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using an Array in a While Loop
#6
sorry, maybe I don't understand fully what you try to achieve. My understanding was you want to calculate the gravity impact of the sun over each planet.
In your set-up you had the sun in each of the different lists (mass, posit, etc.), thus you had to exclude it when loop over lists to calculate the gravity impact.

In my code I do just what you do in your code, but in more straightforward (I hope) way. Planets holds only the planets and the solar_system dict holds only the visual representation (sphere objects) of sun and planets. Note that you need to keep position info held in both structures in sync. In more advanced approach, using class, this can be done automatically.

I loop over all planets (line 27) and calculate the impact of the sun over each planet and update the data for planet position in planets tuple (lines 29-34) and in solar_system dict (line 35).
If you elaborate further on your task/model  would suggest changes. Do you want to calculate also the impact between the planets (all or only bigger ones toward smaller ones)?

as to the tuple - it's more or less same as list, but immutable, i.e. you cannot change the tuple once created.

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> l = [1,2,3] # list
>>> t = (1,2,3) # tuple
>>> l[1]
2
>>> t[1]
2
>>> l.insert(0,0) # insert 0 in the beginning of the list
>>> l
[0, 1, 2, 3]
>>> t.insert(0,0) # try to insert 0 in the begining of the tuple
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'insert'
in my code I have a bit more complex structure - tuple of tuples, i.e. each element itself is a tuple with 2 elements - str (the planet name) and dict (the planet data).
Note that while tuple is immutable, you can change the mutable elements, like dict within the tuple:

>>> a= (('a',{'r':1,'m':2}),('b',{'r':3,'m':4})) #tuple of tuples. each element is tuple of string and dict like planets in our case
>>> a[1]
('b', {'r': 3, 'm': 4})
>>> x,y = a[1] # unpack element with index 1
>>> y['m']=5 # change element in the dict
>>> a[1]
('b', {'r': 3, 'm': 5})
>>> a
(('a', {'r': 1, 'm': 2}), ('b', {'r': 3, 'm': 5}))
You can access tuple elements by index (like with list) so print planets[2] would print the tuple element with index 2 (0-based), so that's the tuple for the earth.
using name, planet = planets[2] you can unpack the tuple element with index 2 into its own elements, i.e. name would hold the str and planet would hold the dict.
Reply


Messages In This Thread
Using an Array in a While Loop - by JakeWitten - Jun-13-2017, 01:28 AM
RE: Using an Array in a While Loop - by Skaperen - Jun-13-2017, 04:47 AM
RE: Using an Array in a While Loop - by j.crater - Jun-13-2017, 06:01 AM
RE: Using an Array in a While Loop - by buran - Jun-13-2017, 08:48 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-13-2017, 10:24 PM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 04:12 AM
RE: Using an Array in a While Loop - by Skaperen - Jun-14-2017, 04:45 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 05:06 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 05:18 AM
RE: Using an Array in a While Loop - by Skaperen - Jun-14-2017, 05:29 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 05:46 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 05:52 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 07:05 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 08:23 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 08:44 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 10:03 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 10:36 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 10:40 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 10:58 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 11:07 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 11:17 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 11:45 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 03:17 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020