简体   繁体   English

我如何操作 2 个列表来制作 2 个新列表?

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

I have instructions to: A little kid has been given directions on how to get to school from his house.我有指示: 一个小孩得到了如何从他家去学校的指示。 Unfortunately he lost the paper that tells him how to get home from school.不幸的是,他丢失了告诉他如何从学校回家的纸。 Being that you are such a nice person, you are going to write a program to help him.既然你是这么好的人,你打算写一个程序来帮助他。

Suppose his mother gave him a note that said the following:假设他的母亲给他一张纸条,上面写着以下内容:

R R

JOHN约翰

L大号

KING

L大号

SCHOOL学校

this means he turned right on john, left on king, and left to school.这意味着他右转约翰,左转金,然后左转学校。 To get the new list I need to output:要获取新列表,我需要拨打 output:

R R

KING

R R

JOHN约翰

L大号

HOME

this means he turned right on king, right on john, and left to home.这意味着他向右转向国王,向右转向约翰,然后向左转向家。 The input for the program consists of the direction and the street to turn onto.该程序的输入包括方向和要转入的街道。

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方向首先输入为 L 或 R 接下来在单独的输入行中输入街道名称 继续输入直到输入 SCHOOL 作为街道名称

MY QUESTION: What I understand is that I need 4 lists.我的问题:我的理解是我需要 4 个列表。 I also need to be able to check if R or L is to be printed for the directions home since the directions aren't opposites of each other like R=L or L=R in the new output. But how can I check this?我还需要能够检查是否要为回家的方向打印 R 或 L,因为在新的 output 中方向不是彼此相反的 R=L 或 L=R。但是我该如何检查呢? Also, if school can't be an input since the program is going to break, how is the first instruction from the kid going to school going to be entered?另外,如果由于程序即将中断而无法输入学校信息,那么如何输入孩子上学的第一条指令? I'm really confused.我真的很困惑。 This is all of my code right now..这是我现在所有的代码..

     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:        

Assuming that the inputs are correctly formatted and seperated by spaces:假设输入格式正确并以空格分隔:

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 makes it possible to iterate multiple iterables at the same time it would be roughly you used the range function and used the index to access both items like this: zip可以同时迭代多个可迭代对象,大致上您使用了范围 function 并使用索引来访问这两个项目,如下所示:

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

If you wanted the user to input the data instruction by instruction and breaking once it sees the "SCHOOL" input then it could look something like this:如果您希望用户按指令输入数据指令,并在看到“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")

This builds a list starting at HOME, adds the directions to get to SCHOOL, then reverses them replacing the L/R turns:这将构建一个从 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: 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