如何在随机列表中查找连续元素的列表,其中结果列表的数量不应大于给定数量

时间:2018-08-03 16:36:52

标签: python arrays python-3.x list

我们考虑给定一个列表[4, 5, 2, 19, 3, 8, 9],给定的数字为8 那么输出应为[4, 5, 2],因为在输出列表中没有大于8的元素,并且它是唯一具有最大连续数字的列表。请告诉我python 3中的代码 *注意:请在不使用导入模块的情况下告诉逻辑。 这是我的代码

test_cases = int(input())
for test_case in range(test_cases):
    n_and_c = list(map(int, input().split()))
    no_of_plots = n_and_c[0]
    max_cost_of_each_plot = n_and_c[1]
    list_of_cost_of_each_plot = list(map(int, input().split()))

    def list_of_required_plots(l_plots, m_plot):
        r = []
        for i in l_plots:
            for j in range(len(l_plots)-1):
                if i < m_plot:
                    r.append(i)        
        return r


    def list_of_eligible_plots(l_plots, r_plots):
        e = []
        e.append(r_plots[0])
        for i in range(len(r_plots) - 1):
            idx = l_plots.index(r_plots[i])
            lp = 0
            for j in range(len(r_plots)-1):
                if l_plots[idx+lp] == r_plots[i+lp] :
                        e.append(r_plots[i+lp])
                        lp+=1
                else:
                    break
            return e



    def max_profit_func(r_plots, m_plot):
        m = 0
        for i in r_plots:
            m+=(m_plot - i)
        return m


    required_plots = list_of_required_plots(list_of_cost_of_each_plot, max_cost_of_each_plot)

    eligible_plots = list_of_eligible_plots(list_of_cost_of_each_plot, required_plots)

    print(eligible_plots)
    if len(required_plots) == 0:
        print(0)
    else:
        max_profit = max_profit_func(eligible_plots, max_cost_of_each_plot)

        print(max_profit)

我正在尝试在我的代码中获得list_of_eligible_plots()的解释输出。请任何人帮助或提出任何逻辑。

预先感谢

4 个答案:

答案 0 :(得分:0)

对于分组元素,您可以使用groupbydocs here)中的itertools。此代码段将查找包含最大连续元素的子列表,每个元素<=数字(在这种情况下为8):

from itertools import groupby

l = [4, 5, 2, 19, 3, 8, 9]
number = 8

print(max([list(g) for v, g in groupby(l, key=lambda v: v <= number) if v], key=len))

这将打印:

[4, 5, 2]

编辑(解释):

1.step 是查找元素为<=所选数字的组:

for v, g in groupby(l, key=lambda v: v <= number):
    print(v, list(g))

打印:

True [4, 5, 2]
False [19]
True [3, 8]
False [9]

2.step 已过滤出False个组:

print([list(g) for v, g in groupby(l, key=lambda v: v <= number) if v])

打印:

[[4, 5, 2], [3, 8]]

3.step 是查找具有最大元素数的子列表(带有max()参数的key函数,作为键,我们使用len()函数):

print(max([list(g) for v, g in groupby(l, key=lambda v: v <= number) if v], key=len))

打印:

[4, 5, 2]

答案 1 :(得分:0)

takewhile的时间:

from itertools import takewhile

lst = [4, 5, 2, 19, 3, 8, 9] 
print(list(takewhile(lambda x: x < 8, lst)))

# [4, 5, 2]

如何?

制作一个迭代器,只要谓词为true,就从迭代器返回元素。

答案 2 :(得分:0)

您可以使用itertools.takewhile查找少于8个的所有子列表,然后使用max查找最大的列表

>>> from itertools import takewhile, chain
>>> lst = [4, 5, 2, 19, 3, 8, 9] 
>>> n = 8
>>> itr = iter(lst)
>>> max((list(chain([f], takewhile(lambda x: x<n, itr))) for f in itr), key=len)
[4, 5, 2]

如果您不想导入itertoolschain方法takewhile,则可以自己定义

def takewhile(predicate, iterable):
    # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
    for x in iterable:
        if predicate(x):
            yield x
        else:
            break

def chain(*iterables):
    # chain('ABC', 'DEF') --> A B C D E F
    for it in iterables:
        for element in it:
            yield element

答案 3 :(得分:0)

只需保留一个“最大列表”并随时更新即可。

maxList = []
currList = []
lst = [4,5,2,19,3,8,9]
n = 8
for x in lst:
   if x < n:
       currList.append(x)
       if len(currList) > len(maxList):
           maxList = currList
   else:
       currList = []

我很确定这会起作用