Python Collection Usage Instance analysis

Source: Internet
Author: User
This example describes the Python collection usage. Share to everyone for your reference. The specific analysis is as follows:

# Sets is unordered collections of unique hashable elements# Python23 tested vegaseat 09mar2005# Python v2.4 has sets Built Inimport setsprint "List the functions within module ' sets ':" For Funk in Dir (sets): Print funk# Create an empty SE Tset1 = set ([]) # now load the Setfor K in range: Set1.add (k) print "\nloaded a set with 0 to 9:" Print Set1set1.add (7) p Rint "tried to add another 7, but it is already there:" Print set1# make a list of fruits as you put them into a basketbas  ket = [' Apple ', ' orange ', ' apple ', ' pear ', ' orange ', ' banana ']print ' \nthe original list of fruits: ' Print basket# Create a Set from the list, removes the Duplicatesfruits = sets. Set (basket) print "\nthe set is unique, and the order has a changed:" Print fruits# let ' s get rid of some duplicate wordsstr1 = "Senator Strom Thurmond dressed as as Tarzan" print "\noriginal string:" Print Str1print "A List of the words in the Strin G: "WrdList1 = Str1.split () print wrdlist1# now create a set of unique Wordsstrset = sets. SeT (wrdList1) print "The set of the words in the string:" Print Strsetprint "Convert set back to string (Order have changed!):" Print "". Join (Strset) print# comparing-sets, bear with me ... colorSet1 = sets. Set ([' Red ', ' green ', ' blue ', ' black ', ' orange ', ' White ')] ColorSet2 = sets. Set ([' Black ', ' maroon ', ' grey ', ' Blue ']) print "ColorSet1 =", colorset1print "ColorSet2 =", colorset2# same as (colorset1-c OlorSet2) ColorSet3 = colorset1.difference (colorSet2) print "\nthese is the colors in ColorSet1 that is not in ColorSet2:" Print colorset3# same as (ColorSet1 | colorSet2) ColorSet4 = colorset1.union (colorSet2) print "\nthese is the colors appear ing in both sets: "Print colorset4# same as (colorSet1 ^ colorSet2) colorSet5 = colorset1.symmetric_difference (colorSet2) PR int "\nthese is the colors in ColorSet1 or in ColorSet2, and not both:" Print colorset5# same as (ColorSet1 & Colorset 2) ColorSet6 = Colorset1.intersection (colorSet2) print "\nthese is the colors common to ColorSet1 and ColorSet2:" Print COlorSet6 

Hopefully this article will help you with Python programming.

  • 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.