从一个列表中输入要添加到另一个列表的元素
Input element from one list to add to another
我的 python 旅程还很早,所以我的编码知识非常基础。简单的答案表示赞赏。
我这里的挑战是创建一个函数来 select 列表中的一个项目添加到另一个列表
这里是大致思路的伪代码:
# My Lists
list1 = ["item1", "item2", "item3", "item4", "item5"]
list2 = []
# print verticle, indexed list
for element in (list1):
print(list1.index(element) + 1, element)
# create function to input selected item an add to list2
def selectitem():
selection = input("Enter the number of item you'd like to add to list2: ")
if selection == "1": # How would I now define "1" to select and append index 0 from list1 to list2?
list2.append(selection)
我希望你已经清楚了。总之,如果我在输入中输入“1”,我想将索引 0 添加到 list2。任何关于如何做到这一点的简明解决方案将不胜感激!
#My Lists
list1 = ["item1", "item2", "item3", "item4", "item5"]
list2 = []
#create function to input selected item an add to list2
def selectitem():
selection = int(input("Enter the number of item you'd like to add to list2: "))
list2.append(list1[selection-1])
print(list2)
selectitem()
我的 python 旅程还很早,所以我的编码知识非常基础。简单的答案表示赞赏。
我这里的挑战是创建一个函数来 select 列表中的一个项目添加到另一个列表
这里是大致思路的伪代码:
# My Lists
list1 = ["item1", "item2", "item3", "item4", "item5"]
list2 = []
# print verticle, indexed list
for element in (list1):
print(list1.index(element) + 1, element)
# create function to input selected item an add to list2
def selectitem():
selection = input("Enter the number of item you'd like to add to list2: ")
if selection == "1": # How would I now define "1" to select and append index 0 from list1 to list2?
list2.append(selection)
我希望你已经清楚了。总之,如果我在输入中输入“1”,我想将索引 0 添加到 list2。任何关于如何做到这一点的简明解决方案将不胜感激!
#My Lists
list1 = ["item1", "item2", "item3", "item4", "item5"]
list2 = []
#create function to input selected item an add to list2
def selectitem():
selection = int(input("Enter the number of item you'd like to add to list2: "))
list2.append(list1[selection-1])
print(list2)
selectitem()