设置差异运算符不采取差异(似乎什么都不做)?
set difference operator not taking difference (appears to do nothing)?
尝试在 python 中做一个简单的 set
差分,得到的结果表明差分运算符什么也没做。例如。有代码
python 版本 2.7.15+
assert isinstance(left_frame, h2o.H2OFrame)
assert isinstance(right_frame, h2o.H2OFrame)
assert isinstance(left_key, str)
assert isinstance(right_key, str)
# ensure that the primary_key exists in both frames
assert left_key in left_frame.columns, 'left_key: {} does not exist in left_frame'.format(left_key)
assert right_key in right_frame.columns, 'right_key: {} does not exist in right_frame'.format(right_key)
# ensure that the primary_key is the only common column between the left and right frame
left_non_pk_cols = set(left_frame.columns) - set(left_key)
assert left_on not in left_non_pk_cols, '%s' % left_key
right_non_pk_cols = set(right_frame.columns) - set(right_key)
assert right_on not in right_non_pk_cols, '%s' % right_key
left_non_pk_cols_in_right = left_non_pk_cols.intersection(right_non_pk_cols)
assert len(left_non_pk_cols_in_right) == 0,\
'The primary_key is not the only common column between frames, h2o merge will not work as expected\n%s\n%s\n%s' \
% (left_non_pk_cols, right_non_pk_cols, left_non_pk_cols_in_right)
我收到错误
assert left_key not in left_non_pk_cols, '%s' % left_key
AssertionError: <the left_key value>
这对我来说真的很奇怪。 运行 在终端中(具有相同的 python 版本)用于简化示例案例
assert u'1' not in (set([u'1', u'2', u'3']) - set(u'1'))
# noting that the H2OFrames `.columns` field is a list of unicode strings
完全没有错误并按预期完成(打印结果 set
时,一切看起来都应该如此(没有 u'1'
元素))。
使用 .difference()
方法而不是 -
运算符也不会产生任何差异。
有谁知道这里会发生什么或需要做些什么来获取更多调试信息?
set()
的参数是一个可迭代对象,它创建了一组可迭代对象的每个元素。因此,如果 left_key
是一个字符串,set(left_key)
将创建一个包含字符串中每个唯一字符的集合,而不是一个以字符串为元素的集合。
解决方案是使用set([left_key])
。参数将是列表,然后集合将包含它的单个元素,即字符串。或者你可以只使用一个集合文字 {left_key}
left_non_pk_cols = set(left_frame.columns) - {left_key}
另一种方法是只从集合中删除元素。
left_non_pk_cols = set(left_frame.columns)
left_non_pk.cols.discard(left_key)
我使用 discard
而不是 remove
,因为如果找不到该元素,它不会发出错误信号。
尝试在 python 中做一个简单的 set
差分,得到的结果表明差分运算符什么也没做。例如。有代码
python 版本 2.7.15+
assert isinstance(left_frame, h2o.H2OFrame)
assert isinstance(right_frame, h2o.H2OFrame)
assert isinstance(left_key, str)
assert isinstance(right_key, str)
# ensure that the primary_key exists in both frames
assert left_key in left_frame.columns, 'left_key: {} does not exist in left_frame'.format(left_key)
assert right_key in right_frame.columns, 'right_key: {} does not exist in right_frame'.format(right_key)
# ensure that the primary_key is the only common column between the left and right frame
left_non_pk_cols = set(left_frame.columns) - set(left_key)
assert left_on not in left_non_pk_cols, '%s' % left_key
right_non_pk_cols = set(right_frame.columns) - set(right_key)
assert right_on not in right_non_pk_cols, '%s' % right_key
left_non_pk_cols_in_right = left_non_pk_cols.intersection(right_non_pk_cols)
assert len(left_non_pk_cols_in_right) == 0,\
'The primary_key is not the only common column between frames, h2o merge will not work as expected\n%s\n%s\n%s' \
% (left_non_pk_cols, right_non_pk_cols, left_non_pk_cols_in_right)
我收到错误
assert left_key not in left_non_pk_cols, '%s' % left_key
AssertionError: <the left_key value>
这对我来说真的很奇怪。 运行 在终端中(具有相同的 python 版本)用于简化示例案例
assert u'1' not in (set([u'1', u'2', u'3']) - set(u'1'))
# noting that the H2OFrames `.columns` field is a list of unicode strings
完全没有错误并按预期完成(打印结果 set
时,一切看起来都应该如此(没有 u'1'
元素))。
使用 .difference()
方法而不是 -
运算符也不会产生任何差异。
有谁知道这里会发生什么或需要做些什么来获取更多调试信息?
set()
的参数是一个可迭代对象,它创建了一组可迭代对象的每个元素。因此,如果 left_key
是一个字符串,set(left_key)
将创建一个包含字符串中每个唯一字符的集合,而不是一个以字符串为元素的集合。
解决方案是使用set([left_key])
。参数将是列表,然后集合将包含它的单个元素,即字符串。或者你可以只使用一个集合文字 {left_key}
left_non_pk_cols = set(left_frame.columns) - {left_key}
另一种方法是只从集合中删除元素。
left_non_pk_cols = set(left_frame.columns)
left_non_pk.cols.discard(left_key)
我使用 discard
而不是 remove
,因为如果找不到该元素,它不会发出错误信号。