附加到列表错误
Appending to List Eror
我是 Python 的新手,正在努力使用将新值加入列表然后将其添加到列表列表的语法。 loc_ref
和 from_loc
都是预先存在的列表,user_says
是使用 user_says = input("Enter Data")
的用户输入字段
print(loc_ref,"\n") #returns [['COMPANY|ADDRESS|CITY|STATE|LOCATION']]
print(from_loc,"\n") #returns ['COMPANY A', '1515 STREET RD', 'CITYA', 'ST']
print([user_says])
loc_ref = loc_ref.append(from_loc + [user_says])
print(loc_ref) #returns None
为什么 loc_ref
返回 None
?
那是因为方法append
returns None
。听起来怪怪的?那是因为它有效 in-place
并更改列表 loc_ref
,但总是 return None
.
您所要做的就是将您的行更改为:
loc_ref.append(from_loc + [user_says])
原始列表只会在最后一个位置包含附加值。
了解更多信息 here:
The append() method adds a single item to the existing list. It doesn't return a new list; rather it modifies the original list.
您正在将 loc_ref
的值设置为 None
loc_ref = loc_ref.append(from_loc + [user_says])
list.append
不是 return 带有附加值的列表,它 return 是 None
,因此 loc_ref
被设置为 None
尝试一下:
loc_ref.append(from_loc + [user_says])
并去掉 loc_ref =
https://www.programiz.com/python-programming/methods/list/append
As mentioned, the append() method only modifies the original list. It doesn't return any value.
append() 是一种方法 return 仅做 loc_ref.append(from_loc + [user_says])
就足够了
我是 Python 的新手,正在努力使用将新值加入列表然后将其添加到列表列表的语法。 loc_ref
和 from_loc
都是预先存在的列表,user_says
是使用 user_says = input("Enter Data")
print(loc_ref,"\n") #returns [['COMPANY|ADDRESS|CITY|STATE|LOCATION']]
print(from_loc,"\n") #returns ['COMPANY A', '1515 STREET RD', 'CITYA', 'ST']
print([user_says])
loc_ref = loc_ref.append(from_loc + [user_says])
print(loc_ref) #returns None
为什么 loc_ref
返回 None
?
那是因为方法append
returns None
。听起来怪怪的?那是因为它有效 in-place
并更改列表 loc_ref
,但总是 return None
.
您所要做的就是将您的行更改为:
loc_ref.append(from_loc + [user_says])
原始列表只会在最后一个位置包含附加值。
了解更多信息 here:
The append() method adds a single item to the existing list. It doesn't return a new list; rather it modifies the original list.
您正在将 loc_ref
的值设置为 None
loc_ref = loc_ref.append(from_loc + [user_says])
list.append
不是 return 带有附加值的列表,它 return 是 None
,因此 loc_ref
被设置为 None
尝试一下:
loc_ref.append(from_loc + [user_says])
并去掉 loc_ref =
https://www.programiz.com/python-programming/methods/list/append
As mentioned, the append() method only modifies the original list. It doesn't return any value.
append() 是一种方法 return 仅做 loc_ref.append(from_loc + [user_says])
就足够了