Python's zip function and sorted function

Source: Internet
Author: User

# Zip () function and sorted () function # Zip () function: Merge two sequences, return a Zip object, cast to list or dictionary # sorted () function: Sort the sequence, return a sorted new list, the original data does not change # merge two lists, Output in list type LIST_STR = [' A ', ' B ', ' C ', ' d ']list_num = [1, 2, 3, 4]list_new = Zip (list_str, list_num) print ("Zip result (list):", the list (Li St_new) # Merges two strings into the dictionary type output str = ' ABCD ' str2 = ' 1234 ' list_new = Zip (str, str2) print ("Zip result (dictionary):", Dict (list_new)) # using Zip () and sorted () sort the dictionary Dict_data = {' A ': ' 4 ', ' B ': ' 1 ', ' C ': ' 3 ', ' d ': ' 2 '}print ("Direct Fetch dictionary min:", Min (Dict_data.items ())) print (" Sort the dictionary directly: ", Sorted (Dict_data.items ())) List_temp = Zip (Dict_data.values (), Dict_data.keys ()) print (" zip-processed minimum value: ", min ( list_temp)) List_temp = Zip (Dict_data.values (), Dict_data.keys ()) List_temp = sorted (list_temp) print ("post-order after zip processing:", list_temp) Print ("min. Two after zip processing:", List_temp[0:2])

Operation Result:

Zip results (list): [(' A ', 1), (' B ', 2), (' C ', 3), (' d ', 4)]zip result (dictionary): {' A ': ' 1 ', ' B ': ' 2 ', ' C ': ' 3 ', ' d ': ' 4 '} directly take the dictionary minimum: (' A ', ' 4 ') Sort the dictionary directly: [(' A ', ' 4 '), (' B ', ' 1 '), (' C ', ' 3 '), (' d ', ' 2 ')]zip processed after the minimum value: (' 1 ', ' B ') after the sort of zip processing: [(' 1 ', ' B '), (' 2 ', ' d '), (' 3 ', ' C '), (' 4 ', ' a ') a minimum of two after]zip processing: [(' 1 ', ' B '), (' 2 ', ' d ')]

Python's zip function and sorted function

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.