【问题标题】:Python adding a list to a slice of another listPython将列表添加到另一个列表的一部分
【发布时间】:2020-09-30 00:12:54
【问题描述】:

这是基本问题:

>>> listb = [ 1, 2, 3, 4, 5, 6, 7 ]
>>> slicea = slice(2,5)
>>> listb[slicea]
[3, 4, 5]
>>> lista = listb[slicea]
>>> lista
[3, 4, 5]
>>> listb[slicea] += lista
>>> listb
[1, 2, 3, 4, 5, 3, 4, 5, 6, 7]

listb 应该是

[1, 2, 6, 8, 10, 6, 7]

但是3, 4, 5 是在3, 4, 5 之后插入的,没有添加到其中。


tl;博士

我有这段代码不起作用:

    self.lib_tree.item(song)['values'][select_values] = adj_list
    self.lib_tree.item(album)['values'][select_values] += adj_list
    self.lib_tree.item(artist)['values'][select_values] += adj_list

完整代码如下:

def toggle_select(self, song, album, artist):

    # 'values' 0=Access, 1=Size, 2=Selected Size, 3=StatTime, 4=StatSize,
    #          5=Count, 6=Seconds, 7=SelSize, 8=SelCount, 9=SelSeconds
    # Set slice to StatSize, Count, Seconds
    total_values = slice(4, 7)       # start at index, stop before index
    select_values = slice(7, 10)     # start at index, stop before index

    tags = self.lib_tree.item(song)['tags']
    if "songsel" in tags:
        # We will toggle off and subtract from selected parent totals
        tags.remove("songsel")
        self.lib_tree.item(song, tags=(tags))
        # Get StatSize, Count and Seconds
        adj_list = [element * -1 for element in \
                    self.lib_tree.item(song)['values'][total_values]]
    else:    
        tags.append("songsel")
        self.lib_tree.item(song, tags=(tags))
        # Get StatSize, Count and Seconds
        adj_list = self.lib_tree.item(song)['values'][total_values]  # 1 past

    self.lib_tree.item(song)['values'][select_values] = adj_list
    self.lib_tree.item(album)['values'][select_values] += adj_list
    self.lib_tree.item(artist)['values'][select_values] += adj_list
    if self.debug_toggle < 10:
        self.debug_toggle += 1
        print('artist,album,song:',self.lib_tree.item(artist, 'text'), \
                                   self.lib_tree.item(album, 'text'), \
                                   self.lib_tree.item(song, 'text'))
        print('adj_list:',adj_list)

adj_list 在调试中显示了正确的值。

如何将值列表添加到列表的切片中?

【问题讨论】:

  • 这不是完整的代码。我不知道大多数数据结构是什么。请发布 MCVE:stackoverflow.com/help/how-to-ask
  • @MadPhysicist 我制作了一个缩小的通用版本。原始代码有 5K 行,包含所有内容是不切实际的。
  • 您是否尝试将值彼此相加,numpy 样式? Python 序列不这样做。如果您希望发生这种情况,您将需要 numpy 数组。
  • @ShadowRanger 没错。将一个列表中的值添加到另一个列表中的值。我读过numpy,但从未使用过它。我认为它是面向“科学”应用的,并不认为添加 2 + 2 = 4 是科学的。
  • @WinEunuuchs2Unix: 2 + 2 = 4 不是。但[2, 3, 4] + [2, 3, 4] = [4, 6, 8] 实际上是为numpy 构建的。科学应用程序受益它,但其核心只是用于矢量化数字运算。

标签: python list slice addition


【解决方案1】:

你想要的行为不是任何 Python 内置类型的特性; + 带有内置序列意味着连接,而不是元素相加。但是numpy 数组会做你想做的事,所以我建议研究numpy。简单例子:

>>> import numpy as np
>>> a = np.array([2,3,4], dtype=np.int64)
>>> b = np.array([5,6,7], dtype=np.int64)
>>> a += b
>>> a
array([ 7,  9, 11])
>>> print(a)
[ 7  9 11]
>>> print(a.tolist())
[7, 9, 11]

请注意,输出(reprstr 形式)看起来与 Python lists 略有不同,但如果需要,您可以转换回纯 Python list

【讨论】:

猜你喜欢
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-22
相关资源
最近更新 更多