LoginSignup
0
0

More than 1 year has passed since last update.

[py2rb] set の差異

Last updated at Posted at 2021-12-08

はじめに

移植やってます

set (Python)

_comments = set('>;')

何気ないsetですが、

>>> print(_comments)
{'>', ';'}

中は既にスプリットされています。

set (Ruby)

require 'set'
_comments = Set.new('>;'.split(''))

二つくらいなら最初から分けておいてもいいんですけど。

intersection (Python)

x = {'is_decoy', 'remove_decoy', 'formula',
                 'ratio', 'correction'}.intersection(kwargs)

intersectionって集合の積なんですけど、このkwargsって辞書型なんですよね。

よくよく見るとSet型でした。
やはり、辞書型でしたが、intersectionを呼んだとき、辞書のキーとの積を取得するようです。

& (Ruby)

x = Set.new(['is_decoy', 'remove_decoy', 'formula',
        'ratio', 'correction']) & kwargs.keys

SetHashの積は取れませんので、Hash#keysを使用します。
この場合、Set.new(Array & Hash.keys)でもSet.new(Array) & Hash.keysでも同じSetが返るようです。

メモ

  • Python の set を学習した
  • 道のりは遠そう
0
0
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0