rubyでやってたアレ、pythonでどうやる?

最近pythonでの仕事を初めて「rubyでやってたアレ、どうやるんだろう」と思う機会が何度かあったのでまとめておく

  • select

ruby

    ary = [1,2,3,4,5,6]
    ary.select { |e| e % 2 == 0 } # => [2,4,6]

python

    ary = [1,2,3,4,5,6]
    # リスト内包表記で
    [e for e in ary if e % 2 == 0]   # => [2,4,6]
    # filter関数で
    filter(lambda e: e % 2 == 0, ary) # => [2,4,6]
  • map

ruby

    ary = [ "a", "b", "c", "d" ]
    ary.map {|e| e + "!" }        #=> ["a!", "b!", "c!", "d!"]
    a                             #=> ["a", "b", "c", "d"]

python

    ary = [ "a", "b", "c", "d" ]
    # リスト内包表記で
    ["%s!" % e for e in ary]      #=> ["a!", "b!", "c!", "d!"]
    ary                           #=> ["a", "b", "c", "d"]
    # map関数で
    map(lambda e: "%s!" % e, ary) #=> ["a!", "b!", "c!", "d!"]
  • sort_by

ruby

    ary = %w{ apple pear fig }
    ary.sort_by {|word| word.length}  #=> ["fig", "pear", "apple"]
    ary                               #=> ["apple","pear","fig"]
    ary.sort_by!{|word| word.length}  #=> ["fig", "pear", "apple"]
    ary                               #=> ["fig", "pear", "apple"]

python

    ary = ["apple","pear","fig"]
    ary.sort(cmp=lambda a,b: cmp(len(a),len(b)))  #=> ["fig", "pear", "apple"]
    # または
    ary.sort(key=len)                             #=> ["fig", "pear", "apple"]
    # 破壊的
    ary                                           #=> ["fig", "pear", "apple"]
  • delete_if

ruby

    ary = [ "a", "b", "c" ]
    ary.delete_if {|e| e >= "b" }   #=> ["a"]
    # 破壊的メソッド
    ary                             #=> ["a"]

python

    ary = [ "a", "b", "c" ]
    # リスト内包表記で
    [e for e in ary if not e >= "b"]  #=> ["a"]
    ary                               #=> [ "a", "b", "c" ]
  • all?

ruby

    ary = ["ant","bear","cat"]
    ary.all?{|word| word.length >= 3}   #=> true
    ary.all?{|word| word.length >= 4}   #=> false

python

    ary = ["ant","bear","cat"]
    all([len(e) >= 3 for e in ary])      #=> True
    all([len(e) >= 4 for e in ary])      #=> False
  • any?

ruby

    ary = ["ant","bear","cat"]
    ary.any?{|word| word.length >= 3}   #=> true
    ary.any?{|word| word.length >= 4}   #=> true

python

    ary = ["ant","bear","cat"]
    any([len(e) >= 3 for e in ary])      #=> True
    any([len(e) >= 4 for e in ary])      #=> True
  • flatten

ruby

    s = [ 1, 2, 3 ]           #=> [1, 2, 3]
    t = [ 4, 5, 6, [7, 8] ]   #=> [4, 5, 6, [7, 8]]
    a = [ s, t, 9, 10 ]       #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
    a.flatten                 #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

python

    s = [ 1, 2, 3 ]           #=> [1, 2, 3]
    t = [ 4, 5, 6, [7, 8] ]   #=> [4, 5, 6, [7, 8]]
    a = [ s, t, 9, 10 ]       #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
 

    def flatten(array):
        res = []
        for e in ary:
            if isinstance(e, (list, tuple)):
                res.extend(flatten(e))
                continue
            res.append(e)
        return res

    a = [ 1, 2, [3, [4, 5] ] ]
    flatten(a)              #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

リスト内包表記はいろいろ応用が効いて便利なものですね