A close relative of a dictionary in Python--a collection

Source: Internet
Author: User

Two functions of a collection
1. The set can realize the function of de-weight;
2. The set can implement relationship testing: intersection, Difference set, and set, whether subset, whether there is no intersection

To elicit a collection with a single application scenario:
Now IPV4 is represented by a 32-bit unsigned integer, which is typically displayed in a point-and-click manner, dividing the IP address into 4 parts
, each part is 8 bits, expressed as an unsigned integer (so it does not need to appear with a plus sign), such as 10.137.17.1,
is a very familiar IP address, there is no space in an IP address string (because to be represented as a 32 number).
Now you need to use the program to determine whether IP is legitimate.
(1) Input Description: Enter an IP address
(2) Output Description: Returns the result of the judgment Yes or NO
Example 1
1. Input: 10.138.15.1
2. Output: YES

#!/usr/bin/env python
#coding: Utf-8

#172.25.254.1
ip = raw_input (' IP: ')
#["172". " 25 "." 254 "." 1 "]
ip = Ip.split ('. ')
#一次判断每一部分是否合法;
For I in IP:
If 0<=int (i) <=255:
print ' YES '
Break
Else
print ' NO '

Scene 1:li = [' A ', ' B ']
Scenario 2: Does the hkcss want to count all the students joining the club?

Li1 = [' A ', ' B ', ' C '] li2 = [' A ', ' C ']
all = []
For I in Li1:for J in Li2:
List to solve too much trouble;
Set (Li1 + li2) Set (Li1.extend (LI2))

I. Definition of SET Set
1. A set is a data structure that is not duplicated;
(1) The curly braces are empty and are dictionary types;
In [1]: s = {}

In [2]: type (s)
OUT[2]: Dict

(2) The first way to define a set;
In [3]: s = {1, 2, 3, 1, 2}

In [4]: type (s)
OUT[4]: Set

In [5]: print S
Set ([1, 2, 3])

2. Factory methods when defining a set, the parentheses are an iterative object, eg: numeric types are not allowed;
(1) The second way to define a collection: Define an empty collection
In [6]: s = set ()

In [7]: type (s)
OUT[7]: Set

(2) Define a collection

* String
In [8]: s = set (' Hello ')

In [9]: print S
Set ([' H ', ' e ', ' l ', ' o '])

* Dictionary
In [all]: s = set ({' A ': 1, ' B ': 2, ' C ': 3})

In []: print S
Set ([' A ', ' C ', ' B '])
* Meta-Group
in [+]: s = Set ((1, 2, 3, 4))

in [+]: print s
Set ([1, 2, 3, 4])
* List

in [+]: s = set ([1, 2, 3])

in [+]: print s
Set ([1, 2, 3])

Two. Application: Implement list to go heavy

1, convert to collection data type: Set (list)
2. The Fromkeys method of the dictionary is realized;
(1) Implement list to go to weight
In []: Li = [1, 2, 3, 4, 2, 3]

In [P]: s = Set (LI)

In []: Li = List (s)

in [+]: print s
Set ([1, 2, 3, 4])

in [+]: print Li
[1, 2, 3, 4]
(2) Fromkeys method implementation list to go to heavy
in [+]: Li = [1, 2, 3, 4, 2, 3]

In []: D = {}.fromkeys (LI)

in [+]: Print D.keys ()
[1, 2, 3, 4]

Three. Characteristics of the Set

1. Sets are unordered, non-repeating data types;
2. Indexes are not supported, tiles are not supported, duplicates are not supported, and connections are not supported;
3. Support Member operators;
4. Support for Loop;

(1) member operators:
in [+]: s = {1, 2, 3, 4, 1, 2}

in [+]: 1 in S
OUT[32]: True

In [Sat]: 1 not in S
OUT[33]: False

(2) The set supports A for loop, which is iterative:
in [+]: For I in S:
.....: Print I
....:
1
2
3
4

Four. Increase in the collection of three changes

1. Increase

in [+]: s = {1, 2, 3, 4, 1, 2}

