0

I have a list called 'DistInt' that contains values of Intensity with corresponding Distances for an earthquake event. Is there a way I can just print a specific row within list?

import math
import json

# Open JSON file
f = open("Path_to.geojson")

# Returns JSON object as a dictionary
data = json.load(f)

for feature in data['features']:
    ml = float(feature['properties']['magnitude'])
    h = float(feature['properties']['depth'])
    i = 0

# Formula for working out Distances for MMI
    Io = 1.5 * (ml - 1.7 * 0.4343 * math.log(h) + 1.4)

    for i in range(1, 13, 1):
        Iso = float(i)
        a = (Io - Iso) / (1.8 * 0.4343)
        d = math.exp(a)
        d = d - 1
        if d <= 0:
            d = 0
        else:
            d = math.sqrt(h * h * d)
            DistInt = [Iso, d]
            print(DistInt)

Would print the DistInt list:

[1.0, 609.1896122140013]
[2.0, 321.0121765154287]
[3.0, 168.69332169329735]
[4.0, 87.7587868508665]
[5.0, 43.88709626561051]
[6.0, 17.859906969392682]

I would like to just print, for example, the row - [2.0, 321.0121765154287]

LiamHems
  • 121
  • 7
  • Can you explain the criterio to choose that? or want just typed coded – Wonka Nov 27 '20 at 13:28
  • Essentially, I would like to use the values within each row for another part of the script further on. The distance and intensity values would be used to create buffers within QGIS (I have a script that can do this). So, using the example above, 2.0, 321.0121765154287 would create a 321km buffer for intensity of 2.0. – LiamHems Nov 27 '20 at 13:33
  • so when you are doing, print(DistInt) you have the row, you are doing it multiple times inside a loop, manage your data – Wonka Nov 27 '20 at 13:35
  • When I use print(DistInt), it prints 6 rows that contains values from 1.0-6.0 intensity. Is there a code I can use to print(DistInt) for just the first row or the second row, for example? Hope this make sense – LiamHems Nov 27 '20 at 13:38
  • NO! try this, add a new print below print(DistInt) , print("-"*8) and check the output, if you see multiples times the print line -------- you are wrong – Wonka Nov 27 '20 at 13:43
  • Did you checked it? – Wonka Nov 27 '20 at 14:14
  • Yes, it prints out 6 rows of 8 dashes but I would just like to just show the intensity with the corresponding distance for just one example row. Not too sure what these dashes have anything to do with my question? Thanks – LiamHems Nov 30 '20 at 13:52

1 Answers1

1

I guess what you want to do is something like this:

DistIntArray = []

for i in range(1, 13, 1):
Iso = float(i)
a = (Io - Iso) / (1.8 * 0.4343)
d = math.exp(a)
d = d - 1
if d <= 0:
    d = 0
else:
    d = math.sqrt(h * h * d)
    DistInt = [Iso, d]
    print(DistInt)
DistIntArray.append(DistInt)
    
print("the first element of array: ", DistIntArray[0])
print("the second element of array: ", DistIntArray[1])

for i in range(0,len(DistIntArray)):
    print("An element of DistIntArray",DistIntArray[i])


for aDistInt in DistIntArray:
    print("Getting an element of DistIntArray",aDistInt)

I just created an array and added the calculated DistInt tuple in each iteration of the first for loop. This way I can get any element of the calculated DistInt variables.

Here, DistIntArray is an array of array because the variable DistInt is a tuple which is an array. The for loops are just different ways of iterating over arrays.

Besides this, I think your code seems to be buggy since the DistInt = [Iso, d] is indented with the else block. It means it will do the assignment only in the else block. I guess this line and the print line should be written like this:

if d <= 0:
    d = 0
else:
    d = math.sqrt(h * h * d)
DistInt = [Iso, d]
print(DistInt)
DistIntArray.append(DistInt)
Dharman
  • 30,962
  • 25
  • 85
  • 135
gne
  • 38
  • 6
  • Thank you for your response. I can seem to get one element (12.0) from the calculated DistInt variables using DistIntArray[0] but when I put in any number above 0, it comes up with the error - print("the first element of array: ", DistIntArray[1]) IndexError: list index out of range – LiamHems Dec 02 '20 at 12:03