我有以下列表,
mainlist=[10,20,30,40,50]
以下子列表,其中第一个列表的每个数字包含6个数字
sublist=[10,12,14,16,18,20,20,22,24,26,28,30,30,32,34,36,38,40,40,42,44,46,48,50,50,52,54,56,58,60]
最后一个子列表1,其中第一个子列表中的每个数字包含5个数字。
[10,10.5,11,11.5,12,12,12.5,13,13.5,14,14,14.5,15,15.5,16,16,16.5,17,17.5,18,18,18.5,19,19.5,20,20,20.5,21,21.5,22,22,22.5,23,23.5,24,24,24.5,25,25.5,26,26.5,27,27.5,28,28,28.5,29,29.5,30,30,30.5,31,31.5,32,32,32.5,33,33.5,34,34.5,35,35.5,36,36,36.5,37,37.5,38,38.5,39,39.5,40,40,40.5,41,41.5,42,42,42.5,43,43.5,44,44,44.5,45,45.5,46,46,46.5,47,47.5,48,48,48.5,49,49.5,50,50,50.51,51.5,52,52.5,53,53.5,54,54.5,55,55.5,56,56,56.5,57,57.5,58,58.5,59,59.5,60]
如何将它们分组?即,当我在10
中选择值mainlist
时,我应该能够从10,12,14,16,18,20
中看到sublist
,而当我选择{{1 }}在10
中,我应该能够从sublist
中看到值10,10.5,11,11.5,12
,依此类推。应该使用哪种数据结构来实现此目的,以及如何从这些列表中创建
sublist1
答案 0 :(得分:0)
这应该提供类似您所追求的输出。目前,仅当您的值包含在主列表中时才会打印:
mainlist = [10,20,30,40,50]
sublist = [10,12,14,16,18,20,20,22,24,26,28,30,30,32,34,36,38,40,40,42,44,46,48,50,50,52,54,56,58,60]
sublist_1 = [10,10.5,11,11.5,12,12,12.5,13,13.5,14,14,14.5,15,15.5,16,16,16.5,17,17.5,18,18,18.5,19,19.5,20,20,20.5,21,21.5,22,22,22.5,23,23.5,24,24,24.5,25,25.5,26,26.5,27,27.5,28,28,28.5,29,29.5,30,30,30.5,31,31.5,32,32,32.5,33,33.5,34,34.5,35,35.5,36,36,36.5,37,37.5,38,38.5,39,39.5,40,40,40.5,41,41.5,42,42,42.5,43,43.5,44,44,44.5,45,45.5,46,46,46.5,47,47.5,48,48,48.5,49,49.5,50,50,50.51,51.5,52,52.5,53,53.5,54,54.5,55,55.5,56,56,56.5,57,57.5,58,58.5,59,59.5,60]
def print_lists(value):
if value in mainlist:
print mainlist[mainlist.index(value):]
print sublist[sublist.index(value):sublist.index(value)+6]
print sublist1[sublist1.index(value):sublist1.index(value)+5]
示例输出:
print_lists(50)
>>> [50]
>>> [50, 50, 52, 54, 56, 58]
>>> [50, 50, 50.51, 51.5, 52]
答案 1 :(得分:0)
由于您要求使用结构来存储这些关系而不是函数,因此我建议使用字典。使用字典理解-
sublist_lookup = {x:sublist[6*i:6*i+6] for i, x in enumerate(mainlist)}
sublist1_lookup = {x:sublist1[5*i:5*i+5] for i, x in enumerate(sublist)}
用法:
>>> sublist_query[10]
[10, 12, 14, 16, 18, 20]
>>> sublist_query[20]
[20, 22, 24, 26, 28, 30]
>>> sublist1_query[10]
[10, 10.5, 11, 11.5, 12]
>>> sublist1_query[12]
[12, 12.5, 13, 13.5, 14]
答案 2 :(得分:0)
因此,我尝试了一种递归方法来解决该问题。我相信这就是您想要的:
var str = "bla"
str.removeLast() // returns "a"; str is now "bl"
功能参数:
乘数:您希望在新间隔中除以多少个数字(如果间隔为10-20,乘数为6,您将得到[10,12,14,16,18,20])。
it:您希望执行几次。不可避免地,乘数将达到1,但我不知道您要使用的列表的大小,因此最好给您一种停止它的方法。就是这样。请记住,如果(it>乘数),脚本将一直运行直到乘数等于1。
您应该能够即插即用。只要提供所有想要的列表,只要所有项目彼此等距即可。