我想在python列表中删除特定列表

时间:2019-03-04 08:38:50

标签: python

我有以下列表项。要删除此列表中的特定列表。当用户输入电话号码时,它会检查列表并获取与电话号码列表匹配的特定列表,然后删除该特定列表并显示其余列表。

account= [['abc', 8566665891, 's', 5000], ['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]]

c_phone = int(input("Enter your phone to Close Account : "))
    for a, b, c, d in account:
        if c_phone == b:
           del account[b]
           print(account)

结果将是:

Enter your phone to Close Account : 9852560352

[['abc', 8566665891, 's', 5000], ['pqr', 6854265891, 's', 7000]]

5 个答案:

答案 0 :(得分:1)

您可以使用listcomp过滤列表:

$(function() {
var URL_PREFIX = "http://localhost:8983/solr/archiveCore/select?q=strSO:";
var URL_SUFFIX = "&wt=json";
$("#searchBoxstrSO").autocomplete({
source : function(request, response) {
var URL = URL_PREFIX + $("#searchBoxstrSO").val() + URL_SUFFIX;
$.ajax({
url : URL,
success : function(data) {
var docs = JSON.stringify(data.response.docs);
var jsonData = JSON.parse(docs);
response($.map(jsonData, function(value, key) {
return {
label : value.strSO
}
}));
},
dataType : 'jsonp',
jsonp : 'json.wrf'
});
},
minLength : 1
})
});
$(function() {
var URL_PREFIX = "http://localhost:8983/solr/archiveCore/select?q=strSO:";
var URL_MIDDLE = "OR strSO_ngram:";
var URL_SUFFIX = "&wt=json";
$("#ngramBoxstrSO").autocomplete(
{
source : function(request, response) {
var searchString = "\"" + $("#ngramBoxstrSO").val() + "\"";
var URL = URL_PREFIX + searchString + URL_MIDDLE
+ searchString + URL_SUFFIX;
$.ajax({
url : URL,
success : function(data) {
var docs = JSON.stringify(data.response.docs);
var jsonData = JSON.parse(docs);
response($.map(jsonData, function(value, key) {
return {
label : value.strSO
}
}));
},
dataType : 'jsonp',
jsonp : 'json.wrf'
});
},
minLength : 1
})
});
</script>

或者,您可以使用函数account= [['abc', 8566665891, 's', 5000], ['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]] number = 8566665891 account = [i for i in account if i[1] != number] # [['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]]

filter()

您还可以使用列表的字典而不是列表的列表。删除字典中的项目比列表中的项目要快。

list(filter(lambda x: x[1] != number, account))

答案 1 :(得分:1)

使用过滤器重建列表可能更好,但这是实际删除元素的答案:

account= [['abc', 8566665891, 's', 5000], ['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]]

c_phone = int(input("Enter your phone to Close Account : "))
for a, b, c, d in account:
    if c_phone == b:
       account.remove([a,b,c,d])
       print(account)

答案 2 :(得分:0)

尝试一下:

{
  ...state,
  byId: {
    ...state.byId,
    [employeeId]: { ...state.byId[employeeId], data }
  }
}

答案 3 :(得分:0)

您可以在遍历列表时使用You can also use rownum=1 SELECT rdo_code, batch_no, reference_no, dln, retrn_seq_num, ftype_code, tin, branch_code, tax_type retrn_period, version FROM rfp_returns_ref WHERE tin = '000079108' AND ftype_code = '1702EX' AND UPPER(status) = UPPER('POSTED') AND rownum=1 ORDER BY version DESC; --------------------------------------------------------------------------------------- or subquery like SELECT rdo_code, batch_no, reference_no, dln, retrn_seq_num, ftype_code, tin, branch_code, tax_type retrn_period, version FROM rfp_returns_ref a WHERE tin = '000079108' AND ftype_code = '1702EX' AND UPPER(status) = UPPER('POSTED') AND a.version = (SELECT Max(b.version) FROM rfp_returns_ref b WHERE b.tin = a.tin AND b.ftype_code = a.ftype_code AND b.UPPER(status) = UPPER(a.status)); ,然后使用np.random.choice从列表中删除索引为enumerate的元素。

del account[i]

答案 4 :(得分:0)

您可以在列表上枚举,以便它在idx中索引,在i中的元素中,您可以检查列表中是否存在项,例如if 8566665891 in i并通过 index删除该项

account= [['abc', 8566665891, 's', 5000], ['xyz', 9852560352, 'c', 6000], ['pqr', 6854265891, 's', 7000]]
c_phone = int(input("Enter your phone to Close Account : "))
for idx,i in enumerate(account):
     if c_phone in i:
          del account[idx]

print(account)