Python-based Join function

Source: Internet
Author: User

Python Join () function
When writing Python 100 today, one of the topics is roughly the following: The known input form is 1+3+2+1 and requires the output form to be 1+1+2+3

The first idea is to divide the input string into arrays with the split () function, sort the array, and then use the For loop to output

The code is attached:

A = Map (Int,raw_input (). Split (' + '))
A = sorted (a)
For I in A:
if (i! = Len (a)):
Print I, ' + '
Else
Print I



Although it is possible to output the correct results, the code is too cumbersome. As a result of the previous study of C language, but also done the relevant algorithm exercises, so the idea is fixed.

Look at the code of the Great god and worship it.

The code is attached:

Print "+". Join (Sorted (Raw_input () [:: 2])



The code is concise and Python functions are familiar. So go check the join related knowledge, the following is a more comprehensive explanation and examples.



Function: String.Join ()

Python has join () and Os.path.join () two functions, which work as follows:
Join (): An array of connection strings. Generates a new string from a string, tuple, or element in the list, with the specified character (delimiter) connection
Os.path.join (): Combine multiple paths to return



First, function description
1. Join () function

Syntax: ' Sep '. Join (SEQ)

Parameter description
Sep: Delimiter. Can be empty
SEQ: sequence of elements to concatenate, strings, tuples, dictionaries
The syntax above is to combine all the SEQ elements into a new string with Sep as a delimiter

Return value: Returns a string that is generated after each element is concatenated with the delimiter Sep



2. Os.path.join () function

Syntax: Os.path.join (path1[,path2[,......])

Return value: Combine multiple paths to return

Note: Parameters before the first absolute path are ignored



Second, examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#对序列进行操作 (use ' and ': ' as separators, respectively)

>>> seq1 = [' Hello ', ' good ', ' boy ', ' Doiido ']
>>> print '. Join (SEQ1)
Hello Good boy Doiido
>>> print ': ' Join (SEQ1)
Hello:good:boy:doiido


#对字符串进行操作

>>> seq2 = "Hello Good boy Doiido"
>>> print ': ' Join (SEQ2)
H:e:l:l:o:: g:o:o:d:: b:o:y::d: O:i:i:d:o


#对元组进行操作

>>> seq3 = (' Hello ', ' good ', ' boy ', ' Doiido ')
>>> print ': ' Join (SEQ3)
Hello:good:boy:doiido


#对字典进行操作

>>> seq4 = {' Hello ': 1, ' good ': 2, ' Boy ': 3, ' Doiido ': 4}
>>> print ': ' Join (SEQ4)
Boy:good:doiido:hello


#合并目录

>>> Import OS
>>> os.path.join ('/hello/', ' good/boy/', ' Doiido ')
'/hello/good/boy/doiido '

Python-based Join function

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.