Python List& for-each访问(在内置列表中查找/替换) [英] Python List & for-each access (Find/Replace in built-in list)

查看:253
本文介绍了Python List& for-each访问(在内置列表中查找/替换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



来自C / C ++的我不禁想到内存管理,而且它也是一个纯粹的传递引用的语言。很难把它从我的头上。所以我试图从Java的角度来思考它,并把除了原始数据之外的所有东西都视为通过引用来传递。



问题:我有一个列表,包含一堆



如果我使用for-each语法,例如: $ p> 用于my_list中的成员:
print(member.str);

成员相当于一个实际的参考对象是什么?



这是相当于做:

  i = 0 
,而我< len(my_list):
print(my_list [i])
i + = 1



<我认为这不是,因为当我想要做一个替换,它不起作用,也就是说,这是行不通的:

  for my_list中的成员:
if member == some_other_obj:
member = some_other_obj

一个简单的查找和替换列表。那可以在for-each循环中完成,如果是的话,怎么样?否则,我只需要使用随机访问语法(方括号),或将无法工作,我需要删除条目,并插入一个新的?即:

  i = 0 
for my_list中的成员:
if member == some_other_obj:
my_list.remove(i)
my_list.insert(i,member)
i + = 1


<解决方案

解决方案

解决方案



成员成员成员变量绑定到每个连续的列表元素。但是,在循环内重新分配该变量不会直接影响列表本身。例如,这段代码不会改变列表:

  my_list = [1,2,3] 
成员在my_list中:
member = 42
print my_list

输出:

$ block $
$ 1,2,3 $]如果您想更改包含不可变类型的列表,您需要执行以下操作:

  my_list = [1,2,3 ] 
for ndx,枚举成员(my_list):
my_list [ndx] + = 42
print my_list


$ b $输出:

lockquote
<43,44,45>

如果您的列表包含可变对象,则可以直接修改当前的成员对象:

  class C:
def __init __(self,n):
self.num = n
def __repr __(self ):
return str(self.num)

my_list = [C(i)for my xrange(3)]
my_list中的成员:
成员.nu​​m + = 42
print my_list




<42,43, 44

请注意,您仍然不会更改列表,只需修改列表中的对象即可。



您可以通过阅读命名和绑定


I originally thought Python was a pure pass-by-reference language.

Coming from C/C++ I can't help but think about memory management, and it's hard to put it out of my head. So I'm trying to think of it from a Java perspective and think of everything but primitives as a pass by reference.

Problem: I have a list, containing a bunch of instances of a user defined class.

If I use the for-each syntax, ie.:

for member in my_list:
    print(member.str);

Is member the equivalent of an actual reference to the object?

Is it the equivalent of doing:

i = 0
while i < len(my_list):
    print(my_list[i])
    i += 1

I think it's NOT, because when I'm looking to do a replace, it doesn't work, that is, this doesn't work:

for member in my_list:
    if member == some_other_obj:
        member = some_other_obj

A simple find and replace in a list. Can that be done in a for-each loop, if so, how? Else, do I simply have to use the random access syntax (square brackets), or will NEITHER work and I need to remove the entry, and insert a new one? I.e.:

i = 0
for member in my_list:
   if member == some_other_obj:
      my_list.remove(i)
      my_list.insert(i, member)
   i += 1

解决方案

Answering this has been good, as the comments have led to an improvement in my own understanding of Python variables.

As noted in the comments, when you loop over a list with something like for member in my_list the member variable is bound to each successive list element. However, re-assigning that variable within the loop doesn't directly affect the list itself. For example, this code won't change the list:

my_list = [1,2,3]
for member in my_list:
    member = 42
print my_list

Output:

[1, 2, 3]

If you want to change a list containing immutable types, you need to do something like:

my_list = [1,2,3]
for ndx, member in enumerate(my_list):
    my_list[ndx] += 42
print my_list

Output:

[43, 44, 45]

If your list contains mutable objects, you can modify the current member object directly:

class C:
    def __init__(self, n):
        self.num = n
    def __repr__(self):
        return str(self.num)

my_list = [C(i) for i in xrange(3)]
for member in my_list:
    member.num += 42
print my_list

[42, 43, 44]

Note that you are still not changing the list, simply modifying the objects in the list.

You might benefit from reading Naming and Binding.

这篇关于Python List&amp; for-each访问(在内置列表中查找/替换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