NOTE: I do not want to use
del
I am trying to understand algorithms better which is why I want to avoid the built-in
del
statement.
I am populating a list of 10 randomly generated numbers. I then am trying to remove an item from the list by index, using a for
loop:
if remove_index < lst_size:
for value in range(remove_index, lst_size-1):
lst[value] = lst[value+1]
lst_size -= 1
Everything works fine, except that the loop is adding the last item twice. Meaning, if the 8th item has the value 4, it will add a 9th item also valued 4. I am not sure why it is doing this. I still am able to move the value at the selected index (while moving everything up), but it adds on the duplicate.
答案 0 :(得分:0)
您的列表中没有添加任何内容。它始于$( document ).ready(function() {
$('ul.feed li').first().addClass('top');
$('ul.feed li').last().addClass('stack');
var touch = $('ul.feed li.top').offset().left;
var pullLeftLimit = touch - 15;
var stackPos = $('ul.feed li.stack').position();
console.log('Stack Of Cards at: ' + stackPos.left + ' top: ' + stackPos.top);
$($('.content ul li').get().reverse()).each(function(i, el) {
$(el).css('z-index', i + 1);
var degrees = Math.floor((Math.random() * 20) - 10);
$(el).css({
'-webkit-transform' : 'rotate('+degrees+'deg)',
'-moz-transform' : 'rotate('+degrees+'deg)',
'-ms-transform' : 'rotate('+degrees+'deg)',
'-o-transform' : 'rotate('+degrees+'deg)',
'transform' : 'rotate('+degrees+'deg)',
'zoom' : 1
});
});
$( function() {
$( ".draggable" ).draggable({
axis: "x",
drag: function( event, ui ) {
var newTouch = $('ul.feed li.top').offset().left;
if(pullLeftLimit > newTouch){
$(this).addClass('swipe');
setTimeout(function(){
$(this).css({top: stackPos.top, left: stackPos.left});
}, 1000);
}
}
});
});
$(function(){
$( ".cover" ).on( "swipe", swipeHandler );
function swipeHandler( event ){
$( this ).parent().addClass( "swipe" );
}
});
function stackMe(){
console.log('test');
}
});
元素,并且由于您不会删除任何元素,因此在您完成操作时它会保留相同的数字。
如果您将所有项目从lst_size
开始复制到列表中的上一个索引后,您想删除最后一项,那么您可以使用remove_index
或lst.pop()
。
冒着轻浮的风险,这是一般规则:如果你想做某事,你必须这样做。说'#34;我不想使用del
&#34;不会改变这个事实。
仅仅递减del
对列表没有影响 - 虽然您可能正在使用它来存储列表的大小,但它们没有连接,而更改一个对另一个没有影响。