• 周四. 4月 25th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

对嵌入字符串中的数字进行排序

[db:作者]

3月 7, 2023

我有一个如下所示的数据文本文档:

(数据.txt)

John|5|02-20-2019
Sally|3|02-19-2019
Billy|12|02-12-2019
Smith|1|02-16-2019

我希望能够根据嵌入在“|”中的数字将这些值排序到名为(data|sorted.txt)的新文本文档中。所以基本上我希望输出文档是:

Billy|12|02-12-2019
John|5|02-20-2019
Sally|3|02-19-2019
Smith|1|02-16-2019

有什么想法吗?我已尝试使用split(’\n’)将此列成一个列表,但我不确定之后可以做什么。我知道如何分离这些数字,然后我可以单独比较这些数字,但我不知道如何保留它们的其他识别信息。
谢谢

Tags:

数据文档txt信息列表data排序数字文本文档johnsallysplitsmithsortedbilly1条回答网友

1楼 ·

发布于 2023-03-07 00:11:17

with open('data.txt', 'r') as infile:
    datalist = list(infile)

datalist = sorted(datalist, key=lambda x: int(x.split('|')[1]), reverse=True)

with open('sorted_data.txt', 'w') as outfile:
    for line in datalist:
        outfile.write(line)

《对嵌入字符串中的数字进行排序》有153个想法
  1. I do believe all of the ideas you have offered on your post.
    They are really convincing and will definitely work. Nonetheless, the posts are too brief for
    novices. Could you please lengthen them a little
    from subsequent time? Thank you for the post.

  2. Hello! This is kind of off topic but I need some help
    from an established blog. Is it difficult to set up your own blog?
    I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about creating my own but I’m not sure where
    to start. Do you have any points or suggestions?

    Thank you

  3. Saygrass Company invites homeowners, landscapers, and businesses to join the green revolution by choosing sustainable landscaping solutions. By opting for Saygrass artificial grass, customers elevate their outdoor living experience, contribute to water conservation efforts, and reduce their ecological footprint.

  4. I just could not depart your web site prior to suggesting that I really loved the usual info an individual supply in your visitors Is gonna be back regularly to check up on new posts

  5. of course like your website but you have to check the spelling on several of your posts A number of them are rife with spelling issues and I in finding it very troublesome to inform the reality on the other hand I will certainly come back again

  6. I am absolutely thrilled to introduce you to the incredible Sumatra Slim Belly Tonic! This powdered weight loss formula is like no other, featuring a powerful blend of eight natural ingredients that are scientifically linked to fat burning, weight management, and overall weight loss. Just imagine the possibilities! With Sumatra Slim Belly Tonic, you have the opportunity to finally achieve your weight loss goals and transform your body into the best version of yourself.

  7. Your positivity and optimism are contagious It’s impossible to read your blog without feeling uplifted and inspired Keep up the amazing work

  8. Thank you a lot for sharing this with all of
    us you actually realize what you are speaking about!

    Bookmarked. Please additionally consult with my website =).
    We could have a link alternate arrangement among us

  9. I love your blog.. very nice colors & theme.
    Did you make this website yourself or did you hire someone to
    do it for you? Plz respond as I’m looking to design my own blog
    and would like to know where u got this from. many thanks

  10. Hello Neat post Theres an issue together with your site in internet explorer would check this IE still is the marketplace chief and a large element of other folks will leave out your magnificent writing due to this problem.

  11. 저희는 구글 계정 판매 전문 회사입니다.우리의 구글 계정은 이메일, 문서, 캘리더, 클라우드 저장 등의 기능을 포함한 포괄적인 디지털 솔루션을 제공합니다.구글 계정을 통해 우리는 사용자에게 효율적인 협업 플랫품을 제공하여 개인과 팀이 일과 삶을 더 스마트하게 관리할 수 있도록 지원합니다.

  12. Hello, Neat post. There’s an issue along with your site in web explorer, might
    test this? IE nonetheless is the marketplace leader and a big
    element of people will pass over your great writing because of this
    problem.

  13. My brother suggested I might like this blog He was totally right This post actually made my day You can not imagine simply how much time I had spent for this info Thanks.

  14. Magnificent beat I would like to apprentice while you amend your site how can i subscribe for a blog web site The account helped me a acceptable deal I had been a little bit acquainted of this your broadcast offered bright clear idea.

  15. Just as much as I did, I relished what you accomplished here. Your language is elegant and the drawing is enticing, yet you appear rushed to get to the next thing you should be providing. If you keep this walk safe, I’ll come back more often.

  16. I’m often to blogging and i really appreciate your content. The article has actually peaks my interest. I’m going to bookmark your web site and maintain checking for brand spanking new information.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注