Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套数组Ruby中具有相同字符的所有索引_Ruby_Arrays_Multidimensional Array - Fatal编程技术网

嵌套数组Ruby中具有相同字符的所有索引

嵌套数组Ruby中具有相同字符的所有索引,ruby,arrays,multidimensional-array,Ruby,Arrays,Multidimensional Array,我想看看一个索引,比如索引3,是否都有相同的字符。例如: nested_array = [[47, 44, 71, 'x', 88], [22, 69, 75, 'x', 73], [83, 85, 97, 'x', 57], [25, 31, 96, 'x', 51], [75, 70, 54, 'x', 83]] 我们可以看到,索引3中包含了所

我想看看一个索引,比如索引3,是否都有相同的字符。例如:

nested_array =   [[47, 44, 71, 'x', 88],
                  [22, 69, 75, 'x', 73],
                  [83, 85, 97, 'x', 57],
                  [25, 31, 96, 'x', 51],
                  [75, 70, 54, 'x', 83]]
我们可以看到,索引
3
中包含了所有
x'x
,而不是数字。我希望能够验证所有索引是否都有
x
,如果是,程序将返回true。如果除1之外的所有对象都有
x
,则返回false

我现在已经实现了这个条件语句,因为它遍历每个索引,寻找大小等于1的值。这似乎奏效了

if array.map {|zero| zero[0]}.uniq.size == 1
  return true
elsif array.map {|one| one[1]}.uniq.size == 1
  return true
elsif array.map {|two| two[2]}.uniq.size == 1
  return true
elsif array.map {|three| three[3]}.uniq.size == 1
  return true
elsif array.map {|four| four[4]}.uniq.size == 1
  return true
else
  return false
end
空数组的特殊条件是必要的,因为它们通常满足为每个元素指定的任何条件,但唯一性测试不会检测到它。如果您实际上没有空的数组,请随意删除它,或者如果数组为空,测试将失败

编辑:似乎我误解了这个问题-如果:

nested_array.transpose.any? { |column| column.uniq.one? }
翻转行和列时,是否有任何行(以前是列)只有一个唯一的元素

(如果希望
true
表示
[]
,请再次使用
nested_array.empty?| |……
作为前缀)

编辑:多亏了斯皮克曼,将
.size==1
替换为
.one?
。它确实读起来更好

空数组的特殊条件是必要的,因为它们通常满足为每个元素指定的任何条件,但唯一性测试不会检测到它。如果您实际上没有空的数组,请随意删除它,或者如果数组为空,测试将失败

编辑:似乎我误解了这个问题-如果:

nested_array.transpose.any? { |column| column.uniq.one? }
翻转行和列时,是否有任何行(以前是列)只有一个唯一的元素

(如果希望
true
表示
[]
,请再次使用
nested_array.empty?| |……
作为前缀)

编辑:多亏了斯皮克曼,将
.size==1
替换为
.one?
。它确实读起来更好

空数组的特殊条件是必要的,因为它们通常满足为每个元素指定的任何条件,但唯一性测试不会检测到它。如果您实际上没有空的数组,请随意删除它,或者如果数组为空,测试将失败

编辑:似乎我误解了这个问题-如果:

nested_array.transpose.any? { |column| column.uniq.one? }
翻转行和列时,是否有任何行(以前是列)只有一个唯一的元素

(如果希望
true
表示
[]
,请再次使用
nested_array.empty?| |……
作为前缀)

编辑:多亏了斯皮克曼,将
.size==1
替换为
.one?
。它确实读起来更好

空数组的特殊条件是必要的,因为它们通常满足为每个元素指定的任何条件,但唯一性测试不会检测到它。如果您实际上没有空的数组,请随意删除它,或者如果数组为空,测试将失败

编辑:似乎我误解了这个问题-如果:

nested_array.transpose.any? { |column| column.uniq.one? }
翻转行和列时,是否有任何行(以前是列)只有一个唯一的元素

(如果希望
true
表示
[]
,请再次使用
nested_array.empty?| |……
作为前缀)


编辑:多亏了斯皮克曼,将
.size==1
替换为
.one?
。它确实读起来更好。

您没有指定嵌套数组的所有元素都必须具有相同的大小,因此我将提供一个解决方案,该解决方案没有要求:

nested_array =   [[47, 44, 71, 'x', 88],
                  [75, 70, 54, 'x', 83, 85, 90],
                  [22, 69, 75, 'x', 73],
                  [83, 85, 97, 'x', 57],
                  [25, 31, 96, 'x', 51, 33]]

nested_array.first.zip(*nested_array[1..-1]).any? {|row| row.uniq.size==1}
  #=> true
我们有:

b = nested_array.first.zip(*nested_array[1..-1])
  #=> [[47, 75, 22, 83, 25],
  #    [44, 70, 69, 85, 31],
  #    [71, 54, 75, 97, 96],
  #    ["x", "x", "x", "x", "x"],
  #    [88, 83, 73, 57, 51]] 

b.any? { |row| row.uniq.size == 1 }
  #=> true
首先,我有:

