【发布时间】:2020-04-17 10:39:16
【问题描述】:
我有两个列表,它们都包含来自同一类的对象。我想将它们组合在第三个列表中,其中包含具有相同属性值的对象的列表或元组。
例子
Object1.time = 1
Object2.time = 2
Object3.time = 1
Object4.time = 2
Objekt5.time = 3
list1 = [Object1, Object2]
list2 = [Object3,Object4]
排序结果应该是这样的:
result_list = [[Object1,Object3], [Object2,Object4], [Object5]]
我需要提一下:我不需要只包含一个对象的列表!
因此,最终列表应如下所示:
final_result = [[Objekt1, Objekt3], [Objekt2, Objekt4]]
List1 包含 1500 个对象,List2 超过 70,000 个问题是:如果我使用两个 for 循环来比较对象,则花费的时间太长。
这是我的低效示例:
class Example:
def __init__(self,time,example_attribute):
self.time = time
self.example_attribute = example_attribute
test_list1 = [1,1,2,3,4,5,6,6,7,8,9,9]
test_list2 = ["a","b","c","d","e","f","d","e","f","g","h","i"]
test_list3 = ["j","k","l","m","n","o","p","q","r","s","t","u"]
object_list1 = []
for i,j in zip(test_list1,test_list2):
object_list1.append(Example(i,j))
object_list2 = []
for i,j in zip(test_list1,test_list3):
object_list2.append(Example(i,j))
# How to group both lists together by the time attribute? This part takes too long.
group_by_time = []
for i in object_list1:
my_list = [i]
for j in object_list2:
if i.time == j.time:
my_list.append(j)
group_by_time.append(my_list)
for sub_list in group_by_time:
for index, item in enumerate(sub_list):
if index == 0:
print(item.time, ",",item.example_attribute,end =",")
else:print(item.example_attribute, end = ",")
print("")```
【问题讨论】:
标签: python performance loops class for-loop