[英]The error comes as can only concatenate list (not “NoneType”) to list
I want to reverse the Jamies_list and Drews list should be in first then attach both the list and I want to return the complete list.我想颠倒 Jamies_list 和 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()
is an inplace operation. .reverse()
是就地操作。 It won't return the reversed list, it returns None
它不会返回反向列表,它返回None
Use sorted(list1, reverse=True)
.使用sorted(list1, reverse=True)
。
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+sorted(list1, reverse=True))
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))
ELSE别的
def combine_lists(list1, list2):
# Generate a new list containing the elements of list2
# Followed by the elements of list1 in reverse order
list1.reverse()
return(list2+list1)
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))
ELSE别的
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[::-1]
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))
reverse() is an inbuilt method in Python programming language that reverses objects of the list in place reverse()是 Python 编程语言中的内置方法,可将列表的对象反转到位
You can also check it by using print(list1.reverse()).您也可以使用 print(list1.reverse()) 来检查它。 It will print None.它将打印无。
So would suggest using return list2 + list1[::-1]
所以建议使用return list2 + list1[::-1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.