10Python Notes-dict Delete/Method/Feature, set

Keywords: Python

[set] The elements inside a set are not allowed to repeat. The elements inside a set are out of order. The set elements are case sensitive.

[set creation] Using set(), and passing in a list, the elements of the list will be converted to the elements of the set

[Read set elements]

'Alice' in name_set # ==> True

[Add set element] If you add an element that already exists, you will not get an error or change anything

Individual additions:

name_set.add('Gina')

Multiple additions: add list directly

names = ['Alice', 'Bob', 'Candy', 'David', 'Ellena']
new_names = ['Hally', 'Isen', 'Jenny', 'Karl']
name_set = set(names)
name_set.update(new_names)

[Remove element of set]

Method 1: remove() Method: If the remove element is not in the set, an error will be raised, so you need to decide if it is in the set before reading it

Method 2: Use the discard() method to delete elements. When elements do not exist, using discard() will not cause errors.

[Clear set elements] clear() method to clear all elements

name_set.clear()
print(name_set) # ==> set([])

[Subsets and Supersets of set Sets]

s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Determine if s1 is a subset of s2
s1.issubset(s2) # ==> True
# Determining whether s2 is a superset of s1
s2.issuperset(s1) # ==> True

The isdisjoint() method can quickly determine if two set s are coincident, and if they are coincident, it returns False, otherwise it returns True.

s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
s1.isdisjoint(s2) # ==> False because there are repeating elements 1, 2, 3, 4, 5

The pop() method specifies the key of the element to be deleted and returns the corresponding value, which also causes an error when the key does not exist.

[dict's keys] can return all dict keys

for key in d.keys():
    print(key)
# ==> Alice
# ==> Bob
# ==> Candy

Get all dict value s

for key in d.values():
    print(key)
# ==> [50, 61, 66]
# ==> [80, 61, 66]
# ==> [88, 75, 90]

[dict Clear] clear() function

d.clear()
print(d) # ==> {}

[Characteristics of dict]

Fast search: dict has 10 elements or 100,000 elements, all at the same speed

Ordered and out of order: The disadvantage is that it takes up a lot of memory, wastes a lot of content, is ordered slowly but takes up a small amount

Keys are not mutable: tuple s can be dict keys, but list s cannot be dict keys, otherwise errors will be reported.

Walk through dict:

Method 1:

for key in d: # Traversing d's key
    value = d[key]
    if value > 60:
        print(key, value)

Method 2:

for key, value in d.items():
    if value > 60:
        print(key, value)
# ==> Candy 75
# ==> David 86

Method 3: Print out a complete dict;

1. The last three grades of your classmates are as follows. Please output each result of your classmates in turn. [Write the correct results in all the sentences you can think of, and then compare the better codes]

# Enter a code
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
for key,value in d.items():
    for score in value:
        print(key,score)

2. Please use set to represent five students in the class.
'Alice', 'Bob', 'Candy', 'David', 'Ellena'

# Enter a code
L = ['Alice', 'Bob', 'Candy', 'David', 'Ellena']
s = set(L)
print(s)

3. Since name_set does not recognize lower case names, please improve name_set so that lower case names can also be judged within name_set.

names = ['Alice', 'Bob', 'Candy', 'David', 'Ellena']
name_set = set(names)
names = ['Alice', 'Bob', 'Candy', 'David', 'Ellena', 'alice', 'bob', 'candy', 'david', 'ellena']
name_set = set(names)
print(name_set) # ==> set(['ellena', 'alice', 'Candy', 'Alice', 'candy', 'Ellena', 'Bob', 'David', 'bob', 'david'])

4. Add the following names to an empty set in two ways: ['Jenny','Ellena','Alice','Candy','David','Hally','Bob','Isen','Karl'].

# coding=utf-8
s=set()
s.add('Jenny')
L=['Jenny', 'Ellena', 'Alice', 'Candy', 'David', 'Hally', 'Bob', 'Isen', 'Karl']
s.update(L)
print(s)

5. Given a list for the following set, for each element in the list, if the set contains this element, delete it, otherwise add it to the set.

L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
S = set([1, 3, 5, 7, 9, 11])
# Enter a code
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
S = set([1, 3, 5, 7, 9, 11])
a = 0
for ch in L:
    if ch in S:
        S.remove(ch)
    else:
        S.add(L[a])
    a = a + 1
print(S)

6.If you know two sets s1 and s2, determine if they are coincident, and if they are, print out the coincident elements.

s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Enter a code
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
if s1.isdisjoint(s2):
    print('no')
else:
    for ch in s1:
        if ch in s2:
            print(ch)

 

Posted by StormTheGates on Sun, 10 Oct 2021 10:00:33 -0700