[英]how to generate list of lists from conditional items in another list of lists
I have a list of lists and am trying to make another list of lists from specific items within the first list of lists 我有一个列表列表,并且正在尝试从列表的第一个列表中的特定项目制作另一个列表列表
listOne = [[1,1,9,9],[1,4,9,6],[2,1,12,12]]
listTwo = []
for every inner list with the same numbers in positions 0
and 2
, append to listTwo only the inner list with the largest value in position 3
对于在位置
0
和2
具有相同数字的每个内部列表,追加到listTwo仅在位置3
具有最大值的内部列表
for example, inner list 0
and inner list 1
both have a 1
in position 0
and a 9
in position 2
, but inner list 0
has a 9
in position 3
and inner list 1
has a 6
in position 3
so I want to append inner list 1
and not inner list 9
to listTwo
. 例如,
inner list 0
和inner list 1
都具有1
在position 0
和9
在position 2
,但inner list 0
具有9
在position 3
和inner list 1
具有6
在position 3
,所以我要追加inner list 1
,而不是inner list 9
至listTwo
。 Since inner list 2
is the only list with a 2
in position 0
and a 12
in position 1
, it need not be compared to anything else, and can be appended to listTwo. 由于
inner list 2
是唯一的列表,其position 0
为2
, position 0
position 1
为12
,因此无需与其他任何内容进行比较,可以将其附加到listTwo。
I'm thinking something like: 我在想类似的东西:
for items in listOne :
#for all items where items[0] and items[2] are equal :
#tempList = []
#tempList.append(items)
#tempList.sort(key = lambda x: (x[3]))
#for value in tempList[0] :
#listTwo.append(all lists with value in tempList[0])
but I'm not sure how to implement this without a lot of really bad looking code, any suggestions for a "pythonic" way of sorting these lists? 但是我不确定如何在没有很多看起来很糟糕的代码的情况下实现这一点,是否有建议使用“ pythonic”方式对这些列表进行排序?
Perhaps throwing everything into a dictionary? 也许把所有东西都丢进字典了吗? Something like this:
像这样:
def strangeFilter(listOne):
listTwo = []
d = {}
for innerList in listOne:
positions = (innerList[0],innerList[2])
if positions not in d:
d[positions] = []
d[positions].append(innerList)
for positions in d:
listTwo.append(max(d[positions], key= lambda x: x[3]))
return listTwo
Not sure how much of a 'pythonic' solution this is, but it uses python-defined structures alright and has a nice time order of O(n) 不知道这是多少“ pythonic”解决方案,但是它使用python定义的结构好,并且具有很好的时间顺序O(n)
If you're looking to write concise python you will want to use list comprehensions wherever possible. 如果您要编写简洁的python,则将尽可能使用列表推导。 Your description was a little confusing, but something like
您的描述有点混乱,但类似
list_two = [inner_list for inner_list in list_one if inner_list[0] == inner_list[2]]
will get you all of the inner lists in which the 0
and 2
indices match. 将为您提供
0
和2
索引匹配的所有内部列表。 Then you can search all these to find the one with the largest 3
index, assuming there aren't any ties 然后,假设没有任何联系,您可以搜索所有这些以找到具有最大
3
索引的索引
list_three = [0,0,0,0]
for i in list_two:
if i[3] > list_three[3]:
list_three = i
Sort the list on items zero and two of the inner-lists . 对项目零和两个内部列表中的列表进行排序。 Using
itertools.groupby
extract the item in each group that has a maximum value at position 3. 使用
itertools.groupby
提取每个组中在位置 3处具有最大值的项目。
import operator, itertools
# a couple of useful callables for the key functions
zero_two = operator.itemgetter(0,2)
three = operator.itemgetter(3)
a = [[2,1,12,22],[1,1,9,9],[2,1,12,10],
[1,4,9,6],[8,8,8,1],[2,1,12,12],
[1,3,9,8],[2,1,12,15],[8,8,8,0]
]
a.sort(key = zero_two)
for key, group in itertools.groupby(a, zero_two):
print(key, max(group, key = three))
'''
>>>
(1, 9) [1, 1, 9, 9]
(2, 12) [2, 1, 12, 22]
(8, 8) [8, 8, 8, 1]
>>>
'''
result = [max(group, key = three) for key, group in itertools.groupby(a, zero_two)]
You could also sort on items zero, two, three. 您还可以对零,二,三项进行排序。 Then group by items zero and two and extract the last item of the group.
然后按零和二项分组,然后提取该组的最后一项。
zero_two_three = operator.itemgetter(0,2,3)
zero_two = operator.itemgetter(0,2)
last_item = operator.itemgetter(-1)
a.sort(key = zero_two_three)
for key, group in itertools.groupby(a, zero_two):
print(key, last_item(list(group)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.