nested_array.first.zip(*nested_array[1..-1]).any? {|row|
  row[1..-1].all? { |e| e == row.first } }
而不是
{| row | row.uniq.size==1}
。正如@Amadan指出的,前者应该快一点

如果问题是
b
的特定元素是否包含所有相同的值:

index = 3
a = nested_array.first.zip(*nested_array[1..-1])
(index >= a.size) ? false : a[index].uniq.size==1
  #=> true

您没有指定嵌套数组的所有元素都必须具有相同的大小,因此我将提供一个解决方案,该解决方案没有要求:

nested_array =   [[47, 44, 71, 'x', 88],
                  [75, 70, 54, 'x', 83, 85, 90],
                  [22, 69, 75, 'x', 73],
                  [83, 85, 97, 'x', 57],
                  [25, 31, 96, 'x', 51, 33]]

nested_array.first.zip(*nested_array[1..-1]).any? {|row| row.uniq.size==1}
  #=> true
我们有:

b = nested_array.first.zip(*nested_array[1..-1])
  #=> [[47, 75, 22, 83, 25],
  #    [44, 70, 69, 85, 31],
  #    [71, 54, 75, 97, 96],
  #    ["x", "x", "x", "x", "x"],
  #    [88, 83, 73, 57, 51]] 

b.any? { |row| row.uniq.size == 1 }
  #=> true
首先,我有:

nested_array.first.zip(*nested_array[1..-1]).any? {|row|
  row[1..-1].all? { |e| e == row.first } }
而不是
{| row | row.uniq.size==1}
。正如@Amadan指出的,前者应该快一点

如果问题是
b
的特定元素是否包含所有相同的值:

index = 3
a = nested_array.first.zip(*nested_array[1..-1])
(index >= a.size) ? false : a[index].uniq.size==1
  #=> true

您没有指定嵌套数组的所有元素都必须具有相同的大小,因此我将提供一个解决方案,该解决方案没有要求:

nested_array =   [[47, 44, 71, 'x', 88],
                  [75, 70, 54, 'x', 83, 85, 90],
                  [22, 69, 75, 'x', 73],
                  [83, 85, 97, 'x', 57],
                  [25, 31, 96, 'x', 51, 33]]

nested_array.first.zip(*nested_array[1..-1]).any? {|row| row.uniq.size==1}
  #=> true
我们有:

b = nested_array.first.zip(*nested_array[1..-1])
  #=> [[47, 75, 22, 83, 25],
  #    [44, 70, 69, 85, 31],
  #    [71, 54, 75, 97, 96],
  #    ["x", "x", "x", "x", "x"],
  #    [88, 83, 73, 57, 51]] 

b.any? { |row| row.uniq.size == 1 }
  #=> true
首先,我有:

nested_array.first.zip(*nested_array[1..-1]).any? {|row|
  row[1..-1].all? { |e| e == row.first } }
而不是
{| row | row.uniq.size==1}
。正如@Amadan指出的,前者应该快一点

如果问题是
b
的特定元素是否包含所有相同的值:

index = 3
a = nested_array.first.zip(*nested_array[1..-1])
(index >= a.size) ? false : a[index].uniq.size==1
  #=> true

您没有指定嵌套数组的所有元素都必须具有相同的大小,因此我将提供一个解决方案,该解决方案没有要求:

nested_array =   [[47, 44, 71, 'x', 88],
                  [75, 70, 54, 'x', 83, 85, 90],
                  [22, 69, 75, 'x', 73],
                  [83, 85, 97, 'x', 57],
                  [25, 31, 96, 'x', 51, 33]]

nested_array.first.zip(*nested_array[1..-1]).any? {|row| row.uniq.size==1}
  #=> true
我们有:

b = nested_array.first.zip(*nested_array[1..-1])
  #=> [[47, 75, 22, 83, 25],
  #    [44, 70, 69, 85, 31],
  #    [71, 54, 75, 97, 96],
  #    ["x", "x", "x", "x", "x"],
  #    [88, 83, 73, 57, 51]] 

b.any? { |row| row.uniq.size == 1 }
  #=> true
首先,我有:

nested_array.first.zip(*nested_array[1..-1]).any? {|row|
  row[1..-1].all? { |e| e == row.first } }
而不是
{| row | row.uniq.size==1}
。正如@Amadan指出的,前者应该快一点

如果问题是
b
的特定元素是否包含所有相同的值:

index = 3
a = nested_array.first.zip(*nested_array[1..-1])
(index >= a.size) ? false : a[index].uniq.size==1
  #=> true

这实际上似乎起了作用。我确实需要对其进行一些调整,并将其以条件格式应用于每个索引,如下所示:if array.map{zero}.uniq.size==1返回真的elsif array.map{one | one[1]}.uniq.size==1返回真的elsif array.map{two | two[2]}.uniq.size==1 return true elsif array.map{| three | three[3]}.uniq.size==1 return true elsif array.map{| three | three[4]}.uniq.size==1 return true else return false endWait,所以您想测试是否有任何列都相等,而不是一列具有您知道的索引?例如:Winner=5