gpt4 book ai didi

python - append 到列表仅 append 第一项

转载 作者:行者123 更新时间:2023-12-01 07:43:16 27 4
gpt4 key购买 nike

我有一个列表,我正在尝试将数据注入(inject)其中。列表如下所示

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', '!']

谢谢

最佳答案

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

首先,您始终只获取 data2 的第一个元素。解决此问题的一种解决方案是维护一个单独的计数器(此处为 kdt),每次执行 append 阶段时该计数器都会增加,从而跟踪要 append 的 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)

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

关于python - append 到列表仅 append 第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56584925/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com