【每日一题1116】格式转换

:mage:t2: 给定一个字符串:

"🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K"

请编写一个函数,将它输出成指定的格式:

1 🍰小天才团团~ 26.7K
2 🍰天才小团团 26.6K
3 惠子不在 24.7K

题目难度:简单
题目来源:叶开

def solution(s: str) -> str:
    # your code

before = "🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K"

after = """
1 🍰小天才团团~ 26.7K
2 🍰天才小团团 26.6K
3 惠子不在 24.7K
"""

assert solution(before) == after
def solution(s: str) -> str:
    after_list = []
    for w in [i for i in s.split('K') if len(i) > 0]:
        salary = float(w.split(';')[-1])
        w = w.replace('魅力值', '')
        w = w.replace('&#10', ' ')
        if w.startswith('  ;'):
            w = w[3:]
        if ';' + str(salary) in w:
            w = w.replace(';' + str(salary), str(salary))
        after_list.append((salary, w))
    after_list = sorted(after_list, key=lambda x: x[0])[::-1]
    return '\n' + '\n'.join([str(after_list.index(s) + 1) + ' ' + s[1] + 'K' for s in after_list]) + '\n'


before = "🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K"

after = """
1 🍰小天才团团~ 26.7K
2 🍰天才小团团 26.6K
3 惠子不在 24.7K
"""


assert solution(before) == after

好无聊的题!

from operator import itemgetter


def solution(s: str) -> str:
    result, str_res = [], '\n'
    result.append(';'.join(itemgetter(0, -1)(s.split(';', s.count(';') - 1))).replace('
', ' '))
    result.append(s.split(' ')[0].replace('
', ' '))
    result.append(' '.join(s.split(' ')[1].split('
')[1:]))
    return '\n' + ''.join([f"{index + 1} {line}\n" for index, line in enumerate(result)])


before = "🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K"

after = """
1 🍰小天才团团~ 26.7K
2 🍰天才小团团 26.6K
3 惠子不在 24.7K
"""

assert solution(before) == after

搞了半天才发现是头尾没加换行

def solution(s: str) -> str:
    list1 = s.split('魅力值
')
    list1 = [i.replace('
', ' ').strip() for i in list1 if '
' in i]
    return '\n' + '1 ' + list1[2] + '\n' + '2 ' + list1[0] + '\n' + '3 ' + list1[1] + '\n'


before = "🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K"

after = """
1 🍰小天才团团~ 26.7K
2 🍰天才小团团 26.6K
3 惠子不在 24.7K
"""

assert solution(before) == after
def solution(s: str) -> str:
    data_list = s.replace("魅力值
", '').replace("
", ' ').split(
        "K")  # ['🍰天才小团团 26.6', ' 惠子不在 24.7', ' 🍰小天才团团~ 26.7', '']
    data_list_2 = ''.join(data_list).split(' ')  # ['🍰天才小团团', '26.6', '惠子不在', '24.7', '🍰小天才团团~', '26.7']
    data_dict = {}  # {'🍰天才小团团': '26.6', '惠子不在': '24.7', '🍰小天才团团~': '26.7'}
    for i in range(0, len(data_list_2), 2):
        data_dict[data_list_2[i]] = data_list_2[i + 1]
    data = sorted(data_dict.items(), key=lambda v: v[1],
                  reverse=True)  # [('🍰小天才团团~', '26.7'), ('🍰天才小团团', '26.6'), ('惠子不在', '24.7')]
    after = []
    for i in range(len(data)):
        after.append(str(i + 1) + ' ' + data[i][0] + ' ' + data[i][1] + 'K')
    return '\n' + '\n'.join(after) + '\n'


before = "🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K"
print(solution(before))
after = """
1 🍰小天才团团~ 26.7K
2 🍰天才小团团 26.6K
3 惠子不在 24.7K
"""

assert solution(before) == after

Shell
echo "🍰天才小团团
26.6K 魅力值
惠子不在
24.7K 魅力值
🍰小天才团团~
26.7K" | grep -o "[^ ]*" | sed "s/
/ /g" | sed "s/魅力值 //g" | sort -k 2 -r