同步两个数组

时间:2018-08-14 19:39:41

标签: arrays ruby

我正在比较数组以进行同步。使用

arr1 = ['a', 'a', 'a', 'b', 'c', 'f']
arr2 = ['a', 'b', 'b', 'c', 'd']

我不知道如何同步这些数组。我必须弄清楚哪些元素需要添加到一个数组中,同时要记住允许重复。我需要arr1中的不同元素,但不需要arr2中的不同元素:

['a', 'a', 'f']

arr2中的不同元素,而不是arr1中的不同元素:

['b', 'd']

不幸的是,-函数不能用于数组:

arr1 - arr2 # =>  ['f']
arr2 - arr1 # =>  ['d']

4 个答案:

答案 0 :(得分:1)

这里是TNT's elegant solution的实现,用于删除数组的第一个匹配元素

class Array
  def delete_first item
    delete_at(index(item) || length)
  end
  def distinct other, own = self.dup
    other.each{|e| own.delete_first(e)}
    own
  end
end

arr1.distinct arr2 # ["a", "a", "f"]
arr2.distinct arr1 # ["b", "d"]

答案 1 :(得分:0)

这不漂亮!

def array_compare(arr1, arr2)
  arr2 = arr2.clone
  add_to = []
  arr1.each do |el|
    if index = arr2.index(el)
      arr2.delete_at(index)
    else
      add_to << el
    end
  end
  return add_to
end

-> arr1 = ['a', 'a', 'a', 'b', 'c,', 'f']
["a", "a", "a", "b", "c,", "f"]

-> arr2 = ['a', 'b', 'b', 'c,', 'd']
["a", "b", "b", "c,", "d"]

-> array_compare(arr1, arr2)
["a", "a", "f"]

-> array_compare(arr2, arr1)
["b", "d"]

答案 2 :(得分:0)

这是一种方法:

  1. 计算每个数组中的所有出现次数,并将其存储在2个散列中
  2. 遍历哈希1并将每个计数与哈希2进行比较
  3. 为哈希1中的每个其他计数输出一个数组项
  4. 展平并输出

实施:

h1 = arr1.inject(Hash.new(0)) { |total, e| total[e] += 1 ;total }
# => {"a"=>3, "b"=>1, "c"=>1, "f"=>1
h2 = arr2.inject(Hash.new(0)) { |total, e| total[e] += 1 ;total }
# => {"a"=>1, "b"=>2, "c"=>1, "d"=>1}

h1.map { |k, v| [k] * [v - h2[k], 0].max }.flatten
# => ["a", "a", "f"]
h2.map { |k, v| [k] * [v - h1[k], 0].max }.flatten
# => ["b", "d"]

答案 3 :(得分:0)

当允许重复时,Ruby的常规Array算法几乎没有用。 尝试单独删除元素:

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); 
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at 
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
==============================================================================
"""Simple MNIST classifier example with JIT XLA and timelines.

"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.client import timeline

FLAGS = None


def main(_):
  # Import data
  with tf.device("/job:localhost/replica:0/task:0/device:XLA_CPU:0"):
    mnist = input_data.read_data_sets(FLAGS.data_dir)

    # Create the model
    x = tf.placeholder(tf.float32, [None, 784])
    w = tf.get_variable("w",initializer=tf.zeros([784, 10]),use_resource=True)
    b = tf.get_variable("b",initializer=tf.zeros([10]),use_resource=True)
    y = tf.matmul(x, w) + b

    # Define loss and optimizer
    y_ = tf.placeholder(tf.int64, [None])

    # The raw formulation of cross-entropy,
    #
    #   tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),
    #                                 reduction_indices=[1]))
    #
    # can be numerically unstable.
    #
    # So here we use tf.losses.sparse_softmax_cross_entropy on the raw
    # logit outputs of 'y', and then average across the batch.
    cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=y_, logits=y)
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

    config = tf.ConfigProto()
    jit_level = 0
    if FLAGS.xla:
      # Turns on XLA JIT compilation.
      jit_level = tf.OptimizerOptions.ON_1

    config.graph_options.optimizer_options.global_jit_level = jit_level
    run_metadata = tf.RunMetadata()
    sess = tf.Session(config=config)
    tf.global_variables_initializer().run(session=sess)
    # Train
    g = tf.Graph()
    print(dir(g))
    train_loops = 1000
    for i in range(train_loops):
      batch_xs, batch_ys = mnist.train.next_batch(100)

      # Create a timeline for the last loop and export to json to view with
      # chrome://tracing/.
      if i == train_loops - 1:
        sess.run(train_step,
                 feed_dict={x: batch_xs,
                            y_: batch_ys},
                 options=tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE),
                 run_metadata=run_metadata)
        trace = timeline.Timeline(step_stats=run_metadata.step_stats)
        with open('timeline.ctf.json', 'w') as trace_file:
          trace_file.write(trace.generate_chrome_trace_format())
      else:
        sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

    # Test trained model
    correct_prediction = tf.equal(tf.argmax(y, 1), y_)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print(sess.run(accuracy,
                   feed_dict={x: mnist.test.images,
                              y_: mnist.test.labels}))
    sess.close()


if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--data_dir',
      type=str,
      default='/tmp/tensorflow/mnist/input_data',
      help='Directory for storing input data')
  parser.add_argument(
      '--xla', type=bool, default=True, help='Turn xla via JIT on')
  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

希望您会有所帮助。