[英]What is the quickest way to map between lists in python
I have pairs of 4 lists a and b with integer values such as list_1a = [1,2,3,...]
and list_1b = [8,11,15,...]
. 我有4个列表a和b对,它们具有整数值,例如
list_1a = [1,2,3,...]
和list_1b = [8,11,15,...]
。 The idea is that the integer values in list_1a
are now represented by the integer values in list_1b
, and the same for list_2a
and list_2b
etc. 这个想法是
list_1a
中的整数值现在由list_1a
中的整数值list_1b
, list_2a
和list_2b
等中的整数值也是list_2b
。
Now I have a list of 4 columns final_list
which contained integer values corresponding to the a
lists. 现在,我有一个4列
final_list
的列表,其中包含与a
列表相对应的整数值。 I want to map the values in final_list
to the values in the b
lists. 我想将
final_list
中的值final_list
到b
列表中的值。 What is the quickest way to do this in python ? 在python中最快的方法是什么?
Is there a quicker way than using lists ? 有没有比使用列表更快的方法?
Edit: 编辑:
To clarify the question, take the following example: 为了阐明问题,请使用以下示例:
list_1a = [1,2,3]
list_1b = [8,11,15]
list_2a = [5,6,7,8]
list_2b = [22,26,30,34]
list_3a = [11,12,13,14,18]
list_3b = [18,12,25,28,30]
list_4a = [51,61,72,82]
list_4b = [73,76,72,94]
list_1a + list_1b
will never have a repeating integer value. list_1a + list_1b
将永远不会有重复的整数值。 final_list
should look like final_list_b
after the mapping occurs 发生映射后,
final_list
外观应类似于final_list_b
final_list_a = [[1,6,11,51],[3,6,14,72]]
final_list_b = [[8,26,18,73],[15,26,28,72]]
To put things into perspective, this questions is for a database application where these "lists" contain auto-generated key values 直观地讲,这个问题是针对数据库应用程序的,这些“列表”包含自动生成的键值
I think what you want is a dictionary , which associates keys with values. 我认为您想要的是一本字典 ,它将键与值相关联。 Unless i'm confused about what you want to do here.
除非我对您要在这里做什么感到困惑。
So if I make 4 short example lists. 因此,如果我列出了4个简短的示例列表。
list_1a = [1,2,3,4]
list_1b = [8,11,15,18]
list_2a = [5,6,7,8]
list_2b = [22,26,30,34]
and make them into a big list of all "a" values and all "b" values. 并将它们放入所有“ a”值和所有“ b”值的大列表中。
a_list = list_1a + list_2a
b_list = list_1b + list_2b
I can then use zip to merge the lists into a dictionary 然后,我可以使用zip将列表合并到字典中
my_dict = dict(zip(a_list, b_list))
print(my_dict)
See: how to merge 2 list as a key value pair in python 请参阅: 如何在Python中将2个列表合并为键值对
for some other ways to do this last bit. 最后一些其他方法。
result: 结果:
{1: 8, 2: 11, 3: 15, 4: 18, 5: 22, 6: 26, 7: 30, 8: 34}
Now your "a" list makes up the keys of this dictionary.. while the "b" list make up the values. 现在,您的“ a”列表构成了该词典的键..而“ b”列表则构成了这些值。 You can access the values by using the keys.
您可以使用键访问值。 here's some examples.
这是一些例子。
print(my_dict.keys())
print(my_dict.values())
print(my_dict[5])
gives me: 给我:
[1, 2, 3, 4, 5, 6, 7, 8]
[8, 11, 15, 18, 22, 26, 30, 34]
22
Is this what you want? 这是你想要的吗?
EDIT: I feel that I should note that while my dictionary has printed in order, dictionaries are actually not ordered like lists. 编辑:我觉得我应该注意,虽然我的字典是按顺序打印的,但是字典实际上并不像列表那样排序。 You might want to look into collections.OrderedDict or sorted if this is important to you.
您可能想要查看collections.OrderedDict或排序(如果这对您很重要)。
Update: 更新:
For what you want to do, maybe consider nested dictionaries. 对于您想做的事情,也许考虑嵌套字典。 You can make a dictionary whose values are dictionaries, also note that when 1a and 1b don't match in length,
zip
doesn't care and just excludes 60: 您可以制作一个字典值为字典的字典,还请注意,如果1a和1b的长度不匹配,则
zip
无关紧要,仅排除60:
list_1a = [1,2,3,4]
list_1b = [8,11,15,18,60]
list_2a = [5,6,7,8]
list_2b = [22,26,30,34]
a_dict = dict(zip(list_1a, list_2a))
b_dict = dict(zip(list_1b, list_2b))
my_dict = {"a" : a_dict, "b" : b_dict}
print(my_dict)
Result: 结果:
{'a': {1: 5, 2: 6, 3: 7, 4: 8}, 'b': {8: 22, 18: 34, 11: 26, 15: 30}}
Now you can access the inner values in a different way: 现在,您可以通过其他方式访问内部值:
print(my_dict["a"].keys())
print(my_dict["a"].values())
print(my_dict["a"][4])
Result: 结果:
[1, 2, 3, 4]
[5, 6, 7, 8]
8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.