質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1798閲覧

Python3 KeyErrorの対処法

h-matsudo

総合スコア11

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/10/19 07:04

現在
%matplotlib inline
import matplotlib,pyplot as plt
import networkx as nx
import numpy as np

でダイクストラ法を写経しておりますが、教科書通り入力すると

KeyError Traceback (most recent call last)
<ipython-input-18-db35ddf27575> in <module>()
19 T = set({})
20 for v in nodes:
---> 21 sp = nx.dijkstra_path(G,s,v)
22 T=T.union({tuple(x) for x in np.array([sp[:-1],sp[1:]]).T})
23 T = list(T)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\networkx\algorithms\shortest_paths\weighted.py in dijkstra_path(G, source, target, weight)
158 """
159 (length, path) = single_source_dijkstra(G, source, target=target,
--> 160 weight=weight)
161 return path
162

~\AppData\Local\Continuum\anaconda3\lib\site-packages\networkx\algorithms\shortest_paths\weighted.py in single_source_dijkstra(G, source, target, cutoff, weight)
450 """
451 return multi_source_dijkstra(G, {source}, cutoff=cutoff, target=target,
--> 452 weight=weight)
453
454

~\AppData\Local\Continuum\anaconda3\lib\site-packages\networkx\algorithms\shortest_paths\weighted.py in multi_source_dijkstra(G, sources, target, cutoff, weight)
704 paths = {source: [source] for source in sources} # dictionary of paths
705 dist = _dijkstra_multisource(G, sources, weight, paths=paths,
--> 706 cutoff=cutoff, target=target)
707 if target is None:
708 return (dist, paths)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\networkx\algorithms\shortest_paths\weighted.py in _dijkstra_multisource(G, sources, weight, pred, paths, cutoff, target)
792 if v == target:
793 break
--> 794 for u, e in G_succ[v].items():
795 cost = weight(v, u, e)
796 if cost is None:

KeyError: 'v1'

となってしまいます。
Qiitaでは辞書に登録されていないためのエラーとの記載があり
対処法を試してみましたがどれも解決には至りませんでした。

どなたか対処法を教えて頂けますでしょうか。
宜しくお願い申し上げます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayataka2049

2018/10/19 07:05

質問を再編集し、出典を示した上でコードを全文掲載してください。コードは<code> ボタンで挿入できるコードブロックの中に入れてください
y_waiwai

2018/10/19 07:18

コードは、行番号なしのそのままを、<code>ボタン、’’’の枠の中に貼り付けてください
h-matsudo

2018/10/19 07:32

質問し直しました。
guest

回答1

0

自己解決

質問一本化のため解決済みに致します。

投稿2018/10/19 07:53

h-matsudo

総合スコア11

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayataka2049

2018/10/19 07:55

質問文は再編集できるので、もう一つ新しく質問する必要はなかったです。次回からはその機能を活用してください
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問