简体   繁体   English

无法将列表映射到python中的列表列表

[英]Having trouble mapping a list to a list of lists in python

Essentially, I need to create a new list of lists from a value list that maps to another interval list. 基本上,我需要从值列表创建一个新的列表列表,该列表映射到另一个间隔列表。 Some of these values actually fall in the range of two sublists in the intervals. 这些值中的一些实际上落在间隔中的两个子列表的范围内。 For instance, 25 falls between [0,30] and between [20,55]. 例如,25落在[0,30]和[20,55]之间。 However an integer value such as 91 only falls in the range of one sublist [75,100]. 然而,诸如91的整数值仅落在一个子列表[75,100]的范围内。 I want to create a new list where each value maps to the interval list but I want these values separate from the interval list. 我想创建一个新列表,其中每个值映射到间隔列表,但我希望这些值与间隔列表分开。

    intervals = [[0,30], [20,55], [45,80], [75,100]]
    values = [25, 51, 53, 83, 91]

Right now my code is as follows: 现在我的代码如下:

    maplist = [[]]
    for i in values:
        for j in intervals: 
            if(i >= j[0]) and (i <= j[1]):
                maplist.append(i)
    print(maplist)

This ouputs to: [[], 25, 25, 51, 51, 53, 53, 83, 91 ] 输出:[[],25,25,51,51,53,53,83,91]

So, as you can see, Its outputting 25 twice, 51 twice and 53 twice because those values fall between various sublists. 因此,正如您所看到的,它输出25次,51次两次和53次两次,因为这些值落在各个子列表之间。 83 and 91 only fall between [75-100] 83和91仅介于[75-100]之间

I want the output to be: 我希望输出为:

    [[25], [25,51,53], [51,53], [83,91]]

This means each value would map with the interval list of lists. 这意味着每个值都将映射到列表的间隔列表。 Any help would be greatly appreciated. 任何帮助将不胜感激。

List comprehensions? 列表理解?

>>> [[v for v in values if a <= v <= b] for a, b in intervals]
[[25], [25, 51, 53], [51, 53], [83, 91]]

So you could do it by looping through intervals first and getting all the values in that range. 因此,您可以通过首先循环显示间隔并获取该范围内的所有值来实现。 Like this: 像这样:

intervals = [[0,30], [20,55], [45,80], [75,100]]
values = [25, 51, 53, 83, 91]

maplist = []
for i in intervals:
    inrange = []
    for v in values:
        if v >= i[0] and v <= i[1]:
            inrange.append(v)
    maplist.append(inrange)

print(maplist)

This prints [[25], [25, 51, 53], [51, 53], [83, 91]] 打印[[25],[25,51,53],[51,53],[83,91]]

The problem is that you are appending to maplist , which is initialized to be a list containing one element (another list). 问题是你要附加到maplist ,它被初始化为包含一个元素(另一个列表)的列表。 When you process 25, you append it to maplist , first once, at which point maplist is [[], 25] and then again for the next interval ( [[], 25, 25] ). 当你处理25时,你将它附加到maplist ,第一次,此时maplist[[], 25] ,然后再次下一个间隔( [[], 25, 25] )。

Instead, you want one list for each interval: 相反,您需要每个间隔一个列表:

maplist = [[] for _ in range(len(intervals))]
for i in values:
    for idx, j in enumerate(intervals):
        if(i >= j[0]) and (i <= j[1]):
            maplist[idx].append(i)

(If you're not familiar with enumerate , it produces pairs (index, item) for the items in a list.) (如果您不熟悉enumerate ,它会为列表中的项生成对(index, item) 。)

Use a list comprehension. 使用列表理解。

Also you are initializing the maplist with another list.. 您还要使用其他列表初始化地图列表。

maplist = [[x for x in values if i <= x <= j] for i, j in intervals]

print(maplist)

You can use collections.defaultdict to create a better mapping of the occurrences of each each value item in intervals : 您可以使用collections.defaultdictintervals每个value项的出现创建更好的映射:

from collections import defaultdict
d = defaultdict(list)
intervals = [[0,30], [20,55], [45,80], [75,100]]
values = [25, 51, 53, 83, 91]
for i in values:
  for c, h in enumerate(intervals):
     if i >= h[0] and i <= h[-1]:
         d[c].append(i)

final_result = [b for a, b in d.items()]

Output: 输出:

[[25], [25, 51, 53], [51, 53], [83, 91]]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM