如何使用Python处理带有中断的双数组? [英] How to process double array with break using Python?
问题描述
我想处理一个数组.这是我以前的问题.如何在Python中处理中断数组?
I would like to process an array. Here is my previous question. How to process break an array in Python?
但是我还有另一种情况,我参考了这个答案,但是我仍然可以得到我的期望结果.就是这种情况
But I have another case and I refer to that answer, but I still can get my expectation result. Here is the case
Folder = "D:\folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'hhdt5463']
Default = {'gadfg5': '6754435'}
情况二
Folder = "D:\folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'None']
Default = {'gadfg5': '6754435', '546sfdgh': '98769786'}
情况3
Folder = "D:\folder"
Name = ['gadfg5', '546sfdgh']
Ver = [g675436g, 'hhdt5463']
Default = {}
这是我尝试过的:
for dn, dr, key in zip(Name, Ver, Default):
if dr is None:
Path = os.path.join(Folder, dn, Default[key])
print("None",Path)
else:
Path = os.path.join(Folder, dn, dr)
print("Not None", Path)
CASE 3
的输出为 empty
.但我期望输出应该是:
The output of CASE 3
is empty
. But my expectation the output supposed to be:
D:\folder\gadfg5\g675436g
D:\folder\546sfdgh\hhdt5463
CASE 2
的输出符合我的期望,是正确的:
The output of CASE 2
is correct as my expectation which is:
D:\folder\gfg\6754435
D:\folder\546sfdgh\98769786
案例1 的输出仅返回一条路径,如下所示:
The output of CASE 1
only returns one path, like this:
D:\folder\gadfg5\6754435
但是我期望输出应该是这样的:
But my expectation the output supposed to be like this:
D:\folder\gadfg5\6754435
D:\folder\546sfdgh\hhdt5463
任何人都可以给我一个主意.谢谢
Anyone can give me an idea, please. Thanks
推荐答案
如果一个是 Name
和 Ver
包含两个元素而字典 Default
只有一个,因此当您压缩它们时,输出将只有一个元组:('gadfg5',None,'gadfg5')
what happens in case one is Name
and Ver
has two elements while dictionary Default
has just one so when you zip them the output will have just one tuple: ('gadfg5', None, 'gadfg5')
由于 Default
是一本字典,并且其键是 Names
的元素,所以我们不必压缩它们中的三个,而是尝试:
Since the Default
is a dictionary and its keys are the elements of Names
we don't have to zip the three of them, instead try:
for dn, dr in zip(Name, Ver):
if dr is None:
Path = os.path.join(Folder, dn, Default[dn])
print("None", Path)
else:
Path = os.path.join(Folder, dn, dr)
print("Not None", Path)
相同的逻辑适用于情况3
The same logic applies to case 3
但是这里有一个问题,让我用情况4进行演示:
But there is an issue here let me demonstrate with another scenario, case 4:
输入:
Folder = "D:\Folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'hhdt5463']
Default = {}
此处dict < Default
为空, Ver
具有None元素.这将在 Default [dn]
处引发Key错误.因此,我们也如下进行检查:
Here dict Default
is empty and Ver
has a None element. This will throw a Key error at Default[dn]
. So lets put a check for that too as follows:
for dn, dr in zip(Name, Ver):
if dr is None:
if dn in Default: # check if Default contains the key dn
Path = os.path.join(Folder, dn, Default[dn])
print("None", Path)
else:
print('no default path for ', dn)
else:
Path = os.path.join(Folder, dn, dr)
print("Not None", Path)
这篇关于如何使用Python处理带有中断的双数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!