Python string processing with flexibility as its biggest advantage

Source: Internet
Author: User
Tags python list

When we use python strings, many problems hinder our use. Next we will take a detailed look at how to better use the relevant python string, I hope to help you.

  • Introduction to similarity between Python list and C # Language
  • Syntax of Python Data Types in common collections
  • Knowledge about python open-source technology development
  • Analysis of specific problems in Python array practice
  • How to configure the Python socket Service

After reading the implementation of C ++, C #, Java, and other languages, I always feel that it is too troublesome to kill the chicken. If you are interested, you can write or read the original article and reply to them. I have written a lot in Python recently. When I read these questions, I was eager to try. I know that it is ideal to use dynamic languages such as Perl, Python, Ruby, and so on. Later, I made these two questions and the results were satisfactory. the simplicity of the Code remained at the forefront of all the answers.

First look at the first question Python answers:

 
 
  1. Dic = {}
  2. For s in "abcdefgabc ":
  3. Dic [s] = 1 if s not in dic else (dic [s] + 1)
  4. Print '\ n'. join (' % s, % s' % (k, v) for k, v in dic. items ())
  5. Output result:
  6. A, 2
  7. C, 2
  8. B, 2
  9. E, 1
  10. D, 1
  11. G, 1
  12. F, 1

The four lines of Python code are declared as dictionary, assigned values, string traversal, and efficient concatenation.

If you haven't seen its conciseness and strength, please refer to the solution of the second question:

 
 
  1. Def main (offset = 6 ):
  2. String = u'quiet night thinking Li Bai bed bright moonlight, suspected ground cream. Look up at the moon and look down at your hometown. 090131'
  3. A = [[''] * offset for row in xrange (offset)]
  4. For I in xrange (offset ):
  5. For j in xrange (offset ):
  6. A [I] [j] = string [j + I * offset]
  7. B = [[r [col] for r in a [:-1] for col in xrange (len (a [0])]
  8. Print '\ n'. join ([u'row'. join (unicode (c) for c in row) for row in B])

Output result:

 
 
  1. 0 small low lifting suspected dead bed Jing 9 small heads Heads yellow like before the night 0 think again hope to see the ground Ming think 1 think so Ming on in the latest month, the moon, the frost, the moon, and the light, the moon, the sun, the moon,. Success, fail. Expires, expires white

If this problem is implemented using C #, the code should be more than 20 lines. Below I will briefly introduce these lines of code:

 
 
  1. Row 3: Two-dimensional "array" in Python is implemented through nested list. Here, a two-dimensional array with six rows and six columns is initialized;
  2. In row 3, we rotate the "matrix" 90 degrees row and column replacement clockwise, and right-and-tail the line after replacement-here:-1 is used to replace the trick of a list );


In the last line, We splice the elements in each row in the array with two different characters. The join method and for... in... statements are quite common in python strings.
Through these two questions, we can see that Python is very flexible and convenient in processing strings. Although the quality of a language should not be measured solely by whether it is concise or not, for me, python is the best language I have ever used. What's more, isn't that the fun of our problem-solving?

The above is the introduction to python strings.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.