我是编程新手,目前参加CSC 110课程.我们的任务是创建一组函数,使用给定的一些数据执行各种操作.我已经把所有数据都放到了字典中,但是我在获取我想要的数据时遇到了一些麻烦.
这是我的问题:
我有一个字典,存储了一堆国家,后面是一个包含人口和GDP的列表.格式化这样的东西
{'country': [population, GDP], ...}
Run Code Online (Sandbox Code Playgroud)
我的任务是遍历这个并找到人口或GDP最高的国家然后打印:
'The country with the highest population is ' + highCountry+\
' with a population of ' + format(highPop, ',.0f')+'.')
Run Code Online (Sandbox Code Playgroud)
为了做到这一点,我写了这个函数(这个函数专门用于最高人口,但它们看起来都是一样的).
def highestPop(worldInfo):
highPop = worldInfo[next(iter(worldInfo))][0] #Grabs first countries Population
highCountry = next(iter(worldInfo))#Grabs first country in worldInfo
for k,v in worldInfo.items():
if v[0] > highPop:
highPop = v[0]
highCountry = k
return highPop,highCountry
Run Code Online (Sandbox Code Playgroud)
虽然这对我有用,但我认为有一种更简单的方法可以做到这一点.另外,我不是100%确定如何[next(iter(worldInfo))]
运作.这只是抓住它看到的第一个值吗?
感谢您的帮助!
编辑:对不起我想我不清楚.我需要通过国家人口以及国家名称.所以我可以在我的主要功能中打印它们.
我正在尝试在 docker 容器内运行用 Golang 编写的 HTTP 服务器,但连接不断被拒绝。一切都在我的 Windows 10 计算机上运行的 Ubuntu 20.04 Server VM 内运行。
Go服务器代码:
package main
import "github.com/lkelly93/scheduler/internal/server"
func main() {
server := server.NewHTTPServer()
server.Start(3000)
}
Run Code Online (Sandbox Code Playgroud)
package server
import (
"context"
"fmt"
"net/http"
)
type HTTPServer interface {
Start(port int) error
Stop() error
}
func NewHTTPServer() HTTPServer {
return &httpServer{}
}
type httpServer struct {
server *http.Server
}
func (server *httpServer) Start(port int) error {
serveMux := newServeMux()
server.server = &http.Server {
Addr: fmt.Sprintf(":%d", port),
Handler: serveMux,
} …
Run Code Online (Sandbox Code Playgroud)