In [approx]: S.add (8)

In [PNS]: S.add (1)

in [+]: print s
Set ([8, 1, 2, 3, 4])

In [all]: S1 = {' A ', ' B ', ' C '}

In [MAX]: s.update (S1)

in [+]: print s
Set ([' A ', 1, 2, 3, 4, 8, ' C ', ' B '])

2. Change

3. Check
Relationship Test Action

In [the]: S1 = {1, 2, 3, 4}

in []: S2 = {1, 2, 3, 5}
#交集
In [a]: S1 & S2
OUT[47]: {1, 2, 3}
#并集
In []: S1 | S2
OUT[48]: {1, 2, 3, 4, 5}
#差集
In [$]: s1-s2
OUT[49]: {4}

In []: S2-S1
OUT[50]: {5}

#对等差分
In [Wuyi]: S1 ^ s2
OUT[51]: {4, 5}

#交集
In []: s1.intersection (S2)
OUT[52]: {1, 2, 3}
#并集
in [+]: s1.union (S2)
OUT[53]: {1, 2, 3, 4, 5}

#差集
In [si]: s1.difference (S2)
OUT[54]: {4}

in [+]: s2.difference (S1)
OUT[55]: {5}

#对等差分
In []: s1.symmetric_difference (S2)
OUT[56]: {4, 5}

In []: S1 = {1, 2, 3, 4}

In [s2]: = {1, 2, 3}
#s2是否s1子集
In [the]: S2.issubset (S1)
OUT[69]: True
#s1是否是s2的父集
In []: S1.issuperset (S2)
OUT[71]: True

#是否没有交集
In []: S1.isdisjoint (S2)
OUT[72]: False

4. By deleting

In [sat]: s = {1, ' a ', ' hello ', 45,}
(1) S.pop () #随机删除一个, returns an object
In [the]: S.pop ()
OUT[74]: ' A '

(2) S.remove #删除指定元素, is the set member is deleted, not the member error
In []: S.remove (1)

In [Sat]: print s
Set ([*, ' hello '])

In [All]: S.remove (' B ')
File "<ipython-input-77-fe0f6997b18b>", line 1
S.remove (' B ')
^
Syntaxerror:invalid syntax

in [+]: print s
Set ([*, ' hello '])
(3) S.discard () #删除指定元素, is a member removed, not a member does nothing
In [the]: S.discard (45)

in [+]: S.discard (' B ')

In [Bayi]: print S
Set ([' Hello '])

(4) s.clear () #清空集合元素
In [more]: S.clear ()

In [the]: s
OUT[83]: Set ()

Application:
(Huawei Machine Questions) title description
Obviously want to ask some students in the school to do a questionnaire survey, for the objectivity of the experiment, he first used a computer generated n 1 to 1000
The random integer between (n≤1000), N is the user input, for the number of repetitions, only one is left, the remaining same number goes
Different numbers correspond to the student's number. And then the numbers from small to large, in accordance with the order of the row to find students to do the tune
Check. Please help clearly complete the "go to Heavy" and "sort" of work;
Tips:
Generate random numbers,
Import Random
Random.randint (1,1000)
The de-weight of the list

#!/usr/bin/env python
#coding: Utf-8

Import Random
s = Set ()
N = input (' Enter a number: ')
For I in Range (N):
S.add (Random.randint (1,1000))
#sorted是内置方法用来排序;
Print sorted (s)
#li = List (s)
#li. Sort ()
#print Li

Summarize
mutable data types: Lists, dictionaries, collections
Immutable data types: numeric types, strings, tuples
A variable data type implements a function that directly alters the variable data type;
Immutable data types implement a function that requires the assignment of the result to another variable;
Whether to implement a For loop
Iterative data types: str, list, tuple, dict, set
Non-iterative data type: Numeric type
Whether indexes, slices, duplicates, and join attributes are supported
Ordered data types: str, list, tuple
Unordered data type: Dict, set

Close relatives of dictionaries in python--collections

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.