在Python中打印给定列表中的正数集合[已关闭]

b1payxdu  于 2023-01-08  发布在  Python
关注(0)|答案(4)|浏览(130)
    • 已关闭**。此问题需要超过focused。当前不接受答案。
    • 想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

昨天关门了。
Improve this question
我有个任务需要你的帮助如何打印出给定列表中所有只包含正数的集合(按顺序)并选择其中元素最多的集合?
例如:x1月1x
输出:
[3,4,7], [2,3,4,9], [4] #只包含正数的所有集合
[2,3,4,9] #集合中元素最多,如果两个以上集合大小相同,则打印所有集合
我知道如何打印出其中包含最多元素的集合,但当涉及到多个元素时,代码只打印出最后一个集合。

ghhaqwfi

ghhaqwfi1#

您需要构造一个集合列表,并在此过程中跟踪最长集合的长度。

_list = [-1, 3, 4, 7, -12, 2, 3, 4, 9, -11, -12, -13, 4]

list_of_sets = [set()]
hi = 0

for n in _list:
    if n > 0:
        list_of_sets[-1].add(n) # add to set at end of list
        hi = max(hi, len(list_of_sets[-1])) # update length of longest set
    else:
        if len(list_of_sets[-1]) > 0: # append empty set to list if current set is not empty
            list_of_sets.append(set())
# depending on the order of values in the input list the last set in the list could be empty
# specifically, this will happen if the last value in list is less than 1
if len(list_of_sets[-1]) == 0:
    list_of_sets.pop()
print(list_of_sets)
# print any/all sets with appropriate length
print(*(s for s in list_of_sets if len(s) == hi), sep='\n')
    • 输出:**
[{3, 4, 7}, {9, 2, 3, 4}, {4}]
{9, 2, 3, 4}
rwqw0loc

rwqw0loc2#

arr = [-1,3,4,7,-12,2,3,4,9,-11,-12,-13,4,-5,1,2,3,4]

import itertools
sections = []
max_len = 0

for k,g in itertools.groupby(arr,key=lambda x: x>0):
    # k is True when a section is only consisting of positive numbers
    if k:
        section = list(g)
        sections.append(section)
        max_len = max(max_len,len(section))
        
# filter sections to the longest ones
longest = [section for section in sections if len(section) == max_len]
print(longest)

输出:

[[2, 3, 4, 9], [1, 2, 3, 4]]

我使用itertools.groupby来获得正的部分,然后它只是一个简单的过滤,过滤到最长的部分,我在分组迭代中设置了值。我还在输入数据的末尾添加了另一个部分,这样你就可以看到当有多个相同长度的部分时输出的样子,它是一个列表的列表。

6g8kf2rb

6g8kf2rb3#

正如评论中提到的。你想在列表

    • 代码:-**
A=[-1, 2, -3, 2, 3, 4, 5, 6, 7,-4, 3, 4, 5, -6, -19, -20, 7, 8, 9, 10, 6, 8, -19, -22, -99]

all_sublist=[]
tmp=[]
counter = 0  
for num in A:
    if num>0:
        counter+=1
        tmp.append(num)
    else:
        if counter>0:
            all_sublist.append(tmp)
        counter=0
        tmp=[]
if counter>0:  #Edgecase checking for last sublist if counter is greater than append last sublist.
    res.append(tmp)
#print(all_sublist) # all sublist with positive integers.

tmp2=sorted(all_sublist,key=len,reverse=True) #Sorted all_sublist on the basis of maximum length of list.. 
result=[]
max_len=len(tmp2[0]) 
for i in tmp2: #for loop runs till the first list which is not equal to max_len
    if len(i)==max_len:
        result.append(i)
    else:
        break
print(result)
    • 输出:-**
[[2, 3, 4, 5, 6, 7], [7, 8, 9, 10, 6, 8]]

1.通过使用迭代工具.分组比()

    • 代码:**
import itertools 
A=[-1, 2, -3, 2, 3, 4, 5, 6,1,-4, 3, 4, 5, -6, -19, -20, 7, 8, 9, 10, 6, 8, -19, -22, -99]
res=[]
for key, group in itertools.groupby(A,lambda x: x>0):
    if key:
        lis=list(group)
        if res==[]:
            res.append(lis)
        elif len(res[0])<len(lis):
            res.clear()
            res.append(lis)
        elif len(res[0])==len(lis):
            res.append(lis)
            
print(res)  #Same output as above.
dojqjjoe

dojqjjoe4#

感谢@Andrey_Kesely、@Fred@Yash_Mehta和@luk2302,特别是卢克,我已经尝试使用itertools. groupby来解决这个问题:
下面是我修改过的@Andrey_Kesely代码:

#Input:
A=[-1,3,4,7,-12,2,3,4,9,-11,-12,-13,4]
max_1=[]
for key, group in groupby(A, lambda x: x > 0):
    if key:
      print (list(group))
for key, group in groupby(A,lambda x: x>0):
      max_1= max(max_1, list(group), key=len)

print(max_1)
#Output: 

[3,4,7]
[2,3,4,9]
[4]

[2,3,4,9]

相关问题