简体   繁体   English

Python 根据列表元素第一个数字的相等性将列表分成子列表

[英]Python list into sublists based on the equality of first digit of the list elements

a = [58,23,12,24,11,63,54]

I want to make the above list as follows:我想将上面的列表制作如下:

[[58,54],[23,24],[12,11]]

ie, I want to make the list of sublists whose elements start with same digit and having count > 1.即,我想制作其元素以相同数字开头且计数 > 1 的子列表列表。

You can use dictionary你可以用字典

from collections import defaultdict
a = [58,23,12,24,11,63,54]

d = defaultdict(list)
for num in a:
    d[str(num)[0]].append(num)
    
res = [v for v in d.values() if len(v) > 1]
print(res)

Output: Output:

[[58, 54], [23, 24], [12, 11]]

This code groups integer of any digit on the basis of the first digit.此代码根据第一位数字对任何数字的 integer 进行分组。 I hope this is what you are looking for.我希望这就是你要找的。 Also this may not be the most efficient way of doing this.此外,这可能不是最有效的方法。

a=[58,23,12,24,11,63,54]
final=[[],[],[],[],[],[],[],[],[],[]]
for i in a:
    count=0
    a=i
    while (i>0):
        count+=1
        i=i//10
    final[int(a/(10**(count-1)))].append(a)
a=[]
for i in final:
    if len(i)>1:
        a.append(i)
print(a)

you can use itertools.groupby to group a list by any key function you want:您可以使用itertools.groupby按您想要的任何键 function 对列表进行分组:

note the use of Python 3.8's "walrus operator"注意使用Python 3.8 的“海象算子”

from itertools import groupby

a=[58,23,12,24,11,63,54]
get_first_digit = lambda n: str(n)[0]
sorted_by_digit = sorted(a, key=get_first_digit, reverse=True)  # sorting is a must for groupby
result = [y for _,g in groupby(sorted_by_digit, key=get_first_digit) if len(y:=list(g))>1]

print(result)

Output: Output:

[[58, 54], [23, 24], [12, 11]]

I have an answer to this, although it is very big.我有一个答案,虽然它很大。

a = [58,23,12,24,11,63,54]
s = [[a[0]]]

def firstdigit(n):
    while n>=10:
        n = n//10
    return n

def arrange(s, i):
    for j in range(len(s)):
        if firstdigit(s[j][0]) == firstdigit(i):
            s[j].append(i)
            break
    else:
        m=[]
        m.append(i)
        s.append(m)
    return s

def check(s):
    for i in s:
        if len(i) <= 1:
            s.remove(i)
    return s

for i in a[1:]:
    s = arrange(s, i)

#checks for count>1
s = check(s)
print(s)

This code contains only basics with no modules imported.此代码仅包含基础知识,没有导入任何模块。

暂无
暂无

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

相关问题 如何首先基于初始列表的单个元素将列表拆分为子列表,然后仅在 python 中将列表的连续部分拆分为子列表? - How to split a list into sublists based on single elements of the initial list first and then contiguous portions of the list simply in python? Python根据子列表中的第一个元素将列表拆分为子列表 - Python Split list into sublists based on the first element in the sublists Python:按相等的投影值将列表分组为子列表 - Python: group a list into sublists by a equality of projected value 如何根据python3中的元素序列在列表中创建子列表? - How to create sublists within a list based on a sequence of elements in python3? Python - 根据另一个列表将列表拆分为子列表 - Python - split list in to sublists based on another list 根据 Python 上的相等条件添加列表的第二个元素 - Add second elements of a list based on equality condition on Python 将列表元素与子列表python的元素进行比较 - Comparing elements of a list with those of sublists python 来自 python 列表的子列表,使得第一个子列表包含列表的前 2 个元素,然后是前 3 个元素,依此类推,Num 为第 3 个,第 4 个,依此类推 - Sublists from a python list such that first sublist contains first 2 elements of list, then first 3 elements and so on and Num as 3rd, 4th and so on Python:通过制表符将列表分为2个子列表 - Python: Split List into 2 Sublists by tabseperating elements 将列表分组为子列表,由python中的字母元素分隔 - grouping a list into sublists, breaked by alphabet elements in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM