我有两个字典:
a = {0: array([4.5, 5. ]), 1: array([3.5, 4.5]), 2: array([1., 1.])}
b = {0: array([4., 5. ]), 1: array([3, 4]), 2: array([1.5, 1.])}
如何检查这两个字典是否相等? 我尝试过:
a==b
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
还
(a==b).all()
a.all()==b.all()
所有返回错误。
答案 0 :(得分:-1)
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.Setup.Configuration;
using System.IO;
using Microsoft.Win32;
static class VS_Tools
{
public static string GetVSPath(string specificVersion = "", bool avoidPrereleases = true, string requiredWorkload = "")
{
string vsPath = "";
// Method 1 - use "Microsoft.VisualStudio.Setup.Configuration.SetupConfiguration" method.
// Note: This code has is a heavily modified version of Heath Stewart's code.
// original source: (Heath Stewart, May 2016) https://github.com/microsoft/vs-setup-samples/blob/80426ad4ba10b7901c69ac0fc914317eb65deabf/Setup.Configuration.CS/Program.cs
try
{
var e = new SetupConfiguration().EnumAllInstances();
int fetched;
var instances = new ISetupInstance[1];
do
{
e.Next(1, instances, out fetched);
if (fetched > 0)
{
var instance2 = (ISetupInstance2)instances[0];
var state = instance2.GetState();
// Lets make sure this install is complete.
if (state != InstanceState.Complete)
continue;
// If we have a version to match lets make sure to match it.
if (!string.IsNullOrWhiteSpace(specificVersion))
if (!instances[0].GetInstallationVersion().StartsWith(specificVersion))
continue;
// If instances[0] is null then skip
var catalog = instances[0] as ISetupInstanceCatalog;
if (catalog == null)
continue;
// If there is not installation path lets skip
if ((state & InstanceState.Local) != InstanceState.Local)
continue;
// Lets make sure it has the required workload - if one was given.
if (!string.IsNullOrWhiteSpace(requiredWorkload))
{
if ((state & InstanceState.Registered) == InstanceState.Registered)
{
if (!(from package in instance2.GetPackages()
where string.Equals(package.GetType(), "Workload", StringComparison.OrdinalIgnoreCase)
where package.GetId().Contains(requiredWorkload)
orderby package.GetId()
select package).Any())
{
continue;
}
}
else
{
continue;
}
}
// Lets save the installation path and make sure it has a value.
vsPath = instance2.GetInstallationPath();
if (string.IsNullOrWhiteSpace(vsPath))
continue;
// If specified, avoid Pre-release if possible
if (avoidPrereleases && catalog.IsPrerelease())
continue;
// We found the one we need - lets get out of here
return vsPath;
}
}
while (fetched > 0);
}
catch (Exception){ }
if (string.IsNullOrWhiteSpace(vsPath))
return vsPath;
// Fall-back Method: Find the location of visual studio (%VS90COMNTOOLS%\..\..\vc\vcvarsall.bat)
// Note: This code has is a heavily modified version of Kevin Kibler's code.
// source: (Kevin Kibler, 2014) http://stackoverflow.com/questions/30504/programmatically-retrieve-visual-studio-install-directory
List<Version> vsVersions = new List<Version>() { new Version("15.0"), new Version("14.0"),
new Version("13.0"), new Version("12.0"), new Version("11.0") };
foreach (var version in vsVersions)
{
foreach (var isExpress in new bool[] { false, true })
{
RegistryKey registryBase32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
RegistryKey vsVersionRegistryKey = registryBase32.OpenSubKey(
string.Format(@"{0}\{1}.{2}",
(isExpress) ? @"SOFTWARE\Microsoft\VCSExpress" : @"SOFTWARE\Microsoft\VisualStudio",
version.Major, version.Minor));
if (vsVersionRegistryKey == null) { continue; }
string path = vsVersionRegistryKey.GetValue("InstallDir", string.Empty).ToString();
if (!string.IsNullOrEmpty(path))
{
path = Directory.GetParent(path).Parent.Parent.FullName;
if (File.Exists(path + @"\VC\bin\cl.exe") && File.Exists(path + @"\VC\vcvarsall.bat"))
{
vsPath = path;
break;
}
}
}
if (!string.IsNullOrWhiteSpace(vsPath))
break;
}
return vsPath;
}
}
和字典比较键和值。但是值是数组。 ==
产生一个布尔数组,该数组不能很好地满足字典测试的是/否期望(歧义错误)。
一种解决方法是分别比较这些值。 array1 == array2
是浮点数组的最佳测试。假设键匹配,下面的列表理解就可以很好地工作:
np.allclose
应该对密钥进行另一种测试。
但是如果阵列形状不同,In [177]: array=np.array
In [178]: a = {0: array([4.5, 5. ]), 1: array([3.5, 4.5]), 2: array([1., 1.])}
...: b = {0: array([4., 5. ]), 1: array([3, 4]), 2: array([1.5, 1.])}
In [179]: a
Out[179]: {0: array([4.5, 5. ]), 1: array([3.5, 4.5]), 2: array([1., 1.])}
In [180]: b
Out[180]: {0: array([4., 5.]), 1: array([3, 4]), 2: array([1.5, 1. ])}
In [181]: [np.allclose(a[k],b[k]) for k in a]
Out[181]: [False, False, False]
In [182]: [np.allclose(a[k],a[k]) for k in a]
Out[182]: [True, True, True]
将不起作用:
allclose
如果您知道字典可能会有什么不同,则比较任务将简单得多。它们的键可以不同吗?它们的值类型(数组,列表,数字)是否可以不同?如果值是数组,它们的形状可以不同吗?