我有一个包含~300个列表的列表,但是其中一些是重复的,我想删除它们。我试过了:
cleanlist = [cleanlist.append(x) for x in oldlist if x not in cleanlist]
但它一直向我投掷RuntimeError: maximum recursion depth exceeded in comparison
。我试过sys.setrecursionlimit(1500)
,但这没有用。
更好的方法是什么?
答案 0 :(得分:6)
您根据cleanlist
中的cleanlist
(尚未存在)添加一堆内容到append()
,然后保存返回的值从None
操作(cleanlist
)到cleanlist = []
for x in oldlist:
if x not in cleanlist:
cleanlist.append(x)
。它不会很好地结束。
更好的方法是使用老式的循环结构:
$(document).on('click', '.editable', function() {
var $wrapper = $('<div class="editing"></div>'),
$form = $('<form action="#" class="edit-form"></form>'),
$input = $('<input type="text">');
// Place the original element inside a wrapper:
$(this).after($wrapper);
$(this).remove().appendTo($wrapper).hide();
// Build up the form:
$wrapper.append($form);
$form.append($input);
$input.val($(this).text()).focus();
});
$(document).on('submit', '.edit-form', function(e) {
// Don't actually submit the form:
e.preventDefault();
var val = $(this).find('input').val(),
$wrapper = $(this).parent(),
$original = $wrapper.children().first();
// Use text() instead of html() to prevent unwanted effects.
$original.text(val);
$original.remove();
$wrapper.after($original);
$original.show();
$wrapper.remove();
});
答案 1 :(得分:1)
一个疯狂的解决方案。也许它会有所帮助:
oldlist = [[1,2,3], [1,2,3], [4,5,6], [7,8], [7,8]]
cleanlist = set(tuple(x) for x in oldlist)
print(list(cleanlist))
>>> [(7, 8), (4, 5, 6), (1, 2, 3)]
有关列表的列表,请使用:
cleanlist = [list(item) for item in set(tuple(x) for x in oldlist)]
>>> [[7, 8], [4, 5, 6], [1, 2, 3]]