I am trying to search in a 2D numpy array for a specific value, the get_above method returns a list of coordinates above the character 'initial char'
def get_above(current, wordsearch):
list_of_current_coords = get_coords_current(current, wordsearch)
#print(list_of_current_coords)
length = len(list_of_current_coords)
first_coords = []
second_coords = []
for x in range(length):
second = list_of_current_coords[x][1]
new_first = list_of_current_coords[x][0] - 1
first_coords.append(new_first)
second_coords.append(second)
combined = [first_coords, second_coords]
above_coords = []
for y in range(length):
lst2 = [item[y] for item in combined]
above_coords.append(lst2)
return above_coords
def …Run Code Online (Sandbox Code Playgroud) 我有这样的 JSON 数据:
{
"profiles": {
"auto_scaler": [
{
"auto_scaler_group_name": "myasg0",
"auto_scaler_group_options": {
":availability_zones": ["1a", "1b", "1c"],
":max_size": 1,
":min_size": 1,
":subnets": ["a", "b", "c"],
":tags": [
{":key": "Name", ":value": "app0" },
{":key": "env", ":value": "dev" },
{":key": "role", ":value": "app" },
{":key": "domain", ":value": "example.com" },
{":key": "fonzi_app", ":value": "true"},
{":key": "vpc", ":value": "nonprod"}
]
},
"dns_name": "fonz1"
},
{
"auto_scaler_group_name": "myasg1",
"auto_scaler_group_options": {
":availability_zones": ["1a", "1b", "1c"],
":max_size": 1,
":min_size": 1,
":subnets": ["a", "b", "c"],
":tags": [
{":key": …Run Code Online (Sandbox Code Playgroud) 我正在使用第8版开发有角度的应用程序。在ngIf表达式内部,我想检查数组中是否存在存在。所以我写了下面的表达式:
*ngIf="questionniare.factors.some(item => item.intensities.length > 0)"
Run Code Online (Sandbox Code Playgroud)
但是现在我在控制台窗口中收到此错误:
解析器错误:绑定不能包含[questionniare.factors.some(item => item.intensities.length> 0)]中第34列的赋值
但是,正如我所见,我的状况没有任何任务。那么有什么问题,我该如何解决?
(我知道我可以定义一个方法并在该方法内完成这项工作,但是我想知道这是否对ngIf有限制,我下次应该考虑一下吗?)
我在评估生成器表达式时遇到了一个奇怪的问题。
我的代码有一个测试几个条件的生成器表达式。我们称之为“gen_ex”。如果我把它变成一个列表来查看输出:
print(list(gen_ex))
Run Code Online (Sandbox Code Playgroud)
我明白了:
[False, False, False, False, True, True, False, False, False]
Run Code Online (Sandbox Code Playgroud)
但是,当我对其使用 any() 和 all() 时,会得到意想不到的结果。
any(gen_ex)
Run Code Online (Sandbox Code Playgroud)
返回 False,而:
all(gen_ex)
Run Code Online (Sandbox Code Playgroud)
返回真。
这应该是预期的行为吗?还是幕后发生了什么奇怪的事情?
我有一个想要在文本文件中查找的单词列表。现在我正在尝试any迭代文件中的行的方法。它返回True或False正确,因此工作正常。
但我的问题是是否可以看到它找到了哪个单词?我可以通过我的代码和文本看到找到了哪个单词,但如果可能的话,我想以某种方式在代码中使用它。
下面是我的意思的一个例子。如果.TrueFalseline
list_of_words = ['apple', 'banana', 'lemon']
with open(file, 'r') as f:
lines = f.readlines()
for line in lines:
x = any(word in line for word in list_of_words)
print(x)
Run Code Online (Sandbox Code Playgroud) 我使用 python 3.10.1,通过 ipython (7.31.0,调用相同的 python 3.10.1)交互进行探索,然后在脚本准备好后直接通过 python 进行探索。
我的代码中有一个错误,我将其简化为两者之间的以下行为差异:
[Python]
In [1]: any(map(bool, ("")))
Out[1]: <map at 0x7f7f2d6061d0>
Run Code Online (Sandbox Code Playgroud)
[CPython]
>>> any(map(bool, ("")))
False
Run Code Online (Sandbox Code Playgroud)
因为 IPython 中的输出map对象是真实的,所以当在语句中使用代码时,if两个程序将给出相反的结果。我想知道是什么导致了这种差异,是否可以采取任何措施来修复它,以及是否存在其他类似的错误(功能?)我应该注意。
为什么
any(['','foo'])
Run Code Online (Sandbox Code Playgroud)
举起例外
TypeError: cannot perform reduce with flexible type
Run Code Online (Sandbox Code Playgroud)
我认为它应该评估True,因为
bool('') 评估为 Falsebool('foo') 评估为 Trueany()可以使用任何可转换的对象序列bool我正在使用Python 2.7.
def room_actions
case $next_move
when "instructions"
instructions
when $next_move.any? { |x| ['up', 'down', 'left', 'right'].include?(x) }
determine_start
else
puts "I don't understand that."
prompt; $next_move = gets.chomp()
room_actions
end
end
Run Code Online (Sandbox Code Playgroud)
原谅我不理解这里发生了什么,但为什么ruby会抛出这个错误?
foo.rb:83:在
room_actions': undefined method任何?for"":String(NoMethodError)
$next_move 是一个全局变量(我知道它们很糟糕,我正在重构),我从状态机中获取chomp来提供这个方法.
似乎我没有定义'任何?' 方法.或者我传递的字符串是空的?无论如何,不是'.any?' 一个内置的ruby方法?我正在使用2.0.0.
谢谢你看一看并提出建议.
如何编写一个类似这样的检查...如果任何(或至少一个)驱动器类型是CDRom,那么真/继续......否则为假(抛出错误)?
现在,我为每个不通过CDRom要求的驱动器检查抛出了错误.我想我需要使用Any()的LINQ查询,但我不断收到错误.我可能没有正确地写它.
我的计划是在以下情况下抛出错误消息:
- 没有输入CD
- 计算机上没有CD-ROM驱动器
-CD输入为空白
-CD输入不包含所需的特定文件
到目前为止,这是我所希望的方式:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true && d.DriveType == DriveType.CDRom)
{
DirectoryInfo di = new DirectoryInfo(d.RootDirectory.Name);
var file = di.GetFiles("*.csv", SearchOption.AllDirectories).FirstOrDefault();
if (file == null)
{
errorwindow.Message = LanguageResources.Resource.File_Not_Found;
dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
}
else
{
foreach (FileInfo info in di.GetFiles("*.csv", SearchOption.AllDirectories))
{
Debug.Print(info.FullName);
ImportCSV(info.FullName);
break; // only looking for the first one
}
}
} …Run Code Online (Sandbox Code Playgroud) 如何使用LINQ的Contains()或Any()函数检查下面显示的数组或列表是否包含给定值?
这是我的代码:
int[] array3 = { 1, 2, 3, 4, 5, 6 };
List<int> tyest = array3.ToList<int>();
if (tyest.Any(2)) {
}
Run Code Online (Sandbox Code Playgroud)