如何遍历字典的所有键值对并使用python为同一键构建所有不同的值列表。
样本数据:
-- Running cmake version 3.2.2
-- MySQL 5.6.35
-- Packaging as: mysql-5.6.35-Linux-x86_64
-- HAVE_VISIBILITY_HIDDEN
-- ZSTD_INCLUDE_DIR /home/chinyajie/local/usr/local/include
-- ZSTD_LIBRARY /home/chinyajie/local/usr/local/lib/libzstd.so
-- ZSTD_INCLUDE_DIR /home/chinyajie/local/usr/local/include
-- ZSTD_LIBRARY /home/chinyajie/local/usr/local/lib/libzstd.so
test:PATH_TO_ZSTDZSTD_SYSTEM_LIBRARY
-- OPENSSL_INCLUDE_DIR = /usr/include
-- OPENSSL_LIBRARY = /usr/lib64/libssl.so
-- CRYPTO_LIBRARY = /usr/lib64/libcrypto.so
-- OPENSSL_MAJOR_VERSION = 1
-- SSL_LIBRARIES = /usr/lib64/libssl.so;/usr/lib64/libcrypto.so;dl
-- Using cmake version 3.2.2
-- Not building NDB
-- Library fbmysqlclient depends on OSLIBS -lpthread;/home/chinyajie/local/usr/local/lib/libzstd.so;m;rt;/usr/lib64/libssl.so;/usr/lib64/libcrypto.so;dl
-- Googletest was not found. gtest-based unit tests will be disabled. You can run cmake . -DENABLE_DOWNLOADS=1 to automatically download and build required components from source.
-- If you are inside a firewall, you may need to use an https proxy: export https_proxy=http://example.com:80
CMake Error in storage/rocksdb/CMakeLists.txt:
Cannot find source file:
../../rocksdb/usr/bin/make64
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
CMake Error in storage/rocksdb/CMakeLists.txt:
Cannot find source file:
../../rocksdb/MAC=64
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
CMake Error in storage/rocksdb/CMakeLists.txt:
Cannot find source file:
../../rocksdb/--makefile
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
CMake Error in storage/rocksdb/CMakeLists.txt:
Cannot find source file:
../../rocksdb/tmp/tmp.QgIOMFCBRt
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
-- Library mysqlserver depends on OSLIBS -lpthread;/home/chinyajie/local/usr/local/lib/libzstd.so;m;rt;/usr/lib64/libssl.so;/usr/lib64/libcrypto.so;dl;crypt;-lrt
-- CMAKE_BUILD_TYPE: RelWithDebInfo
-- COMPILE_DEFINITIONS: HAVE_CONFIG_H;ZSTD;HAVE_ZSTD_COMPRESS
-- CMAKE_C_FLAGS: -Wall -Wextra -Wformat-security -Wwrite-strings -std=c99
-- CMAKE_CXX_FLAGS: -Wall -Wextra -Wformat-security -Woverloaded-virtual -Wno-unused-parameter -std=c++11 -I /home/chinyajie/local/usr/local/include -I /home/chinyajie/local/usr/local/include
-- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- Configuring incomplete, errors occurred!
For
Cannot find source file:
../../rocksdb/MAC=64
对于上述列表,我需要构建一个列表,例如:
"list": [
{
"1": 1
},
{
"1": 8
},
{
"1": 9
},
{
"1": 1
},
{
"2": 8
},
{
"2": 10
}
],
答案 0 :(得分:1)
这将为您提供想要的东西
import collections
input_ = {'list': [{'1': 1}, {'1': 8}, {'1': 9}, {'1': 1}, {'2': 8}, {'2': 10}]}
result = collections.defaultdict(list)
for elem in input_['list']:
key, value = next(iter(elem.items())) # best way to extract data from your strange format
if value not in result[key]:
result[key].append(value)
result = [dict(result)] # put it in the strange format you want
输出
[{'1': [1, 8, 9], '2': [8, 10]}]
但是,我强烈建议您重新考虑如何构造数据。例如,您的输入应该是一个元组列表,而不是字典列表。
答案 1 :(得分:-1)
这应该可以解决问题-但请注意,仅使用一个键和一个值来制作字典并不是保存数据的方法。
new_dict = {}
for pair in your_list:
for key in pair:
if key in new_dict:
new_dict[key] = new_dict[key].append(pair[key])
else:
new_dict[key] = [pair[key]]
print new_dict
new_dict是具有键->值列表映射
的字典