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 solve the problem of ordered characters by python

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the relevant knowledge of "how to solve the problem of ordered characters in python". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to solve the problem of ordered characters in python" can help you solve the problem.

Number of ordered characters

1.1 vegetable chicken solution

From collections import OrderedDict

Def ordered_count (input): counts = OrderedDict () for char in input: counts [char] = counts.get (char, 0) + 1

Return list (counts.items ())

1.2 Master solution 1

From collections import OrderedDict, Counter

Class OrderedCounter (Counter, OrderedDict): pass def ordered_count (seq): m=OrderedCounter (seq) return list (OrderedCounter (seq). Items ())

1.3 Master solution 2

From collections import Counter

Def ordered_count (input): return list (Counter (input). Items ()

1.4 comments

This question is to consider the order, that is, according to the order of the characters in the string, while counting the number of characters that appear. Master solution 1 is very clever, which inherits the two classes and inherits the two subclasses of OrderedDict and Counter in the collection module. Very second!

Abbreviated double-word name answer

2.1 Master solution 1

Def abbrevName (name): return'. Join (w [0] for w in name.split ()) .upper ()

2.2 Master solution 2

AbbrevName = lambda name: "." .join (e [0] .upper () for e in name.split ())

2.3 comments

Several master solutions are to use list derivation list for fast processing, and then use the string with its own upper () function for full capitalization. It is recommended to be familiar with commonly used built-in functions, such as:

'.lower () '.upper ()' .count () '.startswith ()' .capitalize () that's all for "how python solves the problem of ordered characters". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Internet Technology

Wechat

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

12
Report