[英]How to add elements to a List in python?
我正在python中完成这个任务,但我不确定我是否正确地将这些元素添加到列表中。 所以基本上我假设创建一个create_list函数,它获取列表的大小并提示用户输入那么多值并将每个值存储到列表中。 create_list函数应该返回这个新创建的列表。 最后,main()函数应该提示用户输入的值的数量,将该值传递给create_list函数以设置列表,然后调用get_total函数来打印列表的总和。 请告诉我我错过了什么或做错了什么。 非常感谢你提前。
def main():
# create a list
myList = []
number_of_values = input('Please enter number of values: ')
# Display the total of the list elements.
print('the list is: ', create_list(number_of_values))
print('the total is ', get_total(myList))
# The get_total function accepts a list as an
# argument returns the total sum of the values in
# the list
def get_total(value_list):
total = 0
# calculate the total of the list elements
for num in value_list:
total += num
#Return the total.
return total
def create_list(number_of_values):
myList = []
for num in range(number_of_values):
num = input('Please enter number: ')
myList.append(num)
return myList
main()
在main
您创建了空列表,但没有为其分配create_list
结果。 您还应该将用户输入int
为int
:
def main():
number_of_values = int(input('Please enter number of values: ')) # int
myList = create_list(number_of_values) # myList = function result
total = get_total(myList)
print('the list is: ', myList)
print('the total is ', total)
def get_total(value_list):
total = 0
for num in value_list:
total += num
return total
def create_list(number_of_values):
myList = []
for _ in range(number_of_values): # no need to use num in loop here
num = int(input('Please enter number: ')) # int
myList.append(num)
return myList
if __name__ == '__main__': # it's better to add this line as suggested
main()
您必须将输入转换为整数。 input()
返回一个字符串对象。 做就是了
number_of_values = int(input('Please enter number of values: '))
并且每个输入都要用作整数。
第一个问题是你没有将myList
传递给create_list
函数,因此myList
不会更新。
如果要在函数内部创建一个列表并将其返回,然后获取该列表的总计,则需要先将列表存储在某处。 将输入解析为整数, if __name__ == '__main__':
,也总是这样做if __name__ == '__main__':
。 以下代码应该工作并打印正确的结果:)
def main():
number_of_values = int(input('Please enter number of values: '))
myList = create_list(number_of_values)
print('the list is: ', myList)
print('the total is ', get_total(myList))
def get_total(value_list):
total = 0
for num in value_list:
total += num
return total
def create_list(number_of_values):
myList = []
for num in range(number_of_values):
num = int(input('Please enter number: '))
myList.append(num)
return myList
if __name__ == '__main__':
main()
发布解决方案的另一种方法可能是创建一个函数来创建所述列表并查找该列表的总和。 在解决方案中,map函数遍历给定的所有值,并且只保留整数(split方法用于从值中删除逗号和空格)。 此解决方案将打印您的列表和值,但不会返回任何所述值,因此如果您要检查最后的函数,它将生成NoneType。
def main():
aListAndTotal()
#Creates list through finding the integers and removing the commas
#For loop iterates through list and finds the total
#Does not return a value, but prints what's stored in the variables
def aListAndTotal():
myList = map(int, input("Please enter number of values: ").split(","))
total = 0
for num in myList:
total += num
print ("The list is: ", myList)
print ("The total is: ", total)
if __name__ == "__main__":
main()
您需要将create_list()的返回值赋给变量并将其传递给get_total()
myList = create_list()
total = get_total(myList)
print("list " + str(myList))
print("total " + str(total))
List is one of the most important data structure in python where you can add any type of element to the list.
a=[1,"abc",3.26,'d']
To add an element to the list, we can use 3 built in functions:
a) insert(index,object)
This method can be used to insert the object at the preferred index position.For eg, to add an element '20' at the index 1:
a.index(1,20)
Now , a=[1,20,'abc',3.26,'d']
b)append(object)
This will add the object at the end of the list.For eg, to add an element "python" at the end of the list:
a.append("python")
Now, a=[1,20,'abc',3.26,'d','python']
c)extend(object/s)
This is used to add the object or objects to the end of the list.For eg, to add a tuple of elements to the end of the list:
b=(1.2, 3.4, 4.5)
a.extend(b)
Now , a=[1,20,'abc',3.26,'d','python',1.2, 3.4, 4.5]
If in the above case , instead of using extend, append is used ,then:
a.append(b)
Now , a=[1,20,'abc',3.26,'d','python',(1.2, 3.4, 4.5)]
Because append takes only one object as argument and it considers the above tuple to be a single argument that needs to be appended to the end of the list.
在python中将元素添加到现有列表是微不足道的。 假设谁有一个列表名列表1
>>> list1 = ["one" , "two"]
>>> list1 = list1 + "three"
最后一个命令会将元素“three”添加到列表中。 这很简单,因为列表是python中的对象。 当你打印list1时,你得到:
["one" , "two" , "three"]
完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.