Python Merge and reorder first

Source: Internet
Author: User

A few days ago, the American group pen question

Title: Probably asked to enter two sets of numbers, the two sets of values sorted and then output the results

Idea: Enter two sets of numbers, combine two sets of numbers, sort

1List1 = Raw_input ("input some numbers ex 2 7 1:")2List2 = Raw_input ("input some numbers ex 2 7 1:")3 4 #Convert a string into a list5 6list11 = List1.split (" ")7list22 = List2.split (" ")8 9 #merge list, append list22 to list11 trailerTen #You can also use slices to implement append One  A list11.extend (list22) -  - #sort the list11 the #about List Sort List.sort () The biggest difference from the sorted function is that sorted returns an object and does not change the original list - PrintSorted (list11)

The resulting returned result may have duplicate elements, so the Deduplication method

1.set Collection because there are no duplicate elements in the collection

1 >>> list = [1,2,3,1,2,3]23 >>> set (list)4 Set ([1, 2, 3])

#注意: Using set does not preserve the original order

2. Iterate through the list, append all elements to the new list if the element is already present in the new list and do not append

1 list = [1,2,1,3,2,]2 list_new = []34 for in list: 5     if  not inch list_new: 6         list_new.append (i)7print  list_new8   

Wait a minute

Simple things to do simply

Python Merge and reorder first

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.