Summary of basic topics of introduction to python [just less than two months]

As a freshman, I started my 0 programming basic Python self-study journey. It took me seven weeks to learn python. In these seven weeks, I learned and completed the following 22 basic Python introduction questions (far more than 22...) and summarized here.

1. Xiao Ou takes 100 yuan, 5 yuan for a rooster, 3 yuan for a hen and 1 yuan for three chicks. Buy a hundred and ask how many cocks, hens and chicks each. Please solve it by list derivation!

The total amount is 100 yuan and the total amount is 100. This is a very simple ternary quadratic equation problem. Let's set the number of cocks as x, the number of hens as y and the number of chicks as z. we can list the expression

x + y + z = 100,
5 * x + 3 * y + z / 3 = 100

Through simple judgment, the maximum number of cocks is 20, the maximum number of hens is 33 and the maximum number of chicks is 100. The conditions are introduced through the list expression and the list of x, y and z can be directly output through print output

# cock = x
# hen = y
# chick = z
list1 = [(x, y, z) for x in range(21) for y in range(34) for z in range(101) if
         x + y + z == 100 and 5 * x + 3 * y + z / 3 == 100]
print(list1)

The results are as follows:

[(0, 25, 75), (4, 18, 78), (8, 11, 81), (12, 4, 84)]

2. There are the following value sets [11,22,33,44,55,66,77,88,99,90], save all values greater than 66 to the first key of the dictionary, and save values less than 66 to the values of the second key.

In this question, he asked us to separate the values greater than 66 and less than 66 in the list into two lists and save them in the dictionary. We can export the list through traversal, and then add them to the empty list built in advance by judging whether they are greater than or less than 66, so as to obtain two lists with values greater than 66 and less than 66 respectively, At the same time, the title requires that all values greater than 66 be saved to the first key of the dictionary, and values less than 66 be saved to the value of the second key, so we can create the dictionary

dict1 = {'k1': s1, 'k2': s2}

You can meet the requirements of the topic

list1 = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
s1 = []
s2 = []
for i in list1:
    if i > 66:
        s1.append(i)
    if i < 66:
        s2.append(i)
dict1 = {'k1': s1, 'k2': s2}
print(dict1)

The results are as follows:  

{'k1': [77, 88, 99, 90], 'k2': [11, 22, 33, 44, 55]}

List derived version:  

list1 = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
s1 = [i1 for i1 in list1 if i1 < 66]
s2 = [i2 for i2 in list1 if i2 > 66]
dict1 = {'k1': s1, 'k2': s2}
print(dict1)

The results are consistent with the above

3. Use lambda function to sum 1 ~ 1000

The purpose of this question is to investigate the application of lambda function. We can know from the study of lambda function that lambda function can accept a parameter and return a value (it is recommended to see an example for details)

For summation, we can use the sum () function

as follows

num1 = lambda a:sum(range(a+1))

We only need print(1000) to output the sum of the first 1000

num1 = lambda a:sum(range(a))
print(num1(1001))

The results are as follows:

500500

4. Please program the lottery process shown in the figure below and package it into a function.

This topic mainly focuses on our four points, how to distinguish whether there is a winning or not, the encapsulation of functions, the application of time module, and the line feed of strings when drawing patterns.     Here we use def to encapsulate a function, use sleep in the time module to complete the countdown, and use \ n after the string to achieve the effect of line feed, so as to draw the pattern.

  In judging whether there is a winner or not, I use the choice in the random module to achieve the lottery function

  random.choice([0, 0, 0, 1, 1, 2])

According to the different numbers of 0, 1 and 2 in the list, he is divided into three kinds of awards with different probabilities, and then judge which number he is through if, so as to judge his first prize

The original requirement of the title was to let us judge whether we won the prize or not, but I made a slight improvement here, which turned into winning several prizes, and then drew the pattern we liked, adding a little evil interest. This may be a little fun for our novices in programming.

def roll():
    import random
    import time
    num1 = random.choice([0, 0, 0, 1, 1, 2])
    print('Countdown to lottery 3')
    time.sleep(0.5)
    print('Lottery countdown 2')
    time.sleep(1)
    print('Countdown to lottery 1')
    time.sleep(1.5)
    print('   /> _ フ\n'
'  |          |\n'
' |   .   .  | \n'
' /`   _x_ ノ\n'
' /  ヽ   ノ\n'
'/ ̄| | | |\n'
'| ( ̄ヽ_ヽ_)__)\n')
    if num1 == 0:
        return 'Nigger, I didn't win'
    if num1 == 1:
        return 'Congratulations on the third prize of junior European secondary school'
    if num1 == 2:
        return 'Congratulations on the second prize of small European middle school'
#     Smile to death, do you think there is really a first prize??

print(roll())

  The output results are as follows:  

