Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to sort strings

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how to use Python to achieve string sorting related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

Description

1. When the sort () method sorts strings, it uses "ASCII character order" instead of the actual dictionary order.

2. If you need to sort in normal dictionary order, set the keyword parameter key to str.lower when the sort () method is called.

Example

Spam = ['elephants',' dogs', 'cats',' badgers', 'ants'] spam.sort () print (spam) prints the result: the [' ants', 'badgers',' cats', 'dogs',' elephants'] sort () method sorts strings using "ASCII character order" instead of the actual dictionary order. This means that uppercase letters come before lowercase letters. So when sorting, lowercase a comes after uppercase Z. Spam = ['Alice',' ants', 'Bob',' badgers', 'Carol',' cats'] spam.sort () print (spam) prints the result: ['Alice',' Bob', 'Carol',' ants', 'badgers',' cats'] if you need to sort it in normal dictionary order, set the keyword parameter key to str.lower when the sort () method is called. Spam = ['A','z','A','Z'] spam.sort (key=str.lower) print (spam) print the result: ['A','A','z','Z'] is all the content of the article "how to use Python to sort strings". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report