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