- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个列表,我正在尝试将数据注入(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/
Racket 的 pict , 有几个 combinators for combining other pictures .这些文档包含一个很好的表格,说明其 *-append 组合器的工作方式: 这
我看过 Insert content into iFrame和他们的 fiddle http://jsfiddle.net/8VP4y/3/提出以下我遇到问题的代码。 我已经为下面的问题创建了一个 j
我有一个显示非常奇怪结果的微基准: @BenchmarkMode(Mode.Throughput) @Fork(1) @State(Scope.Thread) @Warmup(iterations =
我想知道是否有人可以回答我使用 StringBuilder 对象在 java 中执行这些语句中的哪一个会更好: 使用 .append(string1 + string 2) 对比 .append(st
假设我有两个相同类型的流。是否可以将一个流 append 到另一个流而无需事先将它们转换为列表? 例子: Stream ms = ...; Stream ns = ...; return ms.app
我有以下有效的 jQuery 代码,但它让我思考是否可以对正在 append 的内容执行 append 操作,而无需指定我想要 append 的内容。 append().append() 并没有达到目
这是为了显示诊断页面的检查。我有一个 .append(not_ok) 但当 swf 文件加载 100% 时,我想删除 not_ok 附加,然后添加一个 .append(ok)。 function ca
x = [[1,2],[2,3],[10,1],[10,10]] def duplicatingRows(x, l): severity = x[l][1] if severity =
我有一个列表,我正在尝试将数据注入(inject)其中。列表如下所示 data2 = ['TECH2_HELP', 'TECH2_1507', 'TECH2_1189', 'TECH2_4081',
为了有效地进行一些 DOM 操作,我分离了一个元素。在这个过程中,我遇到了一个有趣的情况: var $holder = $("#d"); var $wrapper = $("").css("borde
我遇到了图片在移动设备上加载速度不够快的问题。我的元素有一个图像和一个按钮。单击该按钮时,图像向下滑动,另一幅图像从顶部滑动以取代它。这是代码 html CSS .moveF
我正在编写一个包含 10 个遗愿 list 的简单哈希表。使用内置的 hash() 计算索引,然后对表大小取模。但是,当我尝试将该对象 append 到该索引处的存储桶列表时,它会 append 到每
我是 LISP 的新手,我正在尝试处理类的 cond 语句。目前,我正在尝试检查传递的值是否为列表,如果是,则将字母 d append 到列表中。 这是我的代码: (defun test(L) (li
我正在使用 Jquery 将数据 append 到 div。但是,append 语句之后页面上没有显示任何内容。 我尝试使用 $(window).load 来确保页面已加载,但这仍然不起作用。 HTM
我有以下代码; function SetupDropdowns() { var PrevType; dropdown1 = document.getElemen
我想在 smarty 中创建一个数组并在其中执行 append 功能!就像我在 smarty 模板中声明一个变量(如 {assign var=sizearr value=''} )然后我想在循环中向其
请考虑以下代码片段: var ul = $(".list_b").find("li").remove().end(); $.each(Sites, functi
我的日志记录配置中有两个 appenders。其中之一在 ERROR 事件上发送电子邮件。 一个类,我无法控制,垃圾邮件 ERROR 消息。所以我仍然想要那些消息,但不是在两个 appenders 中
我正在尝试制作 editText,我要在其中插入一些文本。在每三个字符之后,我想插入破折号。 例子: 类型:123 结果:123- 现在当光标在破折号后面并且你按下删除键时,我想删除破折号和破折号
当我尝试 append 简单的“hello”时,它会被 append ,但很快就会自动删除。仅当我在下面给出的表单中使用它时,才会出现此问题,如果删除该表单,则不会出现问题,并且 hello 会正确
我是一名优秀的程序员,十分优秀!