简体   繁体   English

为什么列表在python中包含超过n个元素?

[英]Why list contains more than n element in python?

I am novice in Python. 我是Python的新手。 I would like to enter 10 elements into the list. 我想在列表中输入10个元素。 Below program appends 10 elements to each of the list. 下面的程序将10个元素追加到每个列表中。 But below program prints 11 objects in list why? 但是下面的程序为什么在列表中打印11个对象? I got this program from http://www.learnpython.org/page/Basic%20Operators link. 我从http://www.learnpython.org/page/Basic%20Operators链接获得了此程序。 I wanted to know x = object() , what does it mean? 我想知道x = object() ,这是什么意思?

x = object()
y = object()
i = 0
# change this code
x_list = [x]
y_list = [y]
while(i < 10):
    x_list.append((10))
    y_list.append(11)
    i = i + 1
#x_list = [x]
#y_list = [y]
big_list = x_list + y_list

print "x_list contains %d objects" % len(x_list) # prints 11 objects, #Why?
print "y_list contains %d objects" % len(y_list) # prints 11 objects, #Why?
print "big_list contains %d objects" % len(big_list)
print x_list.count(10)
print y_list.count(11)
    print big_list.count(10)
# testing code
if x_list.count(x) == 10 and y_list.count(y) == 10:
    print "Almost there..."
if big_list.count(x) == 10 and big_list.count(y) == 10:
    print "Great!"

x_list = [x] => 1 object. x_list = [x] => 1个对象。 while(i < 10): x_list.append((10)) => 10 objects (0 to 9) So 11 is perfectly normal. while(i < 10): x_list.append((10)) => 10个对象(0到9)所以11完全正常。 You should have set x_list = [] (empty list) not to have this 1st element. 您应该设置x_list = [] (空列表)不具有该第一个元素。

The learnpython.org basic operators page provides some examples of using arithmetic operators in different contexts, including lists. Learnpython.org基本运算符页面提供了一些在不同上下文(包括列表)中使用算术运算符的示例。 Based on quickly reading the tutorial in that page, probably the answer that the page authors were after would be: 基于快速阅读该页面中的教程,该页面作者所追求的答案可能是:

x_list = [x] * 10
y_list = [y] * 10
big_list = x_list + y_list

Which would create a list with 10 x:s in it. 这将创建一个包含10 x:s的列表。

You were using a loop to do that, which is much more flexible way. 您正在使用循环来执行此操作,这是一种更加灵活的方法。 A more pythonic way to fill a list by using a loop would be eg to use list comprehensions: 使用循环来填充列表的更Python方式是例如使用列表推导:

x_list = [x for i in xrange(10)]

or use a for loop, then you don't have to keep track of the index yourself: 或使用for循环,则不必自己跟踪索引:

for i in xrange(10):
    x_list.append(x)

Below program appends 10 elements to each of the list. 下面的程序将10个元素追加到每个列表中。 But below program prints 11 objects in list why? 但是下面的程序为什么在列表中打印11个对象?

Because the list had 1 element in it before you started appending. 因为列表中有1个元素,然后才开始追加。 The list [x] contains 1 element, x . 列表[x]包含1个元素x 1 + 10 = 11. 1 + 10 = 11。

I wanted to know x = object(), what does it mean? 我想知道x = object(),这是什么意思?

It means exactly what it says: "x shall be a name for the result of calling object ". 它的意思完全是它所说的:“ x应该是调用object结果的名称”。 object is a class, so calling it creates an instance. object是一个类,因此调用它会创建一个实例。 An object instance is exactly what it sounds like - just an object, with nothing interesting about it. 一个object实例确实就是它的外观-只是一个对象,对此没有任何兴趣。

object() just generates a new instance of the class object . object()只是生成类object的新实例。 you first insert this element to x_list , then you add another 10 elements 您首先将此元素插入x_list ,然后再添加10个元素

x_list contains X plus 10 times 10 = 11... x_list包含X加10乘以10 = 11 ...

y_list - the same. y_list-相同。

when you type x_list = [x] it means the list is initialized as a list with only x in it. 当您输入x_list = [x] ,表示列表被初始化为仅包含x的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 生成包含多个元素的新项目的列表 - Generate list with new items that contains more than one element Python清单remove()移除超过1个元素 - Python List remove() removing more than 1 element 检查字符串是否包含Python中数组中的多个元素 - Check if string contains more than one element from array in Python 如果某个元素在 Python 中出现次数超过 n 次,则删除该元素的出现次数 - Delete occurrences of an element if it occurs more than n times in Python Python列表包含的数字是否超过N次? - Does a Python list contain any number more than N times? 为什么列表元素替换比python中的字符串元素替换慢? - Why is list element replacement slower than string element replacement in python? 是否可以在python中固定列表的大小,以便如果列表包含的内容超过该数量,则会引发错误 - Is it possible to fix the size of a list in python so that if the list contains more than that it'll throw an error 如何测试一个字符串是否包含超过 N 个嵌套的 function 调用? - How to test if a string contains more than N nested function calls? Python如果N个变量中有多个是真的 - Python if more than one of N variables is true 如果 List 中的元素包含一个单词 | Python - if Element in List contains a word | Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM