【问题标题】:How can I manipulate 2 lists to make 2 new lists?我如何操作 2 个列表来制作 2 个新列表?
【发布时间】:2022-12-18 15:58:03
【问题描述】:

我有指示: 一个小孩得到了如何从他家去学校的指示。不幸的是,他丢失了告诉他如何从学校回家的纸。既然你是这么好的人,你打算写一个程序来帮助他。

假设他的母亲给他一张纸条,上面写着以下内容:

R

约翰

大号

大号

学校

这意味着他右转约翰,左转金,左转学校。要获取新列表,我需要输出:

R

R

约翰

大号

这意味着他向右转向国王,向右转向约翰,然后向左转向家。 该程序的输入包括方向和要转入的街道。

方向首先输入为 L 或 R 接下来在单独的输入行中输入街道名称 继续输入,直到输入 SCHOOL 作为街道名称

我的问题:我的理解是我需要 4 个列表。我还需要能够检查是否要为回家的方向打印 R 或 L,因为这些方向并不像新输出中的 R=L 或 L=R 那样彼此相反。但是我该如何检查呢?另外,如果由于程序即将中断而无法输入学校信息,那么如何输入孩子上学的第一条指令?我真的很困惑。这是我现在所有的代码..

     while True:
       direction= input("Enter the directions for all three streets (L or R):")
       street= input("Enter all three street names for the L/R directions in order:\n")
       streets= street.split()
       if streets[0] or streets[1] or streets[2] == "school" or streets[0] or streets[1] or streets[2] =="SCHOOL":
          break
  #original two lists
     directions= direction.split()
     print(directions)
     print(streets)
  #new list:        

【问题讨论】:

    标签: python list direction


    【解决方案1】:

    假设输入格式正确并以空格分隔:

    while True:
        direction= input("Enter the directions for all three streets (L or R):")
        street= input("Enter all three street names for the L/R directions in order:
    ")
        directions = directions.split()[::-1]
        streets = street.split()[::-1]
        # above reverses the lists since we are now going in the opposite direction
        # also remove the first entry from streets "SCHOOL" and add "HOME"
        streets = streets[1:] + ["HOME"]
        # then you just need to output the results:
        for a,b in zip(directions, streets):
            print(a)
            print(b)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 2021-06-04
      • 2010-12-20
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      相关资源
      最近更新 更多