我想反转Jamies_列表和Drews列表,应该先在列表中,然后附加这两个列表,我想返回完整的列表
def combine_lists(list1, list2):
# Generate a new list containing the elements of list2
# Followed by the elements of list1 in reverse order
return(list2+list1.reverse())
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))
reverse()是Python编程语言中的一种内置方法,在适当的位置反转列表中的对象
也可以使用print(list1.reverse())进行检查。它不会打印任何内容
因此建议使用
return list2 + list1[::-1]
.reverse()
是一种就地操作。它不会返回反向列表,而是返回None
使用
sorted(list1, reverse=True)
否则
否则
相关问题 更多 >
编程相关推荐