简体   繁体   English

Python追加或覆盖列表元素

[英]Python append or overwrite list element

How do you alternatively append or overwrite a list element in python? 你如何在python中附加或覆盖列表元素?

I am currently trying to translate a function I have working in ruby to python, my ruby code looks like such: 我目前正在尝试将我在ruby中工作的函数转换为python,我的ruby代码看起来像这样:

def self.push_array_hash(key,selector,level,array)
  (0..array.length).each do |i|
    x = array[i]
    selector[level] = i+1
    if(x.is_a?(Array))
      self.push_array_hash(key,selector,level+1,x)
    end
    self.push_datum("#{key}(#{selector.join(',')})", x)
  end
end

I am able to call this function as: self.push_array_hash(key,Array.new,0,val) without having to know the dimensions of the nested arrays. 我能够将此函数称为: self.push_array_hash(key,Array.new,0,val)而不必知道嵌套数组的尺寸。 The code selector[level] = i+1 easily handles appending an element to the array if selector[level] does not exist yet, and changing the value at level if it does exist. 如果selector[level]尚不存在,则代码selector[level] = i+1可以轻松处理向数组附加元素,如果存在,则更改level的值。

That is ruby (only given so everyone can understand the what I am aiming for), on to python. 这是红宝石(只有这样才能让每个人都能理解我的目标),对于python。 Here is my translated code: 这是我翻译的代码:

def push_list_dict(self, key, selector, level, array):
  for i, x in enumerate(array):
    selector[level] = i+1
    if isinstance(x, (list, set)):
      if isinstance(value, set):
        value = sorted(value)
      self.push_list_dict(key,selector,level+1,x)
    self.push_datum(key+"("+",".join(map(str, selector))+")",x)

I call the function as such: self.push_array_dict(key,[],0,value) , however it breaks on selector[level] = i+1 and gives me the error: IndexError: list assignment index out of range . 我这样调用函数: self.push_array_dict(key,[],0,value) ,但它在selector[level] = i+1上中断并给出错误: IndexError: list assignment index out of range Is there an easy way of alternatively appending a value to the array or resetting the value? 是否有一种简单的方法可以替代地将值附加到数组或重置值? Thanks, 谢谢,

Joshua 约书亚

You could use: 你可以使用:

selector[level:level+1] = [i+1]

If selector[level] already exists, this replaces the value. 如果selector[level]已存在,则替换该值。 If it does not already exist, it appends the value. 如果它尚不存在,则追加该值。

For example, 例如,

In [102]: selector = []

In [103]: selector[0:1] = [100]

In [104]: selector
Out[104]: [100]

In [105]: selector[0:1] = [200]

In [106]: selector
Out[106]: [200]

Notice what happens if the index is beyond the length of selector : 注意如果索引超出了selector的长度会发生什么:

In [107]: selector[7:8] = [300]

In [108]: selector
Out[108]: [200, 300]

It does not insert at that index location, it just appends. 它不会在该索引位置插入,只是附加。

Python doesn't magically extend the array the way ruby does. Python并没有像ruby那样神奇地扩展数组。 One way is to check the length and pad to the correct length using [None] * x or [0] * x 一种方法是使用[None] * x[0] * x检查长度并填充到正确的长度

selector += [None] * (level-len(selector))
selector[level] = i+1

Aside: 在旁边:

range doesn't include the "stop" value, so you may not want the -1 there. range不包括“停止”值,因此您可能不希望-1存在。 The usual way to write this 写这个的通常方法

for i in range(0,len(array)-1):
    x = array[i]
    ...

is

for i, x in enumerate(array):
    ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM