python列表扩展另一个列表

时间:2018-08-31 18:51:33

标签: python extend

我是编码的新手,因此决定开始研究python语言。我在使用此代码时遇到了一些麻烦,但是我不愿意解决它,任何人都可以帮我一下。

add_animals = []
loop = 1
while loop == 1:
user_animal = (input("Enter the animal you want to add to the list: "))
    if user_animal.lower() == "q" or user_animal.lower() == "quit":
        loop = 0
    else:
        add_animals.append(user_animal)
print (add_animals)
animals = ["Chimpanzee", "Panther", "Wolf", "Armadillo"]
animals.extend(add_animals)
order_animals = input("Would you like to see the list in 1(order), 2 (out of order), 3(reverse)")

if order_animals == "1":
    print (sorted(animals)
elif order_animals == "2":
    print (animals)
elif order_animals == "3":
    animals.sort()
    animals.reverse()
    print(animals)

结果是这个

Enter the animal you want to add to the list: zebra
Enter the animal you want to add to the list: elephant 
Enter the animal you want to add to the list: lion
Enter the animal you want to add to the list: q
['zebra', 'elephant ', 'lion']
Would you like to see the list in 1(order), 2 (out of order), 3(reverse)1
['Armadillo', 'Chimpanzee', 'Panther', 'Wolf', 'elephant ', 'lion', 'zebra']

我很难弄清楚的是这个。我选择的输出将对列表进行排序,但与其将“动物”列表视为单个列表,不如将其作为包含2个列表的列表并将其内部itens排序为2个列表一起使用字母顺序列表。

它可以与我尝试过的其他示例一起使用,但是我不愿意使用它吗?有人有线索吗?

0 个答案:

没有答案