简体   繁体   English

两个列表元素之间的关系:如何在Python中利用它?

[英]Relationship between elements of two list: how to exploit it in Python?

SO here is my minimal working example: 所以这是我的最小工作示例:

# I have a list
list1 = [1,2,3,4]
#I do some operation on the elements of the list
list2 = [2**j for j in list1]
# Then I want to have these items all shuffled around, so for instance
list2 = np.random.permutation(list2)

#Now here is my problem: I want to understand which element of the new list2 came from which element of list1. I am looking for something like this:

list1.index(something)

# Basically given an element of list2, I want to understand from where it came from, in list1. I really cant think of a simple way of doing this, but there must be an easy way!

Can you please suggest me an easy solution? 您能建议我一个简单的解决方案吗? This is a minimal working example,however the main point is that I have a list, I do some operation on the elements and assign these to a new list. 这是一个最小的工作示例,但是主要要点是我有一个列表,我对元素进行了一些操作并将它们分配给新列表。 And then the items get all shuffled around and I need to understand where they came from. 然后,所有物品都乱七八糟,我需要了解它们的来源。

You could use a permutation list/index : 您可以使用排列列表/索引:

# I have a list
list1 = [1,2,3,4]
#I do some operation on the elements of the list
list2 = [2**j for j in list1]
# Then I want to have these items all shuffled around, so for instance
index_list = range(len(list2))
index_list = np.random.permutation(index_list)
list3 = [list2[i] for i in index_list]

then,with input_element : 然后,使用input_element

answer = index_list[list3.index(input_element)]

enumerate, like everyone said is the best option but there is an alternative if you know the mapping relation. 枚举,就像每个人都说的是最好的选择,但是如果您知道映射关系,那么还有另一种选择。 You can write a function that does the opposite of the mapping relation. 您可以编写一个与映射关系相反的函数。 (eg. decodes if the original function encodes.) Then you use decoded_list = map(decode_function,encoded_list) to get a new list. (例如,如果原始函数进行了编码,则进行解码。)然后,使用decoded_list = map(decode_function,encoded_list)获得新列表。 Then by cross comparing this list with the original list, you can achieve your goal. 然后通过将此列表与原始列表进行交叉比较,可以实现您的目标。

Enumerate is better if you are certain that the same list was modified using the encode_function from within the code to get the encoded list. 如果您确定使用代码中的encode_function修改了相同列表以获取编码列表,则枚举会更好。 However, if you are importing this new list from elsewhere, eg. 但是,如果要从其他位置导入此新列表,例如 from a table on a website, my approach is the way to go. 从网站上的表格中,我的方法是要走的路。

Based on your code: 根据您的代码:

# I have a list
list1 = [1,2,3,4]
#I do some operation on the elements of the list
list2 = [2**j for j in list1]
# made a recode of index and value
index_list2 = list(enumerate(list2))
# Then I want to have these items all shuffled around, so for instance
index_list3 = np.random.permutation(index_list2)
idx, list3 = zip(*index_list3)

#get the index of element_input in list3, then get the value of the index in idx, that should be the answer you want.
answer = idx[list3.index(element_input)]
def index3_to_1(index):
    y = list3[index]
    x = np.log(y)/np.log(2)  # inverse y=f(x) for your operation
    return list1.index(x)  

This supposes that the operations you are doing on list2 are reversible. 这假定您在list2上执行的操作是可逆的。 Also, it supposes that each element in list1 is unique. 同样,它假定list1中的每个元素都是唯一的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何识别python中两个列表之间的关系/映射? - how to identify relationship/mapping between the two list in python? 如何在Python的列表中重复查找两个元素之间的所有元素? - How to find all elements between two elements repeatedly in a list in Python? 如何在python中检索两个数字之间的列表元素? - How can i retrieve elements of a list between two numbers in python? 如何使用 python 的“列表”添加新列并找到元素之间的对应关系? - How to use “list” of python to add a new column with finding the corresponding relationship between the elements ? 如何在 python 列表中的 2 个元素之间插入元素? - How to insert element between 2 elements in a python list? 如何优化具有两个元素的列表之间的交集,并在python中生成没有重复的列表列表? - How can I optimize the intersections between lists with two elements and generate a list of lists without duplicates in python? Python - 如何区分指向同一对象的两个列表元素? - Python - How do I differentiate between two list elements that point to the same object? Python-使用递归填充两个元素之间的列表中的空格 - Python- Filling Spaces in a List Between Two Elements with Recursion Python 匹配两个列表之间的列表元素中的部分字符串 - Python matching partial strings in list elements between two lists 检测元素是否在python列表中的两个相等元素之间 - detect if element is between two equal elements in python list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM