day8 - string and collection jobs

  1. Use three sets to represent the names of students who choose courses in three disciplines (a student can choose multiple courses at the same time)
biology = {'stu1', 'stu3', 'stu5', 'stu6', 'stu8', 'stu10', 'stu11'}
chemistry = {'stu1', 'stu2', 'stu4', 'stu5', 'stu6', 'stu7', 'stu9'}
physics = {'stu3', 'stu4', 'stu5', 'stu7', 'stu10', 'stu11', 'stu12'}
  1. How many students are there in total
num = biology | chemistry | physics
print('The total number of students:', len(num))
  1. Find the number of people who only selected the first subject and their corresponding names
num2 = (biology | chemistry) - (chemistry | physics)
print(len(num2), num2)
  1. Find the number of students who have chosen only one subject and their corresponding names
num3 = (biology ^ chemistry ^ physics) - (biology & chemistry & physics)
print(len(num3), num3)
  1. Find the number of students who have chosen only two subjects and their corresponding names
num1 = biology | chemistry | physics
num3 = (biology ^ chemistry ^ physics) - (biology & chemistry & physics)
num5 = biology & chemistry & physics
num4 = num1 - num3 -num5 
print(len(num4), num4)
  1. Find the number of students who have selected three courses and their corresponding names
num = biology & chemistry & physics
print('Number of students:', len(num), num)
  1. Enter a string and print all characters in odd bits (subscripts are characters in bits 1, 3, 5, 7...)

For example, input 'abcd1234' and output 'bd24'

#Method 1
str1 = 'abcd1234 '
print(str1[1::2])

#Method 2
for index in range(len(str1)):
    if index % 2:
        print(str1[index], end='')
  1. Enter the user name and judge whether the user name is legal (the length of the user name is 6 ~ 10 characters)
#Method 1
user_name = input('enter one user name:')
if 6 < len(user_name) < 10 :
    print('The user name is legal')
else:
    print('Illegal user name')
#Method 2
result ='legitimate' if 6 < len(user_name) < 10 else 'wrongful'
print(result)
  1. Enter the user name to judge whether the user name is legal (the user name can only be composed of numbers and letters)

    For example, 'ABC' - Legal '123' - Legal 'abc123a' - Legal

user_name = '54slos'
for x in user_name:
    if not('0' <= x <= '9' or 'a' <= x <= 'z' or 'A' <= x <= 'Z'):
        print('Illegal user name')
        break
else:
    print('The user name is legal')
  1. Enter the user name and judge whether the user name is legal (the user name must contain and can only contain numbers and letters, and the first character must be capital letters)

    For example, 'ABC' - illegal 'Mabc' - illegal '123' - illegal 'abc123' - illegal 'Abc123ahs' - Legal

user_name = 'A2njn2ssks'
if 'A' <= user_name[0] <= 'Z':
    count = 0
   # Judge whether the following characters are numbers and letters
    for x in user_name[1:]:
        if not('0' <= x <= '9' or 'a' <= x <= 'z' or 'A' <= x <= 'Z'):
            print('wrongful')
            break
        # Judge whether it is a number. If it is a number, add 1
        elif:
            if '0' <= x <= '9':
                count += 1
     #Judge the number of count statistics           
    else:
        if count:
            print('legitimate')
        else:
            print('wrongful')
else:
    print('wrongful')
  1. Enter a string and take out all the numeric characters in the string to produce a new string

For example: input * * 'abc1shj23kls99+2kkk' * * output: '123992'

st1 = 'abc1shj23kls99+2kkk'
new_str = ''
for x in st1:
    if '0' <= x <= '9':
        new_str += x
print(new_str)
  1. Input a string and change all lowercase letters in the string into corresponding uppercase letters for output (implemented by upper method and self writing algorithm)

    For example: input * * 'A2H2KLM12 +' * * output 'A2H2KLM12 +'

str1 = 'a2h2klm12+'
new_str = ''
for x in str1:
    if 'a' <= x <= 'z':
        new_str += chr(ord(x)-32)
    else:
        new_str += x
print(new_str)
  1. Enter a number less than 1000 to generate the corresponding student number

    For example: input * *'23 ', output' py1901023 '* * input * *'9', output 'py1901009' * * input * *'123 ', output' py1901123 '**

#Method 1
num = input('Please enter a number less than 1000:')
s1 = 'python1901'
new_str = s1 + (3-len(num)) * '0' + num
print(new_str)
#Method 2
s1 = 'python1901000'
print(s1[:-(len(num)+1)] + num)
  1. Enter a string to count the number of non alphanumeric characters in the string

    For example: input * * 'anc2+93-sj nonsense' * * output: 4 input * * '= =' * * output: 3

str1 = 'anc2+93-sj nonsense'
count = 0
for x in str1:
    if not ('0' <= x <= '9' or 'a' <= x <= 'z' or 'A' <= x <= 'Z'):
        count += 1
print(count)
  1. Enter a string, change the beginning and end of the string to '+' to produce a new string

    For example: input string * * 'abc123', output '+ bc12 +'**

str1 = 'abc123'
print('+' + str1[1:-1] + '+')
  1. Enter a string to get the middle character of the string

    For example: input * * 'abc1234' * * output: '1' input * * 'abc123' * * output * * 'c1'**

str1 = 'abc1234l80'
len1 = len(str1) // 2
if len(str1) % 2:
    print(str1[len1])
else:
    print(str1[len1-1:len1+1])
  1. The writer implements the function of the string function find/index (get the position of the first occurrence of string 2 in string 1)

    For example, string 1 is: how are you? Im fine, Thank you! , String 2 is: you, print 8

str1 = 'how are you? Im fine, Thank you!'
str2 = 'you'
for idenx in range(len(str1) - len(str2)+1):  #  You just need to get - 3 here
    if str1[idenx: idenx+len(str2)] == str2:
        print(idenx)
        break
else: 
    print('Can't find')  # for loop else structure
  1. Gets the common characters in two strings

    For example, string 1 is: abc123, string 2 is: huak3, print: common characters are: a3

#Method 1
str1 = 'abc123'
str2 = 'huak3'
new_str = ''
result = set(str1) & set(str2)
for x in result:
    new_str += x
print(new_str)
#Methods 2 in and not in
new_str = ''
for x in str1:
    if x in str2 and x not in new_str:
        new_str += x
print(new_str)

#Method 3 join
result = ''.join(set(str1) & set(str2))
print(result)

‚Äč

Tags: Python

Posted on Wed, 20 Oct 2021 22:52:24 -0400 by madonnazz