Python字符串数据排序

时间:2019-06-06 21:54:50

标签: python

我有我的AWS Python lambda函数返回包含以下数据的字符串:

mydata = ( 
#user                            #Key                           #age

eks-user            AKIAII4CCN6AA           288  days
eks-user            AKIAJFUU2ASII           100  days
Oracle_RMAN         AKIAJ4DMBBCEP           847  days
Oracle_RMAN         AKIAIAWETFUK            134  days
nb-dev-api          AKIAI7OTKONT            199  days
nb-dev-api          AKIAICIDXLDU            100  days
text@abc.com                    AKIAJ4DMBBCEP           120      days )

我想按年龄排序。

请让我知道在这里可行的建议?

提前谢谢!

我尝试使用无济于事的sort和sorted方法。

mydata.sort(key=AgeSort)
print(f'list of Compliance Users: {username}')

#Sort the user by age
def AgeSort(d):
    return d[str(age)]

我希望输出:

Oracle_RMAN         AKIAJ4DMBBCEP           847  days
eks-user            AKIAII4CCN6AA           288  days
nb-dev-api          AKIAI7OTKONT            199  days
Oracle_RMAN         AKIAIAWETFUK            134  days
text@abc.com        AKIAJ4DMBBCEP           120  days
eks-user            AKIAJFUU2ASII           100  days
nb-dev-api          AKIAICIDXLDU            100  days

2 个答案:

答案 0 :(得分:0)

def woe(cont):
    for i in range(0,2):
        table = raw_data[cont.split(" ")[i]]
        table.name = cont.split(" ")[i]
        print(table)
woe(cont)

输出:

from operator import itemgetter

aws_string = """eks-user            AKIAII4CCN6AA           288  days
eks-user            AKIAJFUU2ASII           100  days
Oracle_RMAN         AKIAJ4DMBBCEP           847  days
Oracle_RMAN         AKIAIAWETFUK            134  days
nb-dev-api          AKIAI7OTKONT            199  days
nb-dev-api          AKIAICIDXLDU            100  days
text@abc.com                    AKIAJ4DMBBCEP           120      days"""

def sort_string(string):
    string = string.split("\n")
    array_of_data = []
    for line in string:
        array_of_data.append(line.split())
    #array_of_data = sorted(array_of_data,key=lambda l:l[2], reverse=True)
    array_of_data = sorted(array_of_data, key=itemgetter(2), reverse=True)
    return array_of_data

ls = sort_string(aws_string)
for l in ls:
    print(l)

返回字符串

['Oracle_RMAN', 'AKIAJ4DMBBCEP', '847', 'days']
['eks-user', 'AKIAII4CCN6AA', '288', 'days']
['nb-dev-api', 'AKIAI7OTKONT', '199', 'days']
['Oracle_RMAN', 'AKIAIAWETFUK', '134', 'days']
['text@abc.com', 'AKIAJ4DMBBCEP', '120', 'days']
['eks-user', 'AKIAJFUU2ASII', '100', 'days']
['nb-dev-api', 'AKIAICIDXLDU', '100', 'days']

输出:

from operator import itemgetter

aws_string = """eks-user            AKIAII4CCN6AA           288  days
eks-user            AKIAJFUU2ASII           100  days
Oracle_RMAN         AKIAJ4DMBBCEP           847  days
Oracle_RMAN         AKIAIAWETFUK            134  days
nb-dev-api          AKIAI7OTKONT            199  days
nb-dev-api          AKIAICIDXLDU            100  days
text@abc.com                    AKIAJ4DMBBCEP           120      days"""

def sort_string(string):
    string = string.split("\n")
    array_of_data = []
    for line in string:
        array_of_data.append(line.split())
    #array_of_data = sorted(array_of_data,key=lambda l:l[2], reverse=True)
    array_of_data = sorted(array_of_data, key=itemgetter(2), reverse=True)

    for l in array_of_data:
        array_of_data[array_of_data.index(l)] = " ".join(l)

    array_of_data = "\n".join(array_of_data)
    return array_of_data

ls = sort_string(aws_string)
print(ls)

这应该可行

Oracle_RMAN AKIAJ4DMBBCEP 847 days
eks-user AKIAII4CCN6AA 288 days
nb-dev-api AKIAI7OTKONT 199 days
Oracle_RMAN AKIAIAWETFUK 134 days
text@abc.com AKIAJ4DMBBCEP 120 days
eks-user AKIAJFUU2ASII 100 days
nb-dev-api AKIAICIDXLDU 100 days

输出

from operator import itemgetter

aws_string = """eks-user            AKIAII4CCN6AA           288  days
eks-user            AKIAJFUU2ASII           100  days
Oracle_RMAN         AKIAJ4DMBBCEP           847  days
Oracle_RMAN         AKIAIAWETFUK            134  days
nb-dev-api          AKIAI7OTKONT            199  days
nb-dev-api          AKIAICIDXLDU            100  days
text@abc.com                    AKIAJ4DMBBCEP           120      days"""


def sort_string(string):
    string = string.split("\n")
    array_of_data = []
    for line in string:
        array_of_data.append(line.split())
    #array_of_data = sorted(array_of_data,key=lambda l:l[2], reverse=True)
    array_of_data = sorted(array_of_data, key=itemgetter(2), reverse=True)

    for l in array_of_data:
        array_of_data[array_of_data.index(l)] = " ".join(l)

    array_of_data = "\n".join(array_of_data)
    return array_of_data
complianceusers = sort_string(aws_string)
print(complianceusers)

答案 1 :(得分:0)

这是针对您问题的简单解决方案

arr = [l.split() for l in aws_string.split('\n')]
arr.sort(key=lambda l:int(l[2]), reverse=True)
s = '\n'.join('\t'.join(a) for a in arr)
print(s)

这将产生以下输出

Oracle_RMAN     AKIAJ4DMBBCEP       847     days
eks-user        AKIAII4CCN6AA       288     days
nb-dev-api      AKIAI7OTKONT        199     days
Oracle_RMAN     AKIAIAWETFUK        134     days
text@abc.com    AKIAJ4DMBBCEP       120     days
eks-user        AKIAJFUU2ASII       100     days
nb-dev-api      AKIAICIDXLDU        100     days