簡體   English   中英

我如何操作 2 個列表來制作 2 個新列表?

[英]How can I manipulate 2 lists to make 2 new lists?

我有指示: 一個小孩得到了如何從他家去學校的指示。 不幸的是,他丟失了告訴他如何從學校回家的紙。 既然你是這么好的人,你打算寫一個程序來幫助他。

假設他的母親給他一張紙條,上面寫着以下內容:

R

約翰

大號

大號

學校

這意味着他右轉約翰,左轉金,然后左轉學校。 要獲取新列表,我需要撥打 output:

R

R

約翰

大號

這意味着他向右轉向國王,向右轉向約翰,然后向左轉向家。 該程序的輸入包括方向和要轉入的街道。

方向首先輸入為 L 或 R 接下來在單獨的輸入行中輸入街道名稱 繼續輸入直到輸入 SCHOOL 作為街道名稱

我的問題:我的理解是我需要 4 個列表。 我還需要能夠檢查是否要為回家的方向打印 R 或 L,因為在新的 output 中方向不是彼此相反的 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:        

假設輸入格式正確並以空格分隔:

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")
    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):
        if a == "R":
            print("L")
        else:
            print("R")
        print(b)

zip可以同時迭代多個可迭代對象,大致上您使用了范圍 function 並使用索引來訪問這兩個項目,如下所示:

for i in range(len(directions)):
    a = directions[i]
    b = streets[i]
    ...

如果您希望用戶按指令輸入數據指令,並在看到“SCHOOL”輸入后中斷,那么它可能看起來像這樣:

directions, streets = [], []
while True:
    street = input("Enter street")
    direction = input("Enter Direction")
    directions.append(direction)
    streets.append(street)
    if street == "SCHOOL":
        break

directions = directions[::-1]
streets = streets[::-1][1:] + ['HOME']
for a,b in zip(directions, streets):
    if a == "L":
        print("R")
    else:
        print("L")
    print(b)
directions = []
streets = []

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")
    directions.append(direction)
    streets.append(street)
    if street.lower() == "school":
        break
for (street, direction) in list(zip(streets, directions))[::-1]:
    if street.lower() != "school":
        print(street.upper())
    if direction.upper() == "L":
        print("R")
    else:
        print("L")
print("HOME")

這將構建一個從 HOME 開始的列表,添加到 SCHOOL 的方向,然后將它們反轉以替換 L/R 轉彎:

# start at home
directions = ['HOME']

# add turn and street until arrive at school.
# Note that the instructions say: "The direction is entered first as L or R
# The name of the street is entered next on a separate line of input The input
# keeps going until SCHOOL is entered as the street name", so you can't
# ask for all the directions on one line.
while True:
    d = input('Direction (L or R)? ').upper()
    s = input('Street name? ').upper()
    directions.append(d)
    # break if at school and don't add it to the directions
    if s == 'SCHOOL':
        break  # exit the while loop
    directions.append(s)

# Reverse the directions and replace L/R with R/L.
# This construction is called a "list comprehension".
# It builds a new list by calculating new values from the
# original list.
new_directions = ['R' if item == 'L' else 'L' if item == 'R' else item
                  for item in reversed(directions)]

# Above is equivalent to below:
#new_directions = []
#for item in reversed(directions):
#    if item == 'L':
#        new_directions.append('R')
#    elif item == 'R':
#        new_directions.append('L')
#    else:
#        new_directions.append(item)

# display new directions
for item in new_directions:
    print(item)

Output:

Direction (L or R)? r
Street name? john
Direction (L or R)? l
Street name? king
Direction (L or R)? l
Street name? school
R
KING
R
JOHN
L
HOME

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM