[英]Finding all possible permutations of a hash when given list of grouped elements
Best way to show what I'm trying to do: I have a list of different hashes that consist of ordered elements, seperated by an underscore.展示我正在尝试做的事情的最佳方式:我有一个由有序元素组成的不同哈希列表,由下划线分隔。 Each element may or may not have other possible replacement values.每个元素可能有也可能没有其他可能的替换值。 I'm trying to generate a list of all possible combinations of this hash, after taking into account replacement values.在考虑替换值后,我正在尝试生成此 hash 的所有可能组合的列表。
Example: grouped_elements = [["1", "1a", "1b"], ["3", "3a"]] original_hash = "1_2_3_4_5"示例:grouped_elements = [["1", "1a", "1b"], ["3", "3a"]] original_hash = "1_2_3_4_5"
I want to be able to generate a list of the following hashes:我希望能够生成以下哈希列表:
[
"1_2_3_4_5",
"1a_2_3_4_5",
"1b_2_3_4_5",
"1_2_3a_4_5",
"1a_2_3a_4_5",
"1b_2_3a_4_5",
]
The challenge is that this'll be needed on large dataframes.挑战在于大型数据帧需要这样做。
So far here's what I have:到目前为止,这就是我所拥有的:
def return_all_possible_hashes(df, grouped_elements)
rows_to_append = []
for grouped_element in grouped_elements:
for index, row in enriched_routes[
df["hash"].str.contains("|".join(grouped_element))
].iterrows():
(element_used_in_hash,) = set(grouped_element) & set(row["hash"].split("_"))
hash_used = row["hash"]
replacement_elements = set(grouped_element) - set([element_used_in_hash])
for replacement_element in replacement_elements:
row["hash"] = stop_hash_used.replace(
element_used_in_hash, replacement_element
)
rows_to_append.append(row)
return df.append(rows_to_append)
But the problem is that this will only append hashes with all combinations of a given grouped_element, and not all combinations of all grouped_elements at the same time.但问题是,这只会 append 散列与给定 grouped_element 的所有组合,而不是同时所有 grouped_elements 的所有组合。 So using the example above, my function would return:所以使用上面的例子,我的 function 会返回:
[
"1_2_3_4_5",
"1a_2_3_4_5",
"1b_2_3_4_5",
"1_2_3a_4_5",
]
I feel like I'm not far from the solution, but I also feel stuck, so any help is much appreciated!我觉得我离解决方案不远了,但我也觉得卡住了,所以非常感谢任何帮助!
If you make a list of the original hash value's elements and replace each element with a list of all its possible variations, you can use itertools.product
to get the Cartesian product across these sublists.如果您列出原始 hash 值的元素并用所有可能变化的列表替换每个元素,则可以使用itertools.product
跨这些子列表获取笛卡尔积。 Transforming each element of the result back to a string with '_'.join()
will get you the list of possible hashes:使用'_'.join()
将结果的每个元素转换回字符串将为您提供可能的哈希列表:
from itertools import product
def possible_hashes(original_hash, grouped_elements):
hash_list = original_hash.split('_')
variations = list(set().union(*grouped_elements))
var_list = hash_list.copy()
for i, h in enumerate(hash_list):
if h in variations:
for g in grouped_elements:
if h in g:
var_list[i] = g
break
else:
var_list[i] = [h]
return ['_'.join(h) for h in product(*var_list)]
possible_hashes("1_2_3_4_5", [["1", "1a", "1b"], ["3", "3a"]])
['1_2_3_4_5',
'1_2_3a_4_5',
'1a_2_3_4_5',
'1a_2_3a_4_5',
'1b_2_3_4_5',
'1b_2_3a_4_5']
To use this function on various original hash values stored in a dataframe column, you can do something like this:要在存储在 dataframe 列中的各种原始 hash 值上使用此 function,您可以执行以下操作:
df['hash'].apply(lambda x: possible_hashes(x, grouped_elements))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.