Countdown to lottery 3
 Lottery countdown 2
 Countdown to lottery 1
   /> _ フ
  |          |
 |   .   .  | 
 /`   _x_ ノ
 /  ヽ   ノ
/ ̄| | | |
| ( ̄ヽ_ヽ_)__)

Nigger, I didn't win

5. Play a game of scissors, stone and cloth with the computer: the computer punches randomly. What can we choose. (the computer comes out randomly, the user manually inputs and needs to judge whether the input is legal) encapsulated as a function.

We want the computer to punch randomly. We can use the choice in the random module to select one of the stone scissors cloth as the punch of the computer, and then the player manually outputs the stone scissors cloth through input to determine his own punch. Through the if statement and our experience in playing stone scissors cloth, we can judge the victory or defeat directly with the computer.

def scissors():
    import random
    sfist = ['stone', 'scissors', 'cloth']
    computer = random.choice(sfist)
    player = input('Please punch: (stone, scissors, cloth)')
    if player in sfist:
        print('--Combat process————')
        print(f'The computer is out of order{computer}')
        print(f'What's wrong with you{player}')
        print('--Results————')
    if player not in sfist:
        print('Something went wrong')

    if player == 'stone' and computer == 'scissors' or player == 'scissors' and computer == 'cloth' or player == 'cloth' and computer == 'stone':
        return 'You win'
    elif computer == player:
        return 'it ends in a draw'
    else:
        return 'You lost'


print(scissors())

The output results are as follows:

Please punch: (stone, scissors, cloth) stone
--Combat process————
The computer is out of order
 You're out of stone
--Results————
You win

6. Xiao Ming, Xiao Hong and Xiao Gang are classmates and sit in the same row, first, second and third respectively.
Because they are all about the same height, the teacher plans to let the three of them sit first in turn.
Every time I change seats, the first one changes to the third, and the last two move forward

We can know this question clearly: use subscript transposition.

When I first started this problem, I made a fatal mistake, that is, transposition one by one, as shown in the figure:

Here, Xiao Ming's variable is deleted when changing the variable

In order to change the position while retaining the variable, it is necessary to transpose at the same time

  As follows:

list1 = ['Xiao Ming', 'Xiao Hong', 'Xiao Gang']
list1 = [list1[1], list1[2], list1[0]]
print(list1)

Next, we just need to modify it slightly, put it into the loop, and print the results of multiple transpositions. Here is the results of the first 20 transpositions

list1 = ['Xiao Ming', 'Xiao Hong', 'Xiao Gang']
num = 1
while num <= 20:
    if num == 1:
        print(f'The first{num}Secondary seats are:', list1)
    if num != 1:
        list1 = [list1[1], list1[2], list1[0]]
        print(f'The first{num}Secondary seats are:', list1)
    num += 1

The output is as follows:

The first seat is: ['Xiao Ming', 'Xiao Hong', 'Xiao Gang']
The second seat is: ['Xiao Hong', 'Xiao Gang', 'Xiao Ming']
The third seat is: ['Xiao Gang', 'Xiao Ming', 'Xiao Hong']
The 4th seat is: ['Xiao Ming', 'Xiao Hong', 'Xiao Gang']
The 5th seat is: ['Xiao Hong', 'Xiao Gang', 'Xiao Ming']
The 6th seat is: ['Xiao Gang', 'Xiao Ming', 'Xiao Hong']
The 7th seat is: ['Xiao Ming', 'Xiao Hong', 'Xiao Gang']
The 8th seat is: ['Xiao Hong', 'Xiao Gang', 'Xiao Ming']
Seats for the 9th meeting are: ['Xiao Gang', 'Xiao Ming', 'Xiao Hong']
The 10th seat is: ['Xiao Ming', 'Xiao Hong', 'Xiao Gang']
Seats for the 11th meeting are: ['Xiao Hong', 'Xiao Gang', 'Xiao Ming']
The 12th seat is: ['Xiao Gang', 'Xiao Ming', 'Xiao Hong']
The 13th seat is: ['Xiao Ming', 'Xiao Hong', 'Xiao Gang']
The 14th seat is: ['Xiao Hong', 'Xiao Gang', 'Xiao Ming']
The 15th seat is: ['Xiao Gang', 'Xiao Ming', 'Xiao Hong']
The 16th seat is: ['Xiao Ming', 'Xiao Hong', 'Xiao Gang']
Seats for the 17th meeting are: ['Xiao Hong', 'Xiao Gang', 'Xiao Ming']
The 18th seat is: ['Xiao Gang', 'Xiao Ming', 'Xiao Hong']
The 19th seat is: ['Xiao Ming', 'Xiao Hong', 'Xiao Gang']
The 20th seat is: ['Xiao Hong', 'Xiao Gang', 'Xiao Ming']

7. Make a simple account and password login system

The requirements are as follows:

i. There are multiple users in the system. The user's information is currently saved in the list, and the account and password correspond one by one

    users = ['root','westos']

    passwd = ['123','456']

ii. User login (judge whether the user login is successful)

     1) . judge whether the user exists

     2) . if present

         1) . judge whether the user password is correct

         If correct, log in successfully and launch the cycle

         If the password is incorrect, log in again. There are three chances to log in

     3) . if the user does not exist

Re login, a total of three opportunities

Here, let's sort out our ideas. First, we need to enter the account password to judge whether they are the account password in our list. At the same time, the account password should correspond one by one.

My idea is to use the index function to extract the index value of the input password in the password list, and then use this index value as the index value of the account list, so as to achieve the goal in one step.

    if key == passwd[users.index(user)]:
        print('Successfully logged in')

Here, we need three login opportunities. It is undoubtedly cumbersome for us to judge the code repeatedly three times. Therefore, I encapsulate the judgment of successful login as a function to judge the second and third login, so as to effectively simplify the code.

users = ['root', 'westor']
passwd = ['123', '456']

def sign():
    user = input("Enter your account:")
    key = input("Enter your password:")
    if key == passwd[users.index(user)]:
        print('Successfully logged in')
        return True
    else:
        return False

user = input("Enter your account:")
key = input("Enter your password:")
n = 0
while n < 2:
    if key == passwd[users.index(user)]:
        print('Login succeeded')
        break
    else:
        print('Account or password error')
        if sign():
            break
    n += 1

8. Number guessing game

requirement:

      1. The system randomly generates a number from 1 to 100;

      2. Users have 5 chances to guess numbers in total;

      3. If the number guessed by the user is greater than the number given by the system, print "too big";

      4. If the number guessed by the user is less than the number given by the system, print "too small";

      5. If the number guessed by the user is equal to the number given by the system, print "Congratulations" and exit the cycle;

  The idea of this question is very clear. The system lets us randomly generate a 1 to 100, then let us guess the number, and then have five chances

  We can use the randint of the random module to obtain a random number from 1 to 100

  Make five judgments through the for loop. If the judgment is successful, use break to exit the loop

import random

num = random.randint(1, 100)
for i in range(5):
    user = int(input('Please enter a number within 100'))
    if user < num:
        print('too small')
    if user > num:
        print('too big')
    if user == num:
        print('congratulations')
        break

9. Given a sentence (containing only letters and spaces), reverse the position of the words in the sentence. The words are divided by spaces. There is only one space between the words, and there is no space before > after.

For example: (1) "hello xiao mi" - > "mi xiao hello"

Here, I want to reverse the word order in the input string and then output it. My first reaction here is to use slicing and cutting

Through observation, we can see that we can cut through the space "" and use the split function to cut the string of English words and return the words to the list, as shown in the figure:

['I', 'love', 'python']

We can use slice [:: - 1] to reverse its order, and then put it into an empty string one by one through traversal

temp1 = 'I love python'
list1 = temp1.split(' ')
list2 = list1[::-1]
temp2 = ''
for i in list2:
    temp2 = temp2 + i + ' '
print(temp2)

The output result is:

python love I 

10. Topic content: Hello, people call me 'programmer'. People in the world choose to ignore me. Only you see me and say hello to me. I feel very lonely. Are you willing to become friends with me?

Please print this paragraph in the following format:
Hello, people call me 'programmer'
People in this world choose to ignore me
Only you saw me and said hello to me
I feel very lonely, very lonely
Would you like to be friends with me?

Tip: this topic needs to use three knowledge points: line feed, use escape characters to print single quotation marks, and print functions

You can see from a glance that the text in this question is separated by spaces. We can use replace to replace the spaces with \ n to realize line feed. In the question, we need to escape the single quotation mark with an escape character, that is, add \ to the single quotation mark to escape the symbol.

It's simple. There's nothing to say

txt = 'Hello, people call me\'programmer\' People in this world choose to ignore me. Only you see me and say hello to me. I feel very lonely. Are you willing to be friends with me?'
print(txt.replace(' ','\n'))

The output results are:

Hello, people call me'programmer'
People in this world choose to ignore me
 Only you saw me and said hello to me
 I feel very lonely, very lonely
 Would you like to be friends with me?

11. Define a Person class, use the Person class, create a mayun object, and add the company attribute with the value "Alibaba"; Create a wangjianlin object, add the company attribute, and the value is Wanda Group

This problem requires us to define a person class. We can create a class through class, then create an object and add attributes. This part can be completed as long as we study object-oriented and understand object-oriented.

class Person:
    pass


mayun = Person()
mayun.company = "alibaba"

wangjianlin = Person()
wangjianlin.company = "wanda"

12. The bonus paid by the enterprise shall be deducted according to the profit (I):

If it is less than or equal to 100000 yuan, the bonus can be increased by 10%;

When the profit is higher than 100000 yuan and lower than 200000 yuan, the part lower than 100000 yuan will be deducted by 10%, and the part higher than 100000 yuan will be deducted by 7.5%;

Between 200000 and 400000 yuan, the part higher than 200000 yuan can be deducted by 5%;

3% commission can be given for the part higher than 400000 yuan between 400000 and 600000 yuan;

When it is between 600000 and 1 million yuan, the part higher than 600000 yuan can be deducted by 1.5%;

When it is higher than 1 million yuan, the part exceeding 1 million yuan shall be deducted by 1%.

Enter the profit I of the current month from the keyboard to find the total amount of bonus to be paid?

The model of this question is similar to that of tax payment. Different percentages are taken as bonuses in different parts of profits. I provide two ways to write it as follows:

1, Not easy to understand, but logical

I = float(input('Input profit (10000 yuan)'))
money = 0
mon = [10, 10, 20, 20, 40]
mp = [0.1, 0.075, 0.05, 0.03, 0.015, 0.01]
award1 = 0
num = 0
for i in range(len(mon)):
    money += mon[i]
    award = mon[i]*mp[i]
    award1 += award
    num += mon[i]
    if money > I > 10:
        all_award = award1 - award +(I-(num - mon[i]))*mp[i]
        break
    if I < 10:
        all_award = 0.1*I
        break
    if I > 100:
        all_award = award1 + (I -100)*0.01
        break
print(all_award,'Ten thousand yuan')

2, It is easy to understand and can be immediately thought of by many people, but it is complicated and the assignment of num in the code is cumbersome

I = float(input('Please enter the profit of the current month in 10000 yuan:'))
if I <= 10:
    num = 10 * 0.1
elif 10 < I <= 20:
    bns = 10 * 0.1 + (I-10) * 0.075
elif 20 < I <= 40:
    num = 10 * 0.1 + 10 * 0.075 + (I-20) * 0.05
elif 40 < I <= 60:
    num = 10 * 0.1+ 10 * 0.075 + 20 * 0.05 + (I-40) * 0.03
elif 60 < I <= 100:
    num = 10 * 0.1+ 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (I-60) * 0.015
elif I > 100:
    num = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (I-100) * 0.015
print(num, 'Ten thousand yuan')

It is strongly recommended to understand the first one, which greatly improves my thinking. Anyway, I write in a muddle. It will take a long time to understand what I wrote.

13. There are the following figures:

05 04 03
06 01 02
07 08 09

Expand to include up to the first 25 numbers like this:

17 16 15 14 13
18 05 04 03 12
19 06 01 02 11
20 07 08 09 10
21 22 23 24 25

You may easily notice that the first layer (and the innermost layer 01) contains only one number (), the second layer - immediately around it - contains eight numbers (numbers in the range of 02-09), and so on.

Your task is to create a layers(n) function for a given number, which returns n on which layer

example:

layers(1)  return 1
layers(5)  return 2
layers(25) return 3
layers(30) return 4
layers(50) return 5

This problem looks fancy. In fact, it's not difficult at all. It's just the difficulty of primary school Olympic mathematics

We know from the title that there are 1 number in the first layer, 9 in the first two layers and 25 in the first three layers. According to the mathematical logic ability of yidui, we know that the square of (number of layers x2-1) is the number of numbers in those layers. At the same time, it is not difficult to find that the largest number of each layer is the square of (number of layers x2-1).

We know that any number is less than or equal to the maximum number of the floor where he is located, but greater than the maximum number of the upper floor of the floor where he is located. We start here:

0 <= (2*num2-1) ** 2 - num1 <= (2*num2-1) ** 2 - (2*num2-3) ** 2

Through the cycle, we judge from the first layer, gradually increase the number of layers, exit the cycle and output when the conditions are met to complete the task

def layers(num1):
    num2 = 1
    while True:
        if 0 <= (2*num2-1) ** 2 - num1 <= (2*num2-1) ** 2 - (2*num2-3) ** 2:
            print(num2)
            break
        else:
            num2 += 1
layers(66666)

Here I judge the number of floors where 66666 is located

The output results are as follows:

130

  14. Your task is to create a two-dimensional array with 20 rows and 5 columns. Its value range is 0-9 (it needs to be an integer). Randomly select 20 positions from all its values for modification. The modified value is 100.

It is recommended to learn numpy module before doing

python itself does not have the concept of array. Here we need to refer to the numpy module. We create a two-dimensional array through the numpy module and assign values randomly. The method is as follows:

a = numpy.random.randint(0, 10, size=[20, 5])

In the title, we are required to change the number of 20 positions to 100 randomly. My first reaction is to cycle 20 times and make random changes, but there is a problem. We may change the same number twice. So I changed my mind: in this two-dimensional array, there are 20 rows and 5 columns, that is, there are 100 numbers. Therefore, in 100, I randomly take 20 numbers to correspond to each position of the two-dimensional array one by one, and then process these numbers to obtain the coordinates of these numbers. The processing is as follows:

x1 = i // 5
x2 = i % 5

We use x1 and x2 as index values to find the corresponding position of the array and modify it. The total code is as follows:

import random
import numpy

a = numpy.random.randint(0, 10, size=[20, 5])
x = random.sample(range(100), 20)
for i in x:
    x1 = i // 5
    x2 = i % 5
    a[x1][x2] = 100
print(a)

15.A Jun is currently working as an intern in an asset appraisal company. His daily work is to travel all over the residential areas in the urban area to investigate the address, construction year and the orientation and area of each household in each unit of each building. For example, the information of a household is: 401 households, unit 2, building 3, Fu'an Garden (community); Orientation: North-South; Area: 90 square meters. He needs to record the information of each household. In order to save time, they usually draw the data of all units in a building on a plan on the spot. After going back, input the data recorded in the plan into the excel table.

For example, the above plan shows that this building has four units, each unit has 15 floors, of which unit 1 and unit 2 have two households, with an area and orientation of 140 square meters and a north-south orientation. There are four households in unit 3, including two households with an area of 60 square meters, facing north-south, and two households with an area of 80 square meters, facing East-West; Unit 4 has four households, covering an area of 70 square meters and facing east-west. A Jun complains that it's tiring to see the house, and it's more annoying to enter. Maybe you don't think it's very convenient to use the automatic filling function of excel. It's far from so simple. Indeed, there are some duplicate cells that can be copied and pasted as long as they are filled in once. For a Jun (excel rookie), it is only necessary to manually enter the house number each time. (of course, mastering some excel skills can save some effort)

However, the number of households, orientation and area of different units are not exactly the same, so the data of one unit cannot be copied to other units. For example, it is difficult to input the data of unit 1 and copy it to unit 2. However, because there are 4 households in unit 3, the area and orientation are different, unit 3 has to be reprocessed, and the household room number, area and orientation are different.

Our task is to use the program to automatically enter data, and even excel does not need to be opened. After running the program in the terminal, open the file corresponding to excel, and you will find that the data is entered according to a certain law. (CSV format)

We have to learn the CSV module first

Here I push this Python writes data to a CSV file_ Blog of lbj126020629 - CSDN blog

From the observation table, we can find that the only change here is the room. According to the law found here, we can draw a conclusion. Starting from 201, each next grid takes turns + 1 and + 99. We establish a variable to store the value of the room, continuously change the value of the room through circulation and generate a row of Excel table to achieve the task. The code is as follows:

import csv

a = 201
f = open('House type.csv', 'w', encoding='utf-8', newline="")
csv_writer = csv.writer(f)
csv_writer.writerow(["Cell name", "address", "Year Built ", "Building", "unit", "House type", "orientation", "the measure of area"])
for i in range(10):
    for m in range(2):
        list1 = ['Tipping Garden', "66 nongyuan Road", '2022 year', '1 Building', '1 unit', a, 'north and south', 140]
        csv_writer.writerow(list1)
        a += 1
    a += 98

The reason why it is + 98 is that in the step of + 99, a+=1 is performed in the embedded cycle, so a does not need + 99, but only + 98 to meet the requirements

16. Your task is to obtain and output the source code of Zhihu web page, and finally save it as txt text with the link as follows: https://www.zhihu.com/

The task of this problem is a crawler. This is a very simple crawler. It doesn't even have any anti crawling. It only needs UI camouflage

import requests
url = 'https://www.lagou.com/shenzhen-zhaopin/Java/'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                         'Chrome/95.0.4638.69 Safari/537.36'}
page_text = requests.get(url=url, headers=headers).text
with open('./text', 'w', encoding='utf-8') as fp:
    fp.write(page_text)

print(page_text)

17. Use regular expressions to obtain all jpg ending web page image links from the following contents, such as (this is only one of them):
http://kr.shanghai-jiuxin.com/file/2021/1112/small95a18b4db31f407b3cd7c275d2eb7e36.jpg

<a href="/bizhitupian/diannaobizhi/240508.htm" class="TypeBigPics" target="_blank"><img src=" http://kr.shanghai-jiuxin.com/file/2021/1112/small95a18b4db31f407b3cd7c275d2eb7e36.jpg "  />< Span > King glory Daji maid coffee hairtail screen wallpaper < / span > < / a > < div class = "txtinfo gray" > < EM class = "icolist" > View: 0 times < / EM > < EM class = "icotime" > 11-12 < / EM > < / div > < / div > < / Li > < li > < a href = "/ bizhitupian / shoujibizhi / 240460. HTM" class = "typebigpics" target = "_blank" > < img SRC= " http://kr.shanghai-jiuxin.com/file/2021/1111/small61c49fcdf69f916671dd183b6db267c5.jpg "  />< Span > windows 11 4k desktop wallpaper < / span > < / a > < div class = "typepicinfos" > < div class = "txtinfo gray" > < EM class = "icolist" > View: 0 times < / EM > < EM class = "icotime" > 11-11 < / EM > < / div > < / div > < / Li > < li > < a href = "/ bizhitupian / shoujibizhi / 240459. HTM" class = "typebigpics" target = "_ blank"><img src=" http://kr.shanghai-jiuxin.com/file/2021/1111/small8a95f9c4ad7bf3c7bb72233739d5da78.jpg "  />< Span > Microsoft win11 4k landscape wallpaper < / span > < / a > < div class = "typepicinfos" > < div class = "txtinfo gray" > < EM class = "icolist" > View: 0 times < / EM > < EM class = "icotime" > 11-11 < / EM > < / div > < / div > < / Li > < li > < a href = "/ bizhitupian / shoujibizhi / 240458. HTM" class = "typebigpics" target = "_ blank"><img src=" http://kr.shanghai-jiuxin.com/file/2021/1111/small28c61714c3cd01ebbb53ef160c75b20d.jpg "  />< Span > Microsoft windows11 landscape 4k wallpaper < / span > < / a > < div class = "typepicinfos" > < div class = "txtinfo gray" > < EM class = "icolist" > View: 0 times < / EM > < EM class = "icotime" > 11-11 < / EM > < / div

Here, we are required to use regular expressions to extract relevant contents. We can know from simple data observation that every content we need is in target = "_ Blank "> < img SRC = and jpg directly. We can extract them directly with regular expressions. The code is as follows

import re
url = '<a href="/bizhitupian/diannaobizhi/240508.htm" class="TypeBigPics" target="_blank"><img src="http://Kr.shanghai-jiuxin.com/file/2021/1112/small95a18b4db31f407b3cd275d2eb7e36. JPG "/ > < span > King glory Daji maid coffee hairtail screen wallpaper < / span > < / a > < div class =" txtinfo gray "> < EM class =" icolist "> View: 0 times < / EM > < EM class =" icotime "> 11-12 < / EM > < / div > < / div > < / Li > < li > < a href =" / bizhitupian / shoujibizhi / 240460.htm "class =" typebigpics "target ="_ blank"><img src=" http://kr.shanghai-jiuxin.com/file/2021/1111/small61c49fcdf69f916671dd183b6db267c5.jpg "/ > < span > windows 11 4k desktop wallpaper < / span > < / a > < div class =" typepicinfos "> < div class =" txtinfo gray "> < EM class =" icolist "> View: 0 times < / EM > < EM class =" icotime "> 11-11 < / em > < / div > < / div > < / Li > < li > < a href =" / bizhitupian / shoujibizhi / 240459. HTM "class =" TypeBigPics" target="_ blank"><img src=" http://kr.shanghai-jiuxin.com/file/2021/1111/small8a95f9c4ad7bf3c7bb72233739d5da78.jpg "/ > < span > Microsoft win11 4k landscape wallpaper < / span > < / a > < div class =" typepicinfos "> < div class =" txtinfo gray "> < EM class =" icolist "> View: 0 times < / EM > < EM class =" icotime "> 11-11 < / em > < / div > < / div > < / Li > < li > < a href =" / bizhitupian / shoujibizhi / 240458. HTM "class =" TypeBigPics" target="_ blank"><img src=" http://kr.shanghai-jiuxin.com/file/2021/1111/small28c61714c3cd01ebbb53ef160c75b20d.jpg "/ > < span > Microsoft windows11 landscape 4k wallpaper < / span > < / a > < div class =" typepicinfos "> < div class =" txtinfo gray "> < EM class =" icolist "> View: 0 times < / EM > < EM class =" icotime "> 11-11 < / em > < / div '
ex = 'target="_blank"><img src="(.*?.).jpg'
page_text1 = re.findall(ex,url, re.S)
for i in page_text1:
    print(i)

The output results are:

http://kr.shanghai-jiuxin.com/file/2021/1112/small95a18b4db31f407b3cd7c275d2eb7e36
http://kr.shanghai-jiuxin.com/file/2021/1111/small61c49fcdf69f916671dd183b6db267c5
http://kr.shanghai-jiuxin.com/file/2021/1111/small8a95f9c4ad7bf3c7bb72233739d5da78
http://kr.shanghai-jiuxin.com/file/2021/1111/small28c61714c3cd01ebbb53ef160c75b20d

18. Write a program:

score = [23, 54, 65, 1, 0, 34, 67, 22]

For the above score, use the for loop method to sort and output from small to large. The sort function is prohibited

The original idea of this question is to let us use the bubble sorting method (it is recommended to go to the tutorial)

The code is as follows

score = [23, 54, 65, 1, 0, 34, 67, 22]
for i in range(len(score)):
    for j in range(0, len(score) - i - 1):
        if score[j] > score[j + 1]:
            score[j], score[j + 1] = score[j + 1], score[j]
print(score)

But!!! I found a better way to understand

We use the min function to find the smallest number in the list, then add it to a new list, and delete the smallest number. In this cycle, we eliminate the smallest number each time until the sorting is completed

score = [23, 54, 65, 1, 0, 34, 67, 22]
list1 = []
for i in range(len(score)):
    list1.append(min(score))
    score.remove(min(score))
print(list1)

19. Define an automobile class, define a move method in the class, then create BMW_X9 and AUDI_A9 objects respectively, add attributes such as color, horsepower and model, print out attribute values respectively, and call the move method (after the move method is called, output -- the car starts running --)

This question is about how to create classes, methods, objects and attributes. There's nothing to say. I put down the code and have to:

class Car:
    def __init__(self, color, horsepower, model):
        self.horsepower = horsepower
        self.model = model
        self.color = color
    def move(self):
        print(f"{self.color}of{self.model}with{self.horsepower}Flying by")
BMW_X9 = Car('blue', '123456 code', 'BMW_X9')
AUDI_A9 = Car('black', '666666 code', 'AUDI_A9')
BMW_X9.move()
AUDI_A9.move()

20. Xiao Ou wants to invite some students to do a questionnaire survey at school. In order to make the experiment objective, he first uses the computer to generate N random integers between 1 and 1000 (n < = 1000) , n is entered by the user. For the repeated numbers, only one is reserved, and the other same numbers are removed. Different numbers correspond to different student numbers. Then sort these numbers from small to large, and find the students to investigate according to the arranged order. Please help Xiao Ou complete the "de duplication" and "sorting"

Here we have several tasks:

        Enter N

        Generate N random numbers

        De duplicate the generated N numbers

Here, we can input N through input, generate random numbers through randint of random module, and use the nature of the set to remove duplication. The code is as follows:

import random

set1 = set()
N = int(input())
for i in range(N):
    num = random.randint(1, 1001)
    set1.add(num)
set2 = sorted(set1)
print(set2)

21. Write a program, assuming Xiao Ou's birthday is December 1, 2002, and take his birthday as the fixed value. Take today, November 14, 2021 as an example, as the input, the output Xiao Ou is 18 years old this year! If take December 1, 2021 as an example, the output Xiao Ou is just 19 years old. You can go to nongyuan road to celebrate! Take October 15, 2004 as an example, the output Xiao Ou is 1 year old this year!

He has given us a time for this problem. We need to input a time by ourselves, then calculate the difference between the two times and output the difference in the way of tailing. The way we input the time here is: xx, xx, xxxx. In order to calculate, we can replace the three Chinese words of mm / DD / yy through the combination of replace function and split function, and The cutting method is as follows:

bir1 = bir.replace('year', ',').replace('month', ',').replace('day', '')#Remove the words year, month and day and separate them with commas
bir2 = bir1.split(',')#Put the month, year and day into the list respectively, and sort by month, year and day

Then, after cutting, we get a list in the form of [year, month, day]. We can subtract the month, month and day from the previously given date, and convert the month and day into the adult unit.

bir = str(input())#Enter month, year and day
bir1 = bir.replace('year', ',').replace('month', ',').replace('day', '')#Remove the words year, month and day and separate them with commas
bir2 = bir1.split(',')#Put the month, year and day into the list respectively, and sort by month, year and day
a = 2002
b = 12
c = 1#Assign the determined date with abc respectively
years = (((int(bir2[0]) -a )+(int(bir2[1]) - b)/12 +(int(bir2[2])-c)/365))//1# respectively calculate the difference between the date and the input date, and unify it into the unit of year and round down
print(f'Xiao Jie this year{years}One year old')

22. Crawl the position about Java in the dragnet and convert it to CSV format

What we need to pay attention to in this problem are

        1. How to crawl from one page to the next

        2. How to process data

First, we can observe the url. The difference between the first page and the second page lies in 1 and 2 in the url. According to this law, we only need to add one to the corresponding value of the page number in each cycle. At the end of the cycle, when we reach the end of the page number, with an error, it means that we have finished crawling.

I have tried two methods to deal with the problem of data, regular expression and xpath

Regular expression:

import requests
import re
import csv
if __name__ == "__main__":
    header = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'}
    f = open('o.csv', 'w', encoding='utf-8', newline="")
    csv_writer = csv.writer(f)
    csv_writer.writerow(["name", "money", "address", "experience", "company", "industry"])
    n = 1
    while True:
        url = f'https://www.lagou.com/wn/jobs?kd=Java&pn={n}'
        page_text = requests.get(url=url, headers=header).text
        ex1 = '<div class="p-top__1F7CL"><a>(.*?)</a>'  # name and address
        list1 = re.findall(ex1, page_text, re.S)
        list2 = [i.split('<!-- -->') for i in list1]
        list_n = []
        list_a = []
        for j in list2:
            list_n.append(j[0])  # name
            list_a.append(j[1])  # address
        ex2 = '<span class="money__3Lkgq">(.*?)</span>' #sarary
        list3 = re.findall(ex2, page_text, re.S)
        ex3 = '<span class="money__3Lkgq">.*?</span>(.*?)</div>' #experience
        list4 = re.findall(ex3, page_text, re.S)
        ex4 = '<div class="company__2EsC8">.*?<a>(.*?)</a>' #company
        list5 = re.findall(ex4, page_text, re.S)
        ex5 = '<div class="industry__1HBkr">(.*?)</div>' #industry
        list6 = re.findall(ex5, page_text, re.S)
        for k in range(len(list3)):
            csv_writer.writerow([list_n[k], list3[k], list_a[k], list4[k], list5[k], list6[k]])

xpath:

import requests
import csv
from lxml import etree

f = open('occupation.csv', 'w', encoding='utf-8', newline="")
csv_writer = csv.writer(f)
list0 = ['work', 'place', 'company', 'wages', 'requirement', 'supplement']
csv_writer.writerow(list0)
n = 1
while True:
    url = f'https://www.lagou.com/wn/jobs?kd=Java&city=%E6%B7%B1%E5%9C%B3&pn={n}'
    cookie = {'cookie':'RECOMMEND_TIP=true; user_trace_token=20211122000852-14b360f5-5a8f-4359-aac5-4304dcc00c35; LGUID=20211122000852-005c8aea-72e8-4370-a960-cba9e1a43518; _ga=GA1.2.961423054.1637510993; _gid=GA1.2.1637177660.1637510993; __lg_stoken__=5c5efbd39a9f3064625278971f1c8706b4907ecca606db0b3991a9e06cf6d6deeeed3f92a1344d561324ac315ec2d78d5793caa01094712e1a72bd00f9cf8dd9058777ed8083; JSESSIONID=ABAAAECABIEACCAFEF3ACD546D7F610141580641C4DE095; WEBTJ-ID=20211122125957-17d4603db71a6e-086ec99c421dc7-978183a-1327104-17d4603db731051; sensorsdata2015session=%7B%7D; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1637510993,1637557198; X_MIDDLE_TOKEN=03cdce33d58439b4b144252f4934b27a; LG_HAS_LOGIN=1; hasDeliver=0; privacyPolicyPopup=false; showExpriedIndex=1; showExpriedCompanyHome=1; showExpriedMyPublish=1; __SAFETY_CLOSE_TIME__23139559=1; index_location_city=%E5%85%A8%E5%9B%BD; gate_login_token=66304c52e8459a63b1ebacb075c957e241a61ae525770fddbea3d7fa8d464abb; LG_LOGIN_USER_ID=f54c78249219d7a962839b01843fa8f83c9a45c66d2129cabf8ceca3c635cc48; _putrc=D765D3AB6C13BCDD123F89F2B170EADC; login=true; unick=%E7%94%A8%E6%88%B70046; TG-TRACK-CODE=jobs_code; PRE_UTM=; PRE_HOST=; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Fzhaopin%2FJava%2F%3FlabelWords%3Dlabel; LGSID=20211123194240-ee4914ae-a5e0-4b31-967a-9c4fe6210f34; PRE_SITE=; _gat=1; SEARCH_ID=e08ae935a8a640c2968a962ac4cc7746; X_HTTP_TOKEN=92de061c76fc4105587766736136d0b70811362a5d; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1637667847; LGRID=20211123194305-8666e237-0227-4fd8-b4f2-cd33ce3b16eb; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2223139559%22%2C%22first_id%22%3A%2217d4342d431c8-0f8b05834d8f89-978183a-1327104-17d4342d4321277%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24os%22%3A%22Windows%22%2C%22%24browser%22%3A%22Chrome%22%2C%22%24browser_version%22%3A%2296.0.4664.45%22%2C%22lagou_company_id%22%3A%22%22%7D%2C%22%24device_id%22%3A%2217d4342d431c8-0f8b05834d8f89-978183a-1327104-17d4342d4321277%22%7D'}
    header = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',
        'referer': 'https://www.lagou.com/wn/jobs?kd=Java&city=%E5%85%A8%E5%9B%BD',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
    }
    page_text = requests.get(url=url, headers=header, cookies=cookie).text
    tree = etree.HTML(page_text)
    list_xpath =tree.xpath('//div/div/div/div/div/div/div/div/div/div/a/text()')
    list2_xpath =tree.xpath('//div/div/div/div/div/div/div/div/div/div/div/span/text()')
    list3_xpath =tree.xpath('//div/div/div/div/div/div/div/div/div/div/div/text()')
    list3 = list3_xpath[14::2]
    list4 = list3_xpath[15::2]
    list2 = list2_xpath[1::2]
    for m in range(len(list2)):
        while list_xpath != []:
            list1 = []
            for i in range(3):
                list1.append(list_xpath[0])
                list_xpath.pop(0)
            list1.append(list2[m])
            list1.append(list3[m])
            list1.append(list4[m])
            csv_writer.writerow(list1)
    print(f"The first{n}Page crawling succeeded")
    n += 1

It is worth noting that in xpath

    list3 = list3_xpath[14::2]
    list4 = list3_xpath[15::2]
    list2 = list2_xpath[1::2]

This is because in the tags located by xpath, the content we need starts from 14 / 15 and repeats twice, so we have the above processing.

The above 22 questions are what I have done since I started learning for more than a month.

Tags: Python Back-end

Posted on Mon, 29 Nov 2021 16:47:34 -0500 by evilgenius