[英]How to print a specific row from a list in python
我有一個名為“DistInt”的列表,其中包含強度值和地震事件的相應距離。 有沒有辦法只打印列表中的特定行?
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)
將打印 DistInt 列表:
[1.0, 609.1896122140013]
[2.0, 321.0121765154287]
[3.0, 168.69332169329735]
[4.0, 87.7587868508665]
[5.0, 43.88709626561051]
[6.0, 17.859906969392682]
例如,我想打印行 - [2.0, 321.0121765154287]
我想你想做的是這樣的:
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)
我剛剛創建了一個數組,並在第一個 for 循環的每次迭代中添加了計算的 DistInt 元組。 這樣我可以得到計算的 DistInt 變量的任何元素。
這里, DistIntArray 是一個數組數組,因為變量 DistInt 是一個元組,它是一個數組。 for 循環只是迭代 arrays 的不同方式。
除此之外,我認為您的代碼似乎有問題,因為 DistInt = [Iso, d] 與 else 塊縮進。 這意味着它將僅在 else 塊中進行分配。 我猜這一行和打印行應該是這樣寫的:
if d <= 0:
d = 0
else:
d = math.sqrt(h * h * d)
DistInt = [Iso, d]
print(DistInt)
DistIntArray.append(DistInt)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.