如何在python中向List添加元素?

时间:2016-03-20 00:15:42

标签: python python-3.x

我在python中处理这项任务,但我不确定我是否正确地将这些元素添加到列表中。所以基本上我假设要创建一个create_list函数,它获取列表的大小并提示用户输入那么多值并将每个值存储到列表中。 create_list函数应该返回这个新创建的列表。最后,main()函数应该提示用户输入的值的数量,将该值传递给create_list函数以设置列表,然后调用get_total函数来打印列表的总和。请告诉我我错过了什么或做错了什么。非常感谢你。

App.accessRule('*');
App.accessRule('https://*.googleapis.com/*');
App.accessRule('https://*.google.com/*');
App.accessRule('https://*.gstatic.com/*');
App.configurePlugin('plugin.google.maps', {
    'API_KEY_FOR_IOS': 'your private key' });

7 个答案:

答案 0 :(得分:9)

main中您创建了空列表,但没有为其分配create_list结果。您还应该将用户输入转换为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()

答案 1 :(得分:2)

您必须将输入转换为整数。 server { listen 8080; server_name zrdn; sendfile off; ... } 返回一个字符串对象。只是做

input()

并且每个输入都要用作整数。

答案 2 :(得分:1)

第一个问题是你没有将myList传递给create_list函数,因此main中的myList不会更新。

如果要在函数内部创建列表并将其返回,然后获取该列表的总计,则需要先将列表存储在某处。将输入解析为整数,也始终执行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()

答案 3 :(得分:1)

发布解决方案的另一种方法是使用一个函数创建所述列表并查找该列表的总和。在解决方案中,map函数遍历给定的所有值,并且只保留整数(split方法用于从值中删除逗号和空格)。此解决方案将打印您的列表和值,但不会返回任何所述值,因此如果您要检查最后的函数,它将生成NoneType。

elif text == "/news":
  for i in range(3):
    reply("{} {}".format(feed.entries[i].summary, feed.entries[i].link))

答案 4 :(得分:0)

您需要将create_list()的返回值赋给变量并将其传递给get_total()

myList = create_list()
total = get_total(myList)

print("list " + str(myList))
print("total " + str(total))

答案 5 :(得分:0)

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.

答案 6 :(得分:-1)

在python中将元素添加到现有列表很简单。 假设谁的列表名称为list1

>>> list1 = ["one" , "two"]

>>> list1 = list1 + "three"

最后一条命令会将元素“三”添加到列表中。这真的很简单,因为列表是python中的对象。当您打印list1时,您会得到:

["one" , "two" , "three"]

完成