Is python Collection Variable Summary

  • 2021-06-28 13:34:33
  • OfStack

A set is an unordered, variable sequence.The elements in a collection must be hash-immutable data types.

Empty Set

a=set()

Notice that a={} created an empty dictionary.

set - Variable Set.Elements in a collection can be dynamically added or deleted.

frozenset -- an immutable set.Elements in a collection cannot be changed.

Note: For union, intersection, difference, and so on, the return value has the same type as the leftmost operand.For example: s & t takes an intersection.The s collection is an set type collection, and the t collection is an frozenset type collection. The result returned will be an set type collection.

You can also use set() to convert to a set


b=[1,2,3,4]

a=set(b)

a

{1,2,3,4}

You can also create a collection with {}


a={1,2,3,4,1}

a

{1,2,3,4}

As with dictionary 1, since the collection is out of order, only one of the elements is retained when there are duplicates.

An immutable set is an unordered immutable set

Created with frozenset (seq)


a=frozenset([1,2,3,(1,2,4)])

a

frozenset({1,2,3,(1,2,4)})

Elements can only be hash


frozenset([1,2,3,[1,2,4]])

error

It is mainly used as a dictionary key.Unlike tuple, elements are not repeatable, and elements can only be of immutable type.

Description: Other combinatorial data types can be converted to an immutable set type (or a variable set type set can be converted to an immutable set type frozenset), returning an immutable set with no duplicate elements and an arbitrary ordering.

frozenset() function

Syntax: frozenset () - > empty frozenset object returns an invariant empty set

frozenset(iterable) - > frozenset object returns an immutable new set

iterable -- The combined data type to be converted.


Related articles: