国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 裕民县| 郴州市| 益阳市| 甘南县| 宜川县| 海宁市| 电白县| 庆云县| 蕲春县| 九台市| 巩留县| 长沙县| 隆安县| 新昌县| 云霄县| 钟祥市| 桐庐县| 邵阳县| 阜城县| 阳江市| 宜章县| 灵石县| 洛浦县| 东源县| 贵州省| 巴彦淖尔市| 云和县| 皮山县| 宜兰市| 宿松县| 义乌市| 方山县| 邓州市| 铁岭县| 民丰县| 罗山县| 宁安市| 会宁县| 祁阳县| 钦州市| 会泽县|