我有一个包含名称的元组列表.这些名称是父名和子名,我想创建一个带有名称的分层字典树.例如,我有以下列表:
[('john','marry'),('mike','john'),('mike','hellen'),('john','elisa')]
Run Code Online (Sandbox Code Playgroud)
我想创建这个:
{
'mike':{
'john':{
'marry':{}
'elisa':{}
}
'hellen':{}
}
}
Run Code Online (Sandbox Code Playgroud) 出于记录目的,我尝试自动捕获传递给此函数内的函数的参数,并将它们转换为arg : value. 我尝试了该inspect.signature()模块,但它只提供函数的默认输入,而不提供传递给它的参数
import inspect
def my_add_func(a, b ,c=3):
sig = inspect.signature(my_add_func)
print("my_add_func args : {0}".format(sig))
return a + b + c
if __name__ == '__main__':
my_add_func(10, 2, 3)
Run Code Online (Sandbox Code Playgroud)
输出:
(a, b, c=3)
Run Code Online (Sandbox Code Playgroud)
而我想要:
{a: 10, b: 2, c:3}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
如何避免sol在此列表理解中不必要地查询设置对象?目前,我为每个对象查询两次,一次在三元组中,一次在谓词中.但是,我想不出更优雅的解决方案.有吗?
dnf = (
(
(
d if p[i,d,True] in sol
else
-d if p[i,d,False] in sol
)
for d in range(N)
if p[i,d,True] in sol or p[i,d,False] in sol
)
for i in range(M)
)
Run Code Online (Sandbox Code Playgroud) 从字典看起来像:
newdict = {'1':{'label':'10232'},'2':{'label':'2420'},'3':{'label':'3530'},...}
Run Code Online (Sandbox Code Playgroud)
如何创建以下元组列表?
newlist = [(1,10232),(2,2420),(3,3530)]
Run Code Online (Sandbox Code Playgroud)
我认为我的列表理解不正确:
[newlist(k,k['label']) for (k,v) in newdict.items()]
Run Code Online (Sandbox Code Playgroud) 我正在用熊猫和 pyplot 绘制直方图。有关其他信息,我在直方图分布的某些百分位处添加了线条。我已经发现您可以axvline在整个图表的某个百分比高度上显示:
cycle_df = pd.DataFrame(results)
plot = cycle_df.plot.hist(bins=30, label='Cycle time')
plot.axvline(np.percentile(cycle_df,5), label='5%', color='red', linestyle='dashed', linewidth=2, ymax=0.25)
plot.axvline(np.percentile(cycle_df,95), label='95%', color='blue', linestyle='dashed', linewidth=2, ymax=0.25)
Run Code Online (Sandbox Code Playgroud)
是否可以让红/蓝线恰好在直方图条的末端结束以看起来平滑?
OrderedDict我正在尝试更新具有相同值的键列表int,例如
for idx in indexes:
res_dict[idx] = value
Run Code Online (Sandbox Code Playgroud)
其中value是一个int变量,indexes是slist的inta,充当键,res_dict是 an OrderedDict,尝试在一行中解决上述问题,
res_dict[indexes]=value
Run Code Online (Sandbox Code Playgroud)
但得到了错误:
TypeError: unhashable type: 'list'
Run Code Online (Sandbox Code Playgroud)
循环或列表理解是在这里进行此更新的唯一方法吗?
我有这个代码:
class weapon():
def __init__(self, Name, Type, Description):
self.Name=Name
self.Type=Type
self.Description=Description
WEAPONS = { "starterSword":weapon("Starter Sword", "Sword", "A short, stunt steel sword."),
"basicSword":weapon("Basic Sword", "Sword", "A basic steel sword.")
}
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
for item in WEAPONS:
print(self.Name)
Run Code Online (Sandbox Code Playgroud)
我将如何在Python 3中进行此操作?
我的字典有问题。我正在使用Python3。我敢肯定我只是看不到一些简单的东西。
我正在从文件中读取行以创建字典。每行的前3个字符用作键(它们是唯一的)。从那里,我根据该行其余部分的信息创建一个列表。每4个字符组成一个列表。创建列表后,我将目录作为值写入目录,并将行的前三个字符作为键。
问题是,每次我向字典中添加新的key:value对时,它似乎都会覆盖(或更新)先前编写的字典条目中的值。键很好,只是更改了值。因此,最后,所有键的值都等于文件最后一行的列表。
我希望这很清楚。任何想法将不胜感激。
下面的代码片段
formatDict = dict()
sectionList = list()
for usableLine in formatFileHandle:
lineLen = len(usableLine)
section = usableLine[:3]
x = 3
sectionList.clear()
while x < lineLen:
sectionList.append(usableLine[x:x+4])
x += 4
formatDict[section] = sectionList
for k, v in formatDict.items():
print ("for key= ", k, "value =", v)
formatFileHandle.close()
Run Code Online (Sandbox Code Playgroud) 关于这两个选项:
try:
userid = get_userid()
except:
userid = ""
Run Code Online (Sandbox Code Playgroud)
与
userid = ""
try:
userid = get_userid()
except:
pass
Run Code Online (Sandbox Code Playgroud)
是否有任何区别,特别想知道如果userid仅在try块中设置命名空间将如何工作?它们是否都具有相同的命名空间范围?
一个比另一个更受欢迎吗?
我不完全确定这种行为是否符合预期,但这绝对是很奇怪的。当你有这样的代码时:
def a_function():
if a_list != some_other_list:
print(a_list)
Run Code Online (Sandbox Code Playgroud)
它工作得很好,我没有遇到任何问题。但是,如果将其更改为:
def a_function():
if a_list != some_other_list:
a_list = some_other_list
Run Code Online (Sandbox Code Playgroud)
突然,出现了一个问题,提示a_list第 2 行是未解析的引用。为什么if语句中的内容会影响能否a_list解决呢?这种事情正常吗?这可能是 Python 3.6.1 或 PyCharm(社区版 2017.1.5)中的错误吗?任何澄清这一点的帮助将不胜感激。
python ×10
dictionary ×5
python-3.x ×5
list ×3
function ×2
debugging ×1
histogram ×1
inspect ×1
iteration ×1
matplotlib ×1
pandas ×1
pycharm ×1
python-2.7 ×1
scope ×1
tuples ×1