python应用题

python应用题,第1张

有一个巧妙的解法

for i in zip(grid):
  print ''join(i)

zip(grid) 将每一列并在一起,

print ''join(i) 列表中的元素合并,然后输出,如果是Python3 这句改成print(''join(i))

比较笨的方法可以写两重循环,组个字符输出。


if source is None: if target is None: ## Find paths between all pairs if weight is None: paths=nxall_pairs_shortest_path(G) else: paths=nxall_pairs_dijkstra_path(G,weight=weight) else: ## Find paths from all nodes co-accessible to the target directed = Gis_directed() if directed: Greverse(copy=False) if weight is None: paths=nxsingle_source_shortest_path(G,target) else: paths=nxsingle_source_dijkstra_path(G,target,weight=weight) # Now flip the paths so they go from a source to the target for target in paths: paths[target] = list(reversed(paths[target])) if directed: Greverse(copy=False) else: if target is None: ## Find paths to all nodes accessible from the source if weight is None: paths=nxsingle_source_shortest_path(G,source) else: paths=nxsingle_source_dijkstra_path(G,source,weight=weight) else: ## Find shortest source-target path if weight is None: paths=nxbidirectional_shortest_path(G,source,target) else: paths=nxdijkstra_path(G,source,target,weight)

直接用勾股定理。
x1,y1=map(int,input()split(','))
x2,y2=map(int,input()split(','))
print('{:2f}'format((x1-x2)2+(y1-y2)2)05))

欧式距离python实现代码:

import numpy as np

x=nprandomrandom(10)

y=nprandomrandom(10)

#方法一:根据公式求解

d1=npsqrt(npsum(npsquare(x-y)))

#方法二:根据scipy库求解

from scipyspatialdistance import pdist

X=npvstack([x,y])

d2=pdist(X)

曼哈顿距离python实现:

import numpy as np

x=nprandomrandom(10)

y=nprandomrandom(10)

#方法一:根据公式求解

d1=npsum(npabs(x-y))

#方法二:根据scipy库求解

from scipyspatialdistance import pdist

X=npvstack([x,y])

d2=pdist(X,'cityblock')

程序运行结果:

扩展资料:

C语言实现:

#include "pchh"

#define  _CRT_SECURE_NO_WARNINGS

#include<stdioh>

#include<mathh>

void main()

{

float x1, x2, y1, y2;

printf("请输入一组数据:");

while (~scanf("%f%f%f%f", &x1, &y1, &x2, &y2))//开始读取输入的数,知道文件结束。

{

printf("两点间的距离为:%2f\n", sqrt((x1 - x2)(x1 - x2) + (y1 - y2)(y1 - y2)));

printf("请输入一组数据:");

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/10552326.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-09
下一篇 2023-05-09

发表评论

登录后才能评论

评论列表(0条)

保存