简体   繁体   English

给定分组元素列表时,查找 hash 的所有可能排列

[英]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.

相关问题 在 python 中查找给定字符串的所有可能排列 - Finding all possible permutations of a given string in python 当给定一个返回列表的下一个排列的函数时,查找列表的所有排列 - Finding All permutations of a list when given a function that returns the next permutation of a list 列表的所有可能排列中的排序元素数(排列) - Number of sorted elements (permutations), in all possible permutations of a list 查找固定长度数字的所有可能排列以达到给定的总和 - Finding all possible permutations of a fixed length of numbers to reach a given sum 找到给定编号的所有可能排列和组合。 使用 Python 的列表中的元素 - Find all possible permutations and combinations of given no. of elements in a list using Python 用3个常数找到所有可能的排列 - Finding all possible permutations with 3 Constants 列表的相邻元素中的所有排列 - All permutations in adjacent elements of list 查找不固定长度的数字的所有可能排列以达到给定的总和或乘积 - Finding all possible permutations of an unfixed length of numbers to reach a given sum or product 如何查找具有静态角点元素的4x4矩阵的所有可能排列? - How would I go about finding all possible permutations of a 4x4 matrix with static corner elements? 在给定范围内找到列表的所有可能子列表的高效和Pythonic方法以及在将所有元素相乘后的最小乘积? - Efficient & Pythonic way of finding all possible sublists of a list in given range and the minimum product after multipying all elements in them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM