Python 从相似的列表创建一个有序的列表

Python 从相似的列表创建一个有序的列表,python,list,Python,List,我有许多公共汽车站的名单。 每个列表都表示公共汽车在一次旅程中的停靠站。 旅程路线相同,但公共汽车可在任何一站停车 我想按顺序创建所有站点的完整列表 下面是三个巴士旅行示例 bus_1 = ["5171","1337","1341","1350","1352","1357","320","278","10","15","215","218","1623","1624","7347"] bus_2 = ["5171","2976","2979","2981","2991","2992","132

我有许多公共汽车站的名单。 每个列表都表示公共汽车在一次旅程中的停靠站。 旅程路线相同,但公共汽车可在任何一站停车

我想按顺序创建所有站点的完整列表

下面是三个巴士旅行示例

bus_1 = ["5171","1337","1341","1350","1352","1357","320","278","10","15","215","218","1623","1624","7347"]

bus_2 = ["5171","2976","2979","2981","2991","2992","1326","1327","1329","1331","1336","1337","1339","1340","1342","1345","1350","1354","1357","1359","320","278","12","15","17","21","205","215","216","218","1624","1626","1627","7347"]

bus_3 = ["5171","2977" "2978","2991","1325","1326","1327","1329","1330","1331","1332","1333","1337","1340","1341","1342","1344","1345","1347","1348","1352","1353","1354","1355","1357","1359","320","278","10","12","14","15","17","19","21","85","204","215","218","219","220","1622","1623","1624","1625","1626","1627","7347"]

这可能吗?

我可以靠近,但我认为你需要更多的路线才能获得完整的路线。它无法添加的某些值没有足够的类似周围元素,但这可能会让您走上正确的道路

bus_1 = ["5171","1337","1341","1350","1352","1357","320","278","10","15","215","218","1623","1624","7347"]   
bus_2 = ["5171","2976","2979","2981","2991","2992","1326","1327","1329","1331","1336","1337","1339","1340","1342","1345","1350","1354","1357","1359","320","278","12","15","17","21","205","215","216","218","1624","1626","1627","7347"]
bus_3 = ["5171","2977", "2978","2991","1325","1326","1327","1329","1330","1331","1332","1333","1337","1340","1341","1342","1344","1345","1347","1348","1352","1353","1354","1355","1357","1359","320","278","10","12","14","15","17","19","21","85","204","215","218","219","220","1622","1623","1624","1625","1626","1627","7347"]

buses=[bus_1,bus_2,bus_3]

added=set()
final=bus_1[:]
temp=bus_1+bus_2+bus_3
i=0
x=0
while set(final) != set(temp):
  # get 2 elements which we'll see if they're in final route
  if x<len(buses):
    t=buses[x][i:i+2]
  else:
    t=final[i:i+2]
  try:
    a=final.index(t[0])
    b=final.index(t[1])
  except:
    a=b=-1
  # check if in final and not next to each other, and not added yet
  for q,bus in enumerate(buses):
    if x!=q and t[0] in bus and t[1] in bus and a+1==b and (t[0],t[1]) not in added:
      u=bus.index(t[0])
      v=bus.index(t[1])
      final[a:b]=bus[u:v]

      added.add((t[0],t[1]))
      i=0
      x=0

  if x<len(buses):
    for q,bus in enumerate(buses):
      if q==x and i+2<len(bus):
        i+=1
      elif q==x and i+2==len(bus):
        x+=1
        i=0
  elif x==len(buses)+1 and i+2<len(final):
    i+=1
  else:
    x+=1
    i=0

  if x>len(buses)+1:
    break

print("final route(incomplete)")
print(final)
print('unique stops')
print(set(temp))
#this is my result
#mine=['5171', '2976', '2979', '2981', '2991', '2992', '1326', '1327', '1329', '1330', '1331', '1336', '1337', '1339', '1340', '1341', '1350', '1352', '1353', '1354', '1355', '1357', '1359', '320', '278', '10', '12', '14', '15', '17', '19', '21', '205', '215', '216', '218', '219', '220', '1622', '1623', '1624', '1625', '1626', '1627', '7347']
#this is what you probably want
#goal=['5171', '2976', '2977', '2978', '2979', '2981', '2991', '2992', '1325', '1326', '1327', '1329', '1330', '1331', '1332', '1333', '1336', '1337', '1339', '1340', '1341', '1342', '1344', '1345', '1347', '1348', '1350', '1352', '1353', '1354', '1355', '1357', '1359', '320', '278', '10', '12', '14', '15', '17', '19', '21', '85','204', 205', '215', '216', '218', '219', '220', '1622', '1623', '1624', '1625', '1626', '1627', '7347']

#unable to add
#['204', '85', '1348', '1347', '1342', '1344', '1345', '1333', '1332', '1325', '2978']
bus_1=[“5171”、“1337”、“1341”、“1350”、“1352”、“1357”、“320”、“278”、“10”、“15”、“215”、“218”、“1623”、“1624”、“7347”]
总线2=[“5171”、“2976”、“2979”、“2981”、“2991”、“2992”、“1326”、“1327”、“1329”、“1331”、“1336”、“1337”、“1339”、“1340”、“1342”、“1345”、“1350”、“1354”、“1357”、“1359”、“320”、“278”、“12”、“15”、“17”、“21”、“205”、“215”、“216”、“218”、“1624”、“1626”、“1627”、“7347”]
总线3=[“5171”,“2977”,“2978”,“2991”,“1325”,“1326”,“1327”,“1329”,“1330”,“1331”,“1332”,“1333”,“1337”,“1340”,“1341”,“1342”,“1345”,“1347”,“1347”,“1348”,“1352”,“1353”,“1354”,“1355”,“1357”,“1359”,“320”,“278”,“10”,“12”,“14”,“15”,“17”,“19”,“21”,“85”,“204”,“215”,“218”,“219”,“220”,“1622”,“1623”,“1624”,“1625”,“1626”,“1627”,“7347”]
总线=[总线1、总线2、总线3]
added=set()
最终=总线1[:]
温度=总线1+总线2+总线3
i=0
x=0
定位球时(决赛)!=设置(温度):
#获取2个元素,我们将查看它们是否在最终路线中

如果x好,这是我的解决方案-请检查它是否满足您的需求。我已经用字典替换了子路由的单独变量,希望这不是问题

routes = {
    'bus_1': ["5171","1337","1341","1350","1352","1357","320","278","10","15","215","218","1623","1624","7347"],
    'bus_2': ["5171","2976","2979","2981","2991","2992","1326","1327","1329","1331","1336","1337","1339","1340","1342","1345","1350","1354","1357","1359","320","278","12","15","17","21","205","215","216","218","1624","1626","1627","7347"],
    'bus_3': ["5171","2977", "2978","2991","1325","1326","1327","1329","1330","1331","1332","1333","1337","1340","1341","1342","1344","1345","1347","1348","1352","1353","1354","1355","1357","1359","320","278","10","12","14","15","17","19","21","85","204","215","218","219","220","1622","1623","1624","1625","1626","1627","7347"]
}

stops = set()
stop_pairs = set()
for route in routes.values():
    stops.update(route)
    for i in range(1,len(route)):
        # each pair is correctly ordered
        stop_pairs.add(tuple(route[i-1:i+1]))
# at this point, 'stops' is the set of all stops
# and 'stop_pairs' is a set of tuples representing sequence of stops
ordered_stops = []
while stops:
    # let's look for the minimal elements in stops
    minimals = set()
    for stop in stops:
        if not any((s, stop) in stop_pairs for s in stops):
            # there is no such s that s precedes stop 
            minimals.add(stop)
    ordered_stops.append(minimals)
    stops.difference_update(minimals)
print(ordered_stops)
我的结果是:

{{{5171万万元元元元{{{5171万万万元元元元元元元元元元元元元{{{{{5171万元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元元{'1355'},{'1357'},{'1359'},{'320'},{'278'},{'10'},{'12'},{'14'},{'15'},{'17'},{'19'},{'21'},{'205',85'},{'204'},{'215'},{'216'},{'218'},{'219'},{'220'},{'1622'},{'1623'},{'1624'},{'1625'},{'1626'},{'1627'},{'7347'}]

更新:我刚刚注意到,我的程序可以很容易地进行通用化,以生成各种解决方案,并对其进行相应的编辑。现在,结果列表由具有相同优先级的停止集组成(即,可以重新排列每个集合中的停止)。显然,有六个这样的集合:{'2977','2976'},{'2979','2978'},{'1325','2992'},{'1336','1332'},{'1347','1350'},{'205','85'}。
# All three buses in 1 2d list.
route = ["5171","1337","1341","1350","1352","1357","320","278","10","15","215","218","1623","1624","7347"],["5171","2976","2979","2981","2991","2992","1326","1327","1329","1331","1336","1337","1339","1340","1342","1345","1350","1354","1357","1359","320","278","12","15","17","21","205","215","216","218","1624","1626","1627","7347"],["5171","2977","2978","2991","1325","1326","1327","1329","1330","1331","1332","1333","1337","1340","1341","1342","1344","1345","1347","1348","1352","1353","1354","1355","1357","1359","320","278","10","12","14","15","17","19","21","85","204","215","218","219","220","1622","1623","1624","1625","1626","1627","7347"]

temp = set()
out = {}

# remove dupes by adding each stop to a set.
for journey in route:
    for stop in journey:
        temp.add(stop)

# create a dictionary where the key is a unique stop
# the value is a list of two empty sets.
for i in temp:
    out[i] = [set(),set()]

for i in temp:
    for journey in route:
        if i in journey:

            # add stops which occur before this stop to one dictionary
            for before in journey[0:journey.index(i)]:
                out[i][0].add(before)

            # and stops which occur after this stop to the other dictionary
            for after in journey[journey.index(i):-1]:
                out[i][1].add(after)

# create a template final list of unique stops from the set.
final_list = list(temp)

# iterate ove the dictionary
for key, value in out.items():

    # get list of what stops should be ordered before the current stop
    before_set = value[0]
    # and a list of those that should be after.
    after_set = value[1]


    for b in before_set:

        # if the current value is not where is should be move it.
        if final_list.index(b) > final_list.index(key):

            temp = final_list.index(key)
            final_list.insert(final_list.index(b), key)
            final_list.pop(temp)

    for a in after_set:

        # if the current value is not where is should be move it.
        if final_list.index(a) < final_list.index(key):

            temp = final_list.index(key)
            final_list.insert(final_list.index(a), key)
            final_list.pop(temp+1)

# and run again in the opposite direction.
final_list = final_list[::-1]

for key, value in out.items():

    before_set = value[0]
    after_set = value[1]

    for b in before_set:

        if final_list.index(b) > final_list.index(key):

            temp = final_list.index(key)
            final_list.insert(final_list.index(b), key)
            final_list.pop(temp)

    for a in after_set:

        if final_list.index(a) < final_list.index(key):

            temp = final_list.index(key)
            final_list.insert(final_list.index(a), key)
            final_list.pop(temp+1)

print(final_list)
路线=[“5171”、“1337”、“1341”、“1350”、“1352”、“1357”、“320”、“278”、“10”、“15”、“215”、“218”、“1623”、“1624”、“7347”]、[“5171”、“2976”、“2979”、“2981”、“2991”、“2992”、“1326”、“1327”、“1329”、“1331”、“1336”、“1337”、“1339”、“1340”、“1342”、“1345”、“1350”、“1354”、“1357”、“1359”、“320”、“278”、“12”、“15”、“17”、“21”、“205”、“215”、“216”、“218”、“1624”、“1626”、“1327”、“1627”、“737”]、[“2977”,"2978","2991","1325","1326","1327","1329","1330","1331","1332","1333","1337","1340","1341","1342","1344","1345","1347","1348","1352","1353","1354","1355","1357","1359","320","278","10","12","14","15","17","19","21","85","204","215","218","219","220","1622","1623","1624","1625","1626","1627","7347"] temp=set() out={} #通过向集合中添加每个停止来删除重复。 对于路线中的旅程: 中途停留: 临时添加(停止) #创建一个字典,其中键是唯一的停止符 #该值是两个空集合的列表。 对于临时工: out[i]=[set(),set()] 对于临时工: 对于路线中的旅程: 如果我在旅途中: #将在此停止之前发生的停止添加到一个字典 对于旅程中的前[0:旅程索引(i)]: out[i][0]。添加(之前) #并将在此停止之后发生的停止发送到另一个字典 对于在途后[行程索引(i):-1]: out[i][1]。添加(在后面) #从集合中创建唯一站点的模板最终列表。 最终清单=清单(临时) #反复阅读字典 对于键,值in out.items(): #获取在当前停止之前应该排序的停止的列表 设置前=值[0] #以及一份应该关注的人的名单。 后设=值[1] 对于设置前的b: #如果当前值不在位置,则应将其移动。 如果最终列表索引(b)>最终列表索引(键): temp=最终列表索引(键) 最终列表。插入(最终列表。索引(b),键) 最终列表pop(临时) 对于in after_集合: #如果当前值不在位置,则应将其移动。 如果最终列表索引(a)<最终列表索引(键): temp=最终列表索引(键) 最终列表。插入(最终列表。索引(a),键) 最终列表pop(临时+1) #然后再朝相反的方向跑。 最终列表=最终列表[:-1] 对于键,值in out.items(): 设置前=值[0] 后设=值[1] 对于设置前的b: 如果最终列表索引(b)>最终列表索引(键): temp=最终列表索引(键) 最终列表。插入(最终列表。索引(b),键) 最终列表pop(临时) 对于in after_集合: 如果最终列表索引(a)<最终列表索引(键): temp=最终列表索引(键) 最终列表。插入(最终列表。索引(a),键) 最终列表pop(临时+1) 打印(最终清单)
请发布预期结果。重复的结果如何?第一站很简单!按照bruno的建议完整发布所需的输出这不是普遍可行的。如果您有列表[1,5,10]、[1,3,10]和[1,7,10],算法无法确定“正确”顺序是[1,3,5,7,10]还是[1,5,3,7,10]-这两条路线都是有效的。@ChristianKönig Spot on!例如在
巴士2
中有一个序列:
“2991”、“2992”、“1326”
巴士3
中有一个序列:
“2991”、“1325”、“1326”
。那么,我们如何判断“2992”是否在“1325”之前?就像depperm一样