The third day of learning python (string, tuple, list, dictionary) is tough, but stick to it.

Daily Drink: Exercise when you're not in good shape, and try to earn when you don't have money. Don't turn your embarrassment on others. The only thing you can complain about is yourself who doesn't work hard enough.

Topic

Strings and Common Data Structures

Character string

str1 = 'hello,word!'
#String length
print(len(str1))
#Gets the capitalization of the first letter of the string
print(str1.capitalize())
#Get all uppercase copies
print(str1.upper())
#Find the location of the substring from the string
print(str1.find('or')) #Returns the first letter of the query in the index of the string, and find throws an exception when it cannot find the substring
#Check that the string begins and ends with the specified string
print(str1.startswith('he'))  #true
print(str1.endswith('ds'))    #false
#Centralize the string with the specified width and fill the specified characters on both sides
print(str1.center(50,'*'))
# Place the string to the left with the specified width and fill the right with the specified character
print(str1.rjust(50, '*'))
print(str1.ljust(50,'*'))
str2 = 'abc123456'
# Check that strings are made up of numbers
print(str2.isdigit())
# Check that strings are composed of letters
print(str2.isalpha())
#Check whether strings are composed of numbers and letters
print(str2.isalnum())
#Get the left and right sides of the string trimmed
str3 = '    jskdalkds j   sada'
print(str3.strip())
print(str3.lstrip())
print(str3.rstrip())

Use list

list1 = [12,34,5,5,7,8,9]
# print(list1)
# Repeat five times
# list2 = ['HELLO'] * 5
# print(list2)
# Calculate the list length (number of elements)
# print(len(list1))
# Additive elements
# list1.append(200)
# list1.insert(1, 400)
# list1 += [1000, 2000]
# print(list1)
# print(len(list1))
# Delete elements
# print(list1.remove(12))
# del list1[1]
# print(list1)
#Clear list elements
# print(list1.clear())

fruits = ['grape', 'apple', 'strawberry', 'waxberry']
fruits += ['asd','asdwrf','yutfhgh']
# print(fruits)
# Loop through list elements
# for fruit in fruits:
    # print(fruit.title(),end="")
#List slice

# fruit2 = fruits[1:2]
# print(fruit2)
# The default sort is ascending
list2 = sorted(list1)
print(list2)
# The sorted function returns a sorted copy of the list without modifying the incoming list
# Functions should be designed as sorted functions without side effects as much as possible.
list3 = sorted(list1,reverse=True)
print(list3)
# Specify sorting by string length instead of default alphabetical order by key keyword parameter
list4 = sorted(fruits, key=len)
print(list4)
# Send a sort message to the list object and sort it directly on the list object
list1.sort(reverse=True)
print(list1)

List Generator

Transforming a function into a generator function by using the yield keyword

import sys
#A simple list generator x is a parameter, assigning values to X in each loop and putting them in the list in turn
f = [x for x in range(10)]
print(f)
#Conditional Generator, put the conditions in the list
f1 = [x for x in range(10) if x%2 == 0]
print(f1)
# Loop nesting, equivalent to outer x, inner y, into the list in turn
f3 = [x + y for x in 'ABCDE' if x == 'A' for y in '1234567']
print(f3)
# After creating lists with this syntax, the elements are ready, so it takes a lot of memory space.
f = [x ** 2 for x in range(1, 1000)]
print(sys.getsizeof(f))  # View the number of bytes of memory occupied by objects
# View the number of bytes of memory occupied by the object print(f)
# Data can be obtained through a generator, but it does not take up extra space to store data.
# Get data through internal operations every time you need it (it takes extra time)
# Compared with the generative generator, it does not occupy the space for storing data.
f = (x ** 2 for x in range(1, 1000))
print(sys.getsizeof(f)) 

tuple

Note that tuples are not modifiable elements, others are similar to lists. Tuples are superior to lists in terms of creation time and space occupied

t = ('Ha-ha', 38, True, 'Edward')
# Get elements in tuples
print(t[0])
print(t[3])
#Traversing values in tuples
for num in t:
    print(num)

# Variable t refers to a new tuple and the original tuple will be overwritten
t = ('I am there.', 20, True, 'Who are you')
# Converting tuples into lists
person = list(t)
print(person)
#Converting lists to tuples
list1 = tuple(person)
print(list1)

aggregate

set1 = {1, 2, 3, 3, 3, 2}
print(set1)
# print('Length =', len(set1))
set2 = set(range(1, 10))
# print(set2)
# set1.add(4)
# set2.update([11, 12])
# print(set1)
# print(set2)
# set2.discard(5)
# The absence of remote elements causes KeyError
# set1.remove(7)
# Traversing Collection Containers
# for i in set2:
    # print(i**2,end='')
#Converting tuples into collections
# set3 = set(1,23,4,56,4)
# print(set3.pop())
# print(set3)
# Intersection, Union and difference sets of sets
print(set1 & set2)
print(set1.intersection(set2))
print(set1 | set2)
print(set1.union(set2))
print(set1 - set2)

The difference between tuple remove () hediscard ()

Dictionaries

Introduction: All operations of a dictionary are performed by keyname. A dictionary is disordered and has no indexing operation unless converted into an ordered dictionary.

score = {'as':54,'Worship':78,'Defender':96}
# The corresponding values in the dictionary can be obtained by keys
# print(score['as'])
# print(score ['Baiba'])
# Traverse the dictionary (traversing the keys and then getting the corresponding values through the keys)
# for i in score:
#     print('%s\t--->\t%d' % (i, score[i]))

# Update the elements in the dictionary
# score['as'] = 56
# print(score)
#Additive elements
score.update(Cold noodles=67, Fang Qi He=85)
print(score)
if 'Dictionaries' in score:
    print(score['Dictionaries'])
    print(score.get('Dictionaries'))

print(score.get('Xie', 60))
# Delete elements from a dictionary
print(score.popitem())
print(score)
print(score.pop('as',54))
score.clear()
print(score)

Posted by quiksilver on Fri, 04 Oct 2019 18:40:00 -0700