我想通过2个步骤在数组的一行中的指定索引处插入一个字符串。从矩阵中:
A=[[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[1, 1, 2, 2, 3],
[2, 3, 4, 5, 6],
[4, 5, 6, 7, 7],
[5, 7, 6, 8, 9]]
我想收到:
A=[[**x**, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[**x**, 1, 2, 2, 3],
[2, 3, 4, 5, 6],
[**x**, 5, 6, 7, 7],
[5, 7, 6, 8, 9]]
或:
A=[[1, 2, 3, 4, 5],
[**x**, 7, 8, 9, 10],
[1, 1, 2, 2, 3],
[**x**, 3, 4, 5, 6],
[4, 5, 6, 7, 7],
[**x**, 7, 6, 8, 9]]
或:
A=[[1, 2, **x**, 4, 5],
[6, 7, 8, 9, 10],
[1, 1, **x**, 2, 3],
[2, 3, 4, 5, 6],
[4, 5, **x**, 7, 7],
[5, 7, 6, 8, 9]]
,依此类推。希望您能理解我的问题(我使用粗体字母来区分字符串)。如果我尝试:
def r(l):
for i in l[::2]:
i.insert(0, 'x')
return l
它返回:
'int'对象没有属性'insert'
但是我想这不是我非常有价值的评论,如果我最终不知道如何完成任务...
答案 0 :(得分:0)
您可以使用简单的索引代替def r(l, idx=0):
for i in l[::2]:
i[idx] = 'x'
return l
>>> print(r(A))
[['x', 2, 3, 4, 5], [6, 7, 8, 9, 10], ['x', 1, 2, 2, 3], [2, 3, 4, 5, 6], ['x', 5, 6, 7, 7], [5, 7, 6, 8, 9]]
:
idx
x
参数为您提供您要更改条目的索引。例如,如果您想将第3个元素更改为>>> print(r(A, idx=2))
[[1, 2, 'x', 4, 5], [6, 7, 8, 9, 10], [1, 1, 'x', 2, 3], [2, 3, 4, 5, 6], [4, 5, 'x', 7, 7], [5, 7, 6, 8, 9]]
,请使用:
const HamburgerCnt = styled.div`
cursor: pointer;
position: absolute;
top: 35px;
right: 41px;
height: 28px;
width: 30px;
input {
display: none;
}
input + label {
position: absolute;
top: 12px;
right: 0px;
width: 30px;
height: 2px;
background: #fff;
display: block;
transform-origin: center;
transition: .5s ease-in-out;
&:after,
&:before {
transition: .5s ease-in-out;
content: "";
position: absolute;
display: block;
width: 100%;
height: 100%;
background: #fff;
}
&:before {
top: -10px;
}
&:after {
bottom: -10px;
}
}
input:checked + label {
transform: rotate(45deg);
&:after {
transform: rotate(90deg);
bottom: 0;
}
&:before {
transform: rotate(90deg);
top: 0;
}
}
`;