# 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