简体   繁体   English

从字符串列表创建一个 python 树

[英]create a python Tree from a list of strings

Im using the newest version of python on a windows10 machine.我在 windows10 机器上使用最新版本的 python。 I have a list of 100k routes that semi trucks take.我有一份半卡车采用的 10 万条路线的清单。 Each route only has 1 single stop.每条路线只有 1 个单站。 Each route originates at cityA and travels to cityB.每条路线都起源于城市A,然后到达城市B。 it is structured similarly to this:它的结构与此类似:

list_single_stop_routes = [
'cityA to cityB',
'ohio to cali',
'penn to texas',
'cali to tenn',
'tenn to ohio']

What i want to do, is create a list of 'extended routes' where each extended route has some arbitrary number of stops that goes from city-to-city.我想做的是创建一个“扩展路线”列表,其中每条扩展路线都有一些从城市到城市的任意数量的站点。

I started by getting a list of every cityA from each route, and using it as my originating location.我首先从每条路线获取每个城市 A 的列表,并将其用作我的起始位置。 then i took the corresponding cityB, then iterated through each single-stop route where my current cityB is the new routes cityA.然后我取了相应的cityB,然后遍历每条单站路线,其中我当前的cityB是新的路线cityA。 So lets say my originating location is ohio, then cityB is indiana.因此,假设我的出发地是俄亥俄州,那么 cityB 是印第安纳州。 now i want to iterate through each route to find every route where indiana is the originating location (cityA), and i might find a route that says 'indiana to texas'.现在我想遍历每条路线以找到印第安纳州是始发地(cityA)的每条路线,我可能会找到一条写着“印第安纳州到德克萨斯州”的路线。 So my structure so far will say 'ohio to indiana to texas' and so-on until there are (potentially) no more connections to be made.因此,到目前为止,我的结构会说“俄亥俄州到印第安纳州到德克萨斯州”等等,直到(可能)不再建立联系。

I tried creating dictionaries and lists to help me structure the output, but i cant seem to figure out exactly what will work.我尝试创建字典和列表来帮助我构建输出,但我似乎无法弄清楚究竟什么会起作用。 Please keep in mind that it is a requirement to preserve the correct ordering of every single route.请记住,必须保持每条路线的正确顺序。 I then started to consider some sort of data structure, maybe similar to a tree?然后我开始考虑某种数据结构,可能类似于树? Ultimately i want a list like so:最终我想要一个这样的列表:

list_extended_routes = [
    "ohio to cali to tenn to texas to flor to nevada to wisconsin to newyork",
    "missou to texas to wisconsin to texas to ohio",]

Hopefully someone can help lead me in the right direction?希望有人可以帮助我朝着正确的方向前进? Thank you!谢谢!

This is inherently a graph problem, so best might be to use a graph library such as networkx .这本质上是一个图形问题,因此最好使用诸如networkx类的图形库。

The graph is the following:图表如下:

在此处输入图像描述

You can construct it using:您可以使用以下方法构建它:

list_single_stop_routes = [
'cityA to cityB',
'ohio to cali',
'penn to texas',
'cali to tenn',
'tenn to ohio',
'ohio to indiana'
]

import networkx as nx

G = nx.from_edgelist(s.split(' to ') for s in list_single_stop_routes)

Then it's easy to find all the routes (path) between 2 cities (nodes):然后很容易找到2个城市(节点)之间的所有路线(路径):

list(nx.all_simple_paths(G, source='indiana', target='tenn'))

output:输出:

[['indiana', 'ohio', 'cali', 'tenn'], ['indiana', 'ohio', 'tenn']]

directed graph (=specific direction)有向图(=特定方向)

if you want a directed graph, use如果你想要一个有向图,使用

G = nx.from_edgelist((s.split(' to ') for s in list_single_stop_routes),
                     create_using=nx.DiGraph)

在此处输入图像描述

Now there is no route from indiana to tenn现在没有从印第安纳州到田纳西州的路线

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM