【问题标题】:Appending to a list only appends first item附加到列表仅附加第一项
【发布时间】:2019-06-13 16:30:39
【问题描述】:

我有一个列表,我试图将数据注入其中。列出一个看起来像这样

data2 = ['TECH2_HELP', 'TECH2_1507', 'TECH2_1189', 'TECH2_4081', 'TECH2_5625', 'TECH2_4598', 'TECH2_1966', 'TECH2_2573', 'TECH2_1800', 'TECH2_1529']

我正在尝试制作的主列表将使用我现有的配置打印出来。下面的打印不是预期的结果,因为您可以看到它只添加了 data2 列表中的第一项。其次,当我尝试使用 for 循环时,我将所有 data2 数据混合在同一个列表中,请参阅 data3。

config1 = ['interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/7', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi1/0/11', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/35', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/41', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/25', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/43', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/23', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/19', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/39', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!']
data3=['interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/7', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi1/0/11', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/35', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/41', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/25', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!',['interface Gi3/0/43', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/23', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/19', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/39', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!',

我尝试在 data2 上将其拆分为 for 和 len,但是当我这样做时,它会将该列表中的所有数据连续放置。

            for line1 in data1:
                words1 = line1.split()
                if len(words1) > 0:
                    local = ''.join(words1[0:1])
                    config1.append('interface ' + local)
                    config1.append('power inline static max 30000')
                    config1.append('spanning-tree portfast')
                    description = ''.join(data2[:1])
                    config1.append('description ' + description)
                    config1.append('!')  # totally optional
                    hostname = None

预期的列表值应如下所示。

expected_config1 = ['interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/7', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1507', '!', 'interface Gi1/0/11', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1189', '!', 'interface Gi3/0/35', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_4081', '!', 'interface Gi3/0/41', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_5625', '!', 'interface Gi3/0/25', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_4598', '!', 'interface Gi3/0/43', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1966', '!', 'interface Gi3/0/23', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_2573', '!', 'interface Gi3/0/19', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1800', '!', 'interface Gi3/0/39', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1529', '!']

谢谢

【问题讨论】:

  • 我不太明白你的最终目标是什么——你能澄清一下吗?只需提供输入列表,因为您已经提供了输出列表。
  • 最终目标是将描述合并到 config1 列表中。有问题的输入列表是 data2。
  • 代码中的data1列表是接口列表,即'interface Gi3/0/13'。从上面的 config1 输出中可以看出,该列表已正确附加。
  • 我想我解决了您的问题,但我需要更多信息,因为这可能会改变原本简单的解决方案:data2 是您问题中的字符串列表吗?如果是,则无需像此处description = ''.join(data2[:1]) 那样加入其元素。
  • Data2 类型是一个字符串列表。但是,我遇到了一个编译问题,如果我不加入,我将无法迭代 str。感谢您的帮助@atru

标签: python list append


【解决方案1】:

问题在于data2 的处理,否则您的整体附加策略是正确的。

首先,您总是只获取data2 的第一个元素。一种解决方案是维护一个单独的计数器(此处为 kdt),每次执行附加阶段时该计数器都会增加,从而跟踪要附加的 data2 元素,

data2 = ['TECH2_HELP', 'TECH2_1507', 'TECH2_1189', 'TECH2_4081', 'TECH2_5625', 'TECH2_4598', 'TECH2_1966', 'TECH2_2573', 'TECH2_1800', 'TECH2_1529']

config1 = []
data1 = ['Gi3/0/13 some more text']*len(data2)

kdt = 0
for line1 in data1:
    words1 = line1.split()
    if len(words1) > 0:
        local = ''.join(words1[0:1])
        config1.append('interface ' + local)
        config1.append('power inline static max 30000')
        config1.append('spanning-tree portfast')
        description = data2[kdt]
        config1.append('description ' + description)
        config1.append('!')  # totally optional
        hostname = None
        kdt += 1

print config1

打印出来:

['interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1507', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1189', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_4081', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_5625', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_4598', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1966', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_2573', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1800', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1529', '!']

第二个问题是,通过使用data2[0:1],您实际上是在创建一个包含一个字符串的列表,即data2 的第一个元素。如果不加入,这将导致尝试在此行中使用+ 连接字符串和列表,

config1.append('description ' + description)

我认为这是您看到的错误。通过按索引访问正确的元素,您实际上是在检索一个字符串,因此不需要连接操作。

【讨论】:

    猜你喜欢
    • 2016-11-04
    • 1970-01-01
    • 2017-10-18
    • 2016-11-03
    • 1970-01-01
    • 2014-09-17
    • 2014-03-22
    • 2012-07-24
    • 2012-12-20
    相关资源
    最近更新 更多