我有一个嵌套的python dictionary数据结构.我想without用collection模块读取它的键和值.数据结构如下所示.
d = {'dict1': {'foo': 1, 'bar': 2}, 'dict2': {'baz': 3, 'quux': 4}}
Run Code Online (Sandbox Code Playgroud)
我试图使用波纹管方式读取字典中的键但得到错误.
码
for key, value in d:
print(Key)
Run Code Online (Sandbox Code Playgroud)
错误
ValueError: too many values to unpack (expected 2)
Run Code Online (Sandbox Code Playgroud)
所以任何人都可以解释错误背后的原因以及如何遍历字典.
我有两个嵌套的字典数据。我想将它们合并以在python中创建一个字典。字典数据:
dict1 = {'employee':{'dev1': 'Roy'}}
dict2 = {'employee':{'dev2': 'Biswas'}}
Run Code Online (Sandbox Code Playgroud)
现在,我尝试从中创建像波纹管这样的字典。所需输出
dict_output = {'employee':{
'dev1': 'Roy',
'dev2': 'Biswas'
}
}
Run Code Online (Sandbox Code Playgroud)
我的尝试:
import json
dict1 = {'employee':{'dev1': 'Roy'}}
dict2 = {'employee':{'dev2': 'Biswas'}}
dict1.update(dict2)
print(json.dumps(dict1, indent=2))
Run Code Online (Sandbox Code Playgroud)
输出:
{
"employee": {
"dev2": "Biswas"
}
}
Run Code Online (Sandbox Code Playgroud)
我无法合并这两个字典。需要帮助来合并它们
我有一个如下所示的字符串:
string1 = "47482M4I14M7I7M1I26M8D25M4I20M2I11M7I17M7I7M22I14M3I35M3I30M1D15M2I16M17D4M5D15M7D37M1D24M5D5M6D27M4I35M11I10M3I5M3I24M15I175M3D13M236792H"
Run Code Online (Sandbox Code Playgroud)
我想用字母表(即AZ或az)分隔,并将相关值放在列表字典中.每组数字都与字母表相关联.例如,
'M'与47482,14,7I7等相关联.
'我'与4,1等相关联
'H'与236792相关联
我的最终数据结构就像
dict = {
M:[47482, 14, 717],
I:[4, 1],
H:[236792]
}
Run Code Online (Sandbox Code Playgroud)
我的尝试:
import re
string1 = "47482M4I14M7I7M1I26M8D25M4I20M2I11M7I17M7I7M22I14M3I35M3I30M1D15M2I16M17D4M5D15M7D37M1D24M5D5M6D27M4I35M11I10M3I5M3I24M15I175M3D13M236792H"
tmp = re.split('[a-zA-Z]', string1)
print(tmp)
Run Code Online (Sandbox Code Playgroud)
我无法将这些字母作为分隔符.需要帮助来创建数据结构.
我正在尝试从 dnd 文件创建 png 图像,以便更好地理解 dnd 文件。我见过一些将 dnd 文件转换为图像格式的软件,我有大约 2000 个 dnd 文件,我想将这些文件转换为图像文件以便更好地理解
是否可以从 clusterw dnd 文件创建系统发育树图像?
dnd 文件的一个示例如下所示:
(
(
A:0.336889,
(
(
B:0.204161,
(
(
(
C:0.112841,
(
D:0.0605849,
E:0.0605849):0.112841):0.133598,
(
F:0.0946236,
G:0.0946236):0.133598):0.148107,
H:0.148107):0.204161):0.285724,
I:0.285724):0.336889):0.338734,
J:0.338734):0.338734;
Run Code Online (Sandbox Code Playgroud) 要删除语言依赖项,我需要使用任何模块或代码来处理Perl中的命令行参数.之前我使用Python Argparse模块来处理命令行参数.
我的Python代码与argparse模块:
import argparse
default_database_config='config/database.config'
default_general_config = 'config/general.config'
commandLineArgumentParser = argparse.ArgumentParser()
commandLineArgumentParser.add_argument("-dconfig", "--dconfigaration", help="Database Configuration file name", default=default_database_config)
commandLineArgumentParser.add_argument("-gconfig", "--gconfigaration", help="General Configuration file name", default=default_general_config)
commandLineArguments = commandLineArgumentParser.parse_args()
database_config_file = commandLineArguments.dconfigaration
general_config_file = commandLineArguments.gconfigaration
Run Code Online (Sandbox Code Playgroud)
如何将上述Python代码转换为Perl代码?
我正在使用下面的python代码创建字典。但是我收到dct_structure变量的一个PEP 8警告。警告是:do not use a lambda expression use a def
from collections import defaultdict
dct_structure = lambda: defaultdict(dct_structure)
dct = dct_structure()
dct['protocol']['tcp'] = 'reliable'
dct['protocol']['udp'] = 'unreliable'
Run Code Online (Sandbox Code Playgroud)
我对python lambda表达式还不满意。因此,任何人都可以帮助我为以下两行python代码定义函数以避免PEP警告。
dct_structure = lambda: defaultdict(dct_structure)
dct = dct_structure()
Run Code Online (Sandbox Code Playgroud)