如何打印 python 列表中的特定行
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 循环只是迭代数组的不同方式。
除此之外,我认为您的代码似乎有问题,因为
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)
我有一个名为 '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 循环只是迭代数组的不同方式。
除此之外,我认为您的代码似乎有问题,因为 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)