在字典中压缩两个列表但在密钥中保留重复

时间:2017-05-31 09:01:15

标签: python python-2.7 dictionary

我有两个清单:

alist = ['key1','key2','key3','key3','key4','key4','key5']

blist=  [30001,30002,30003,30003,30004,30004,30005]

我想合并这些列表并将它们添加到字典中。

我尝试dict(zip(alist,blist)),但这会给出:

  

{'key3':30003,'key2':30002,'key1':30001,'key5':30005,'key4':   30004}

字典的理想形式是:

  

{'key1':30001,'key2':30002,'key3':30003,'key3':30003,'key4':30004,'key4':   30004,'key5':30005}

我希望将重复项保留在字典中,并且不要在同一个键中加入值(... key3':30003,'key3':30003,...)。是否可能?

提前致谢。

3 个答案:

答案 0 :(得分:3)

字典使用 UNIQUE 键,因此不可能有重复项。

答案 1 :(得分:2)

由于extern "C" JNIEXPORT void JNICALL Java_com_example_ndkbuildtest_MainActivity_convertMap(JNIEnv *env, jobject thiz) { // Obtain the Map instance and its class jclass main_activity_class = env->GetObjectClass(thiz); jmethodID get_the_map = env->GetStaticMethodID(main_activity_class, "getTheMap", "()Ljava/util/Map;"); jobject java_map = env->CallStaticObjectMethod(main_activity_class, get_the_map); jclass map_class = env->GetObjectClass(java_map); // Obtain the key set and its class jclass set_class = env->FindClass("java/util/Set"); jmethodID get_key_set = env->GetMethodID(map_class, "keySet", "()Ljava/util/Set;"); jmethodID get_value = env->GetMethodID(map_class, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); jobject java_key_set = env->CallObjectMethod(java_map, get_key_set); // Convert the key set to an array that we can iterate over easily jmethodID set_toarray = env->GetMethodID(set_class, "toArray", "()[Ljava/lang/Object;"); jobjectArray java_key_array = static_cast<jobjectArray>(env->CallObjectMethod(java_key_set, set_toarray)); jint len = env->GetArrayLength(java_key_array); // Insert all the key,value pairs into a std::map std::map<std::string, std::string> native_map; for (jint i = 0; i < len; ++i) { jstring java_key = static_cast<jstring>(env->GetObjectArrayElement(java_key_array, i)); jstring java_value = static_cast<jstring>(env->CallObjectMethod(java_map, get_value, java_key)); const char *key_chars = env->GetStringUTFChars(java_key, NULL); const char *value_chars = env->GetStringUTFChars(java_value, NULL); native_map.insert(std::pair<std::string, std::string>(key_chars, value_chars)); env->ReleaseStringUTFChars(java_key, key_chars); env->ReleaseStringUTFChars(java_value, value_chars); env->DeleteLocalRef(java_key); env->DeleteLocalRef(java_value); } // Print the result for (const auto& kv : native_map) { __android_log_print(ANDROID_LOG_DEBUG, "convertMap", "nativeMap contains <%s, %s>", kv.first.c_str(), kv.second.c_str()); } } 个对象具有唯一键,因此无法执行此操作。你应该只使用元组的使用列表:

dict

如果您想根据密钥访问所有值,可以使用>>> alist = ['key1','key2','key3','key3','key4','key4','key5'] >>> blist= [30001,30002,30003,30003,30004,30004,30005] >>> zip(alist, blist) [('key1', 30001), ('key2', 30002), ('key3', 30003), ('key3', 30003), ('key4', 30004), ('key4', 30004), ('key5', 30005)] 作为:

collections.defaultdict

您可以访问类似于普通dict对象的>>> from collections import defaultdict >>> my_dict = defaultdict(list) >>> for k, v in zip(alist, blist): ... my_dict[k].append(v) ... >>> my_dict defaultdict(<type 'list'>, {'key3': [30003, 30003], 'key2': [30002], 'key1': [30001], 'key5': [30005], 'key4': [30004, 30004]}) 。例如:

defaultdict

答案 2 :(得分:2)

由于dict必须仅使用唯一键,并且如果您插入两次相同的键,最后一个将被存储 - 这可能是您可以使用的:

from itertools import groupby

alist = ['key1','key2','key3','key3','key4','key4','key5']
alist = [i for i, j in groupby(alist)]

blist = [30001,30002,30003,30003,30004,30004,30005]
blist = [list(j) for i, j in groupby(blist)]

print dict(zip(alist, blist))
#{'key3': [30003, 30003], 'key2': [30002], 'key1': [30001], 'key5': [30005], 'key4': [30004, 30004]}

如果您还想保留键盘顺序,可以使用OrderedDict:

from collections import OrderedDict
print OrderedDict(zip(alist, blist))