简体   繁体   English

错误是因为只能将列表(而不是“NoneType”)连接到列表中

[英]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.

相关问题 TypeError:只能串联列表(不是“ NoneType”)以列出 - TypeError: can only concatenate list (not “NoneType”) to list 接收类型错误:只能将列表(不是“NoneType”)连接到列表 - Receiving TypeError: can only concatenate list (not "NoneType") to list 错误:只能将 str(不是“列表”)连接到 str - Error: can only concatenate str (not "list") to str 当熊猫是导入时,Cx_freeze TypeError只能使用numpy依赖关系将列表(不是“ NoneType”)串联起来 - Cx_freeze TypeError can only concatenate list (not “NoneType”) to list using numpy dependency when pandas is an import 收到错误:“ TypeError:只能将列表(而不是“ tuple”)连接到列表” - Receiving error: “TypeError: can only concatenate list (not ”tuple“) to list” 类型错误只能将列表(不是生成器)连接到列表 - Type Error can only concatenate list (not generator ) to list Python 错误信息:TypeError: can only concatenate list (not "str") to list - Python Error Message: TypeError: can only concatenate list (not "str") to list 运行时错误 - 只能将列表(不是“int”)连接到列表 - Runtime Error - can only concatenate list (not "int") to list 收到此错误:TypeError: can only concatenate list (not "int") to list - Getting this error: TypeError: can only concatenate list (not "int") to list 需要有关此错误的帮助:只能将列表(而不是“str”)连接到列表 - need help regarding this error: can only concatenate list (not “str”) to list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM