【问题标题】:identify groups of ending contunious numbers from dataframe/list从数据框/列表中识别结束连续数字组
【发布时间】:2020-11-12 18:30:27
【问题描述】:

数据如下所示:

data=["q11-23-45","q11-23-46","q11-23-47","b11-73-50","q12-93-55","p11-23-59","p11-23-60","p11-23-61"]

试图得到类似的东西:

q11-23-45 to 47

b11-73-50

q12-93-55

p11-23-59 to 61

试过

a=[]
b=[]
for i in range(0,len(data)):
    try:
        if int(data[i][-2:])+1!= int(data[i+1][-2:]):
            a.append(data[i])
        else:
            b.append(data[i])
    except:
        print(" out of index ")

试图找到解决方案,但给出的解决方案如Identify groups of continuous numbers in a list

它适用于列表中的整数,而不是字符串 + 整数

提前谢谢你:)

【问题讨论】:

    标签: python list range continuous


    【解决方案1】:

    你可以试试这样的:

    def convert(data, split_str):
        output = []
        for d_index, d in enumerate(data):
            d = str(d)
            if d_index == 0:
                same_start = [d]
                continue
            start = d.split(split_str)[0]
            last_start = data[d_index-1].split(split_str)[0]
            if start == last_start:
                same_start.append(d)
            else:
                same_start = [d]
            if same_start not in output:
                output.append(same_start)
    
        for single_output in output:
            if len(single_output) == 1:
                print(single_output[0])
            else:
                print(single_output[0] + " to " + str(single_output[-1]).split(split_str)[-1])
    
    
    data= ["015 443 02 58",'015 443 02 59']
    convert(data, " ")
    
    print("=======")
    data=["q11-23-45","q11-23-46","q11-23-47","b11-73-50","q12-93-55","p11-23-59","p11-23-60","p11-23-61"]
    convert(data, "-")
    

    输出

    015 443 02 58 to 59
    =======    
    q11-23-45 to 47
    b11-73-50
    q12-93-55
    p11-23-59 to 61
    

    【讨论】:

    • 小疑问@Harsh Biyani 如果列表中有数字,它不会工作对吗?例如 ['015 443 02 58",'015 443 02 59']
    • @aminsama 然后你可以把它转换成字符串
    • @aminsama:我修改了答案
    • 非常感谢!但是我们可以比较最后两个字符而不是找到 - 或“”
    • @aminsama:是的,我们可以。您可以删除所有字母数字字符,并可以找出split_str
    【解决方案2】:

    您可以使用带有 max 和 min 函数的 defaultdict 来实现所需的输出

    from collections import defaultdict
    
    data=["q11-23-45","q11-23-46","q11-23-47","b11-73-50","q12-93-55","p11-23-59","p11-23-60","p11-23-61"]
    
    result = defaultdict(list)
    for i in data:
        result[i[:i.rfind("-")]].append(i[i.rfind("-")+1:])
    
    print(*[f"{i}-{min(result[i])} to {max(result[i])}" if max(result[i]) != min(result[i]) else f"{i}-{min(result[i])}" for i in result.keys()], sep="\n")
    

    输出

    q11-23-45 to 47
    b11-73-50
    q12-93-55
    p11-23-59 to 61
    

    此代码将聚合所有具有相同文本的项目,直到最后一个 - 后面的数字将被附加到字典项目的列表中。 然后 print 语句将打印所有具有最小值和最大值的字典键,如果它们不相同,则它将只打印一部分。

    【讨论】:

    • 非常感谢!但是我们可以比较最后两个字符而不是找到一个“-”
    猜你喜欢
    • 2016-08-26
    • 2011-01-10
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多