Last active
March 6, 2023 18:11
-
-
Save ROBOMASTER-S1/ea2cff10106a61216c4e1520589a76f8 to your computer and use it in GitHub Desktop.
Convert Sets Python Program Example: Created by Joseph C. Richardson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create four packed sets, then convert them into packed and sorted, extended lists. | |
# Check to see if their values are all sorted and reversed. After that, reconvert the | |
# 'convert1' list back into a very long extended unsorted, unpacked set. Type and | |
# execute/run the program example below and see what happens. | |
# Created by Joseph C. Richardson, GitHub.com | |
set1,set2,set3,set4=( | |
{1,2,3,4,5,6,7,8,9,10,4,2,10}, | |
{'1','1','2','3','4','4','5','6','7','8','9','10','20'}, | |
{'A','B','C','D','E','E','F','G','H','H','I','J'}, | |
{'Strawberries','Bananas','Apples','Oranges'}) | |
convert1,convert2,convert3,convert4=( | |
list(set1),list(set2),list(set3),list(set4)) | |
# The 'sorted()' function sorts only the returned list values, not the actual list values. | |
convert1,convert2,convert3,convert4=( | |
sorted(convert1),sorted(convert2), | |
sorted(convert3),sorted(convert4)) | |
convert1.extend(convert2) | |
convert1.extend(convert3) | |
convert1.extend(convert4) | |
# Invoke the 'reverse()' function to cause the 'convert1' list to reverse its sorted extended | |
# list values. | |
convert1.reverse() | |
# Reconvert the 'convert1' list back into an unsorted, randomized extended set, via invoking | |
# the 'set()' function. | |
reconvert=set(convert1) | |
print( | |
'\nCheck the converted list values:\n\n'+str(convert1)+'\ | |
\n\nAll looks great!\n\nYou have',len(convert1),\ | |
'sorted list values in your convert1 extended list.' | |
) | |
print( | |
'\nCheck the reconverted set values:\n\n'+str(reconvert)+'\ | |
\n\nAll looks great!\n\nYou have',len(reconvert),\ | |
'unsorted, randomized set values in your reconvert set.' | |
) | |
# Create four packed sets, then convert them into packed and sorted, extended lists. | |
# Check to see if their values are all sorted and reversed. After that, reconvert the | |
# 'convert1' list back into a very long extended unsorted, unpacked set. Type and | |
# execute/run the program example below and see what happens. | |
set1,set2,set3,set4=( | |
{1,2,3,4,5,6,7,8,9,10,4,2,10}, | |
{'1','1','2','3','4','4','5','6','7','8','9','10','20'}, | |
{'A','B','C','D','E','E','F','G','H','H','I','J'}, | |
{'Strawberries','Bananas','Apples','Oranges'}) | |
convert1,convert2,convert3,convert4=( | |
list(set1),list(set2),list(set3),list(set4)) | |
# The 'sort()' function sorts the actual list values. | |
convert1.sort(),convert2.sort() | |
convert3.sort(),convert4.sort() | |
convert1.extend(convert2) | |
convert1.extend(convert3) | |
convert1.extend(convert4) | |
# Invoke the 'reverse()' function to cause the 'convert1' list to reverse its sorted extended | |
# list values. | |
convert1.reverse() | |
# Reconvert the 'convert1' list back into an unsorted, randomized extended set, via invoking | |
# the 'set()' function. | |
reconvert=set(convert1) | |
print( | |
'\nCheck the converted list values:\n\n'+str(convert1)+'\ | |
\n\nAll looks great!\n\nYou have',len(convert1),\ | |
'sorted list values in your convert1 extended list.' | |
) | |
print( | |
'\nCheck the reconverted set values:\n\n'+str(reconvert)+'\ | |
\n\nAll looks great!\n\nYou have',len(reconvert),\ | |
'unsorted, randomized set values in your reconvert set.' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment