国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python字典添加,刪除,查詢等相關操作方法詳解

2020-02-15 21:19:41
字體:
來源:轉載
供稿:網友

一、創建增加修改

1、實現代碼

#創建stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38}print(stu_info)#增加stu_info["luoahong"]=32print(stu_info)#修改stu_info["xiedi"]=29print(stu_info)

輸出結果

{'xiedi': 28, 'liuhailin': 27, 'daiqiao': 30, 'hanwenhai': 25, 'chenqun': 38}{'xiedi': 28, 'liuhailin': 27, 'daiqiao': 30, 'hanwenhai': 25, 'chenqun': 38, 'luoahong': 32}{'xiedi': 29, 'liuhailin': 27, 'daiqiao': 30, 'hanwenhai': 25, 'chenqun': 38, 'luoahong': 32}

二、刪除(del)

1、實現代碼

del stu_info["chenqun"]print(stu_info)

2、輸出結果

{'xiedi': 29, 'liuhailin': 27, 'daiqiao': 30, 'hanwenhai': 25, 'luoahong': 32}

 1、Dict_DelItem 

intPyDict_DelItem(PyObject *op, PyObject *key){  Py_hash_t hash;  assert(key);  if (!PyUnicode_CheckExact(key) ||    (hash = ((PyASCIIObject *) key)->hash) == -1) {    hash = PyObject_Hash(key);    if (hash == -1)      return -1;  }  return _PyDict_DelItem_KnownHash(op, key, hash);}

2、Dict_DelItem_KnownHash

int_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash){  Py_ssize_t ix;  PyDictObject *mp;  PyObject *old_value;  if (!PyDict_Check(op)) {    PyErr_BadInternalCall();    return -1;  }  assert(key);  assert(hash != -1);  mp = (PyDictObject *)op;  ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);  if (ix == DKIX_ERROR)    return -1;  if (ix == DKIX_EMPTY || old_value == NULL) {    _PyErr_SetKeyError(key);    return -1;  }  // Split table doesn't allow deletion. Combine it.  if (_PyDict_HasSplitTable(mp)) {    if (dictresize(mp, DK_SIZE(mp->ma_keys))) {      return -1;    }    ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);    assert(ix >= 0);  }  return delitem_common(mp, hash, ix, old_value);}/* This function promises that the predicate -> deletion sequence is atomic * (i.e. protected by the GIL), assuming the predicate itself doesn't * release the GIL. */

3、PyDict_DelItemIf

int_PyDict_DelItemIf(PyObject *op, PyObject *key,         int (*predicate)(PyObject *value)){  Py_ssize_t hashpos, ix;  PyDictObject *mp;  Py_hash_t hash;  PyObject *old_value;  int res;  if (!PyDict_Check(op)) {    PyErr_BadInternalCall();    return -1;  }  assert(key);  hash = PyObject_Hash(key);  if (hash == -1)    return -1;  mp = (PyDictObject *)op;  ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);  if (ix == DKIX_ERROR)    return -1;  if (ix == DKIX_EMPTY || old_value == NULL) {    _PyErr_SetKeyError(key);    return -1;  }  // Split table doesn't allow deletion. Combine it.  if (_PyDict_HasSplitTable(mp)) {    if (dictresize(mp, DK_SIZE(mp->ma_keys))) {      return -1;    }    ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);    assert(ix >= 0);  }  res = predicate(old_value);  if (res == -1)    return -1;  hashpos = lookdict_index(mp->ma_keys, hash, ix);  assert(hashpos >= 0);  if (res > 0)    return delitem_common(mp, hashpos, ix, old_value);  else    return 0;}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中超| 太康县| 浮山县| 贵州省| 樟树市| 新余市| 堆龙德庆县| 绥滨县| 乌拉特后旗| 佛坪县| 博野县| 恩施市| 瑞金市| 教育| 兴海县| 固安县| 会东县| 黑龙江省| 紫阳县| 水城县| 宁化县| 长治市| 浦北县| 军事| 灌云县| 日照市| 凯里市| 石柱| 忻州市| 大名县| 阿巴嘎旗| 伊川县| 罗源县| 高淳县| 陈巴尔虎旗| 忻州市| 孟州市| 商洛市| 保山市| 马尔康县| 马尔康县|