简体   繁体   English

我怎么知道下面的代码是否正确?

[英]How do I know if the code below is correct?

From my previous request, I created an empty list (clean_mag) and then loop through the mags list, and append the positive numbers to the clean_mag(list) because I realized that the negative numbers was the reason why I was getting a ValueError in my previous code.根据我之前的请求,我创建了一个空列表(clean_mag),然后遍历 mags 列表,append 将正数添加到 clean_mag(list),因为我意识到负数是我在我的以前的代码。 Although the code worked, but I would like to know if is correct, but as a self-learner, I do not know how else to cross check my code for validity.虽然代码有效,但我想知道是否正确,但作为自学者,我不知道如何交叉检查我的代码的有效性。
How do I know that I am not missing anything out?我怎么知道我没有遗漏任何东西? I would like to know if there's a better way to go through the mag list, and then work with the positive numbers without allowing the negative numbers to blow up the code.我想知道是否有更好的方法通过 mag 列表来 go,然后使用正数而不让负数破坏代码。 I thought about using try/except but it didn't work.我考虑过使用 try/except 但它没有用。

import json
from plotly.graph_objs import Scattergeo, Layout
from plotly import offline


# Get a JSON file.
filename = 'earthquake_data/seven_days_earthquake.json'
with open(filename, encoding='utf-8') as f:
    eq_data = json.load(f)

# Create a readable file from the loaded json file above.

readable_file = 'earthquake_data/readable_eq_data.json'
with open(readable_file, 'w') as f:
    json.dump(eq_data, f, indent=4)

# Exploring the structure of the data.
title = eq_data["metadata"]["title"]
all_eq_dicts = eq_data["features"]

mags, lons,  = [], []
lats, hover_texts = [], []

for eq_dict in all_eq_dicts:
    mags.append(eq_dict["properties"]["mag"])
    lons.append(eq_dict["geometry"]["coordinates"][0])
    lats.append(eq_dict["geometry"]["coordinates"][1])
    hover_texts.append(eq_dict["properties"]["title"])

clean_mags = []

for item in mags:
    if item < 0:
        pass
    else:
        clean_mags.append(item)

# Map the earthquakes.
data = [{
    'type': 'scattergeo',
    'lon': lons,
    'lat': lats,
    'text': hover_texts,

    # Outline marks in the map
    'marker': {
        'size':[5*mag for mag in clean_mags],
        'color': mags,
        'colorscale': 'Ylorrd',
        'reversescale': True,
        'colorbar': {'title': 'Magnitude'},
    },

}]

# One-week Earthquake data output.
my_layout = Layout(title = f'{title}')
fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='sevenday_quakes.html')

Since you did not present any data, I got similar data from here .由于您没有提供任何数据,我从 这里得到了类似的数据。 If the magnitude is greater than or equal to 0, the latitude, longitude, and name will also be listed.如果幅度大于等于0,也会列出纬度、经度和名称。 This answer may differ from the data the questioner has, and the method of answering may not be valid.此答案可能与提问者所拥有的数据不同,并且回答的方法可能无效。 If this is the case, please comment.如果是这种情况,请发表评论。

from urllib import request
import json

url='https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson'
with request.urlopen(url) as f:
    eq_data = json.load(f)

from urllib import request
import json

url='https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson'
with request.urlopen(url) as f:
    eq_data = json.load(f)

import plotly.graph_objs as go

fig = go.Figure()

fig.add_trace(go.Scattergeo(
    lon=lons,
    lat=lats,
    text=hover_texts,
    marker=dict(
        size=[x*3 for x in mags],
        color=mags,
        colorscale='Ylorrd',
        reversescale=False,
        colorbar=dict(title='Magnitude'),
    )
))

fig.update_layout(
    autosize=True,
    height=600,
    title_text='All Earthquakes(Last 7 days)',
    showlegend=False,
    geo = dict(
            landcolor='rgb(217, 217, 217)',
        )
    )

fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在下面的代码中实现 DFS? - How do I implement DFS in the code below? 如何添加过滤器以便我可以对向经理报告的员工分组? 请指导我。 也让我知道下面的代码是否正确 - How to add filters so that I can group employees reporting to manager ?? Please guide me. Also let me know if the below code is correct or not 以下“删除列表中的重复项”代码中的问题是什么。 我知道我们可以使用 append() 来做同样的事情 - What is the problem in the below code for "removing duplicates in list". I know we can use append() to do the same thing 我不知道如何混合我的 csv 代码。 我会在下面告诉你我的意思 - I dont know how to blend my csv code. Ill show you what I mean below 如何删除下面python代码中的括号? - How do I remove the parentheses in the python code below? 如何将用户输入合并到下面的python代码中? - How do I incorporate user input into the python code below? 如何将下面的示例代码转换为接受多个输入? - How do I turn the sample code below into accepting multiple inputs? 在将Fabric用于以下代码时,如何添加超时? - How do I add timeout while using fabric for below code? 如何解决 python 中的 KeyError? 检查下面的代码 - How do i resolve a KeyError in python? check the code below 如何使用字典理解编写以下代码 - How do I write below code using dictionary comprehension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM