代码之家  ›  专栏  ›  技术社区  ›  shaik moeed

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

  •  -2
  • shaik moeed  · 技术社区  · 8 年前

    让我们考虑一下给定的列表 [4, 5, 2, 19, 3, 8, 9] 给定的数字是 8 那么输出应该是 [4, 5, 2] 因为在输出列表中没有大于 它是唯一一个连续数字最多的列表。请告诉我密码 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 回复  |  直到 8 年前
        1
  •  0
  •   JimNeedsCoffee    8 年前

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

    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 = []
    

    我很肯定那会有用的

        2
  •  0
  •   Austin    8 年前

    时间到了 takewhile :

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

    怎么用?

    使迭代器返回iterable中的元素,只要谓词为true。

        3
  •  0
  •   Andrej Kesely    8 年前

    对于分组元素,可以使用 groupby itertools ( docs here ). 此代码片段将找到具有最大连续元素的子列表,每个元素lt=数字(在这种情况下为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.步骤 要查找元素为选定数字的<=的组:

    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.步骤 过滤掉了 False 组:

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

    印刷品:

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

    3.步骤 找到具有最大数量元素的子列表( 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]
    
        4
  •  0
  •   Sunitha    8 年前

    你可以用 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]
    

    如果不想导入 itertools 方法 chain 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
    
    推荐文章