简体   繁体   English

在python中的列表之间映射的最快方法是什么

[英]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_1blist_2alist_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_listb列表中的值。 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]
  • Note that some of these lists can contain more than a million entries (So maybe memory can be an issue) 请注意,其中某些列表可能包含超过一百万个条目(因此,内存可能是一个问题)
  • The lists do not have the same length 列表长度不一样
  • All of the integer values in these lists are unique to their lists, ie 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.

相关问题 从Python中的列表列表中获取最小值和最大值的最快方法? - Quickest way to get the min and max from a list of lists of lists in Python? 在 Python 中 HTTP GET 的最快方法是什么? - What is the quickest way to HTTP GET in Python? 在python中两个列表之间映射值的有效方法 - efficient way to map values between two lists in python Python:列之间有三个不同的分隔符,列到列表,最快的方式 - Python: three different delimiters between columns, column to list, quickest way 什么是检查类型(datetime.date)python的最快方法 - what is the quickest way to check for type(datetime.date) python 在 Python 中评估极坐标网格上的插值函数的最快方法是什么? - What is the quickest way to evaluate an interpolated function on a polar grid in Python? Ruby程序员选择Python最快的方法是什么? - What's the quickest way for a Ruby programmer to pick up Python? 在python中获取具有唯一数字的数字的最快方法是什么? - What is the quickest way to get a number with unique digits in python? 我有一个不等长列表的数组,什么是计算每个列表长度的最快方法 - I have an array of unequal length lists, what is quickest way to calculate the length of each 将查找表映射到pandas列的最快方法 - Quickest way to map lookup table to pandas column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM