• 1
    Eingabe und Ausgabe
    • Aufgaben
  • 2
    Bedingungen
    • Aufgaben
  • 3
    For-Schleife
    • Aufgaben
  • 4
    Zeichenketten
    • Aufgaben
  • 5
    While-Schleife
    • Aufgaben
  • 6
    Listen
    • Aufgaben
  • 7
    2D-Arrays
    • Aufgaben
  • 8
    Wörterbücher
    • Aufgaben
  • 9
    Mengen
    • Aufgaben
  • 10
    Funktionen und Rekursion
    • Aufgaben
  • к

Lektion 4. Lines

Schwierigkeit:

Aufgabe«SEO-Optimierung»

💻 Python

Du hast einen Job als Junior-SEO-Spezialist in einer Internetagentur bekommen. Deine erste Aufgabe & mdash; Hilfe bei der Optimierung der Kundenwebsite. Dir wurde eine Liste mit Keywords zur Verfügung gestellt, aber sie ist sehr „schmutzig“: Es gibt viele Füllwörter (die sogenannten „Stoppwörter“), wie Konjunktionen, Präpositionen und allgemeine Anfragen wie „kaufen“ oder „Preis“, die für eine bestimmte Seite keine wesentliche semantische Last tragen.

rn

Deine Aufgabe & mdash; Schreibe ein Programm, das die ursprüngliche Zeile von diesen Stoppwörtern bereinigt und aus den verbleibenden „sauberen“ Keywords eine neue Zeile bildet, in der sie durch ein Komma getrennt aufgelistet werden. Dies hilft, sie später für Meta-Tags auf der Website zu verwenden.

rn

Technische Aufgabenstellung:

rn
    rn
  1. Das Programm muss eine Zeile entgegennehmen, die durch Leerzeichen getrennte Keywords enthält.
  2. rn
  3. Innerhalb des Programms muss eine Liste von Stoppwörtern fest vorgegeben sein (z. B.: „kaufen“, „Preis“, „wie viel“, „kosten“, „in“, „mit“, „auf“).
  4. rn
  5. Das Programm muss die Eingabezeile in einzelne Wörter aufteilen.
  6. rn
  7. Mit einer Schleife & nbsp; for soll das Programm alle Wörter durchgehen.
  8. rn
  9. Mit einer Bedingung & nbsp; if muss für jedes Wort überprüft werden, ob es ein Stoppwort ist. Die Überprüfung soll unabhängig von der Groß-/Kleinschreibung sein (d. h. „Kaufen“ und „kaufen“ & mdash; sind dasselbe Stoppwort).
  10. rn
  11. Wörter, die keine Stoppwörter sind, müssen in einer separaten Liste gesammelt werden.
  12. rn
  13. Am Ende muss das Programm eine Zeile ausgeben, die aus den sauberen Wörtern besteht, verbunden durch Komma und Leerzeichen (, ). Wenn keine sauberen Wörter übrig sind, soll das Programm eine leere Zeile ausgeben.
  14. rn

Eingabeformat

Zeile (Zeichenkette) mit Schlüsselwörtern, durch Leerzeichen getrennt.

Ausgabeformat

Zeichenkette (String) mit gefilterten Schlüsselwörtern, getrennt durch ein Komma und ein Leerzeichen.

Beispiel

Eingabe

Kaufen Sie ASUS Laptop Preis in Moskau

Ausgabe

Laptop, Asus, Moskau

Hinweis

methods for working with strings in python

Python provides many built-in methods for working with strings. These are powerful tools that make it easy to manipulate text data. All methods are called with a dot after the variable in which the string is stored, according to the variable scheme.method().

It is important to remember that strings in Python are immutable. This means that none of the methods changes the original string. Instead, they return a new string with the changes applied.

Basic methods for working with strings

1. upper() and lower()

Convert all characters of the string to upper or lower case, respectively.

string = "Hello, World!"
print(string.upper()) # Output: HELLO, WORLD!
print(string.lower()) # Output: hello, world!
print(string) # Output: Hello, World! (the original line has not changed)

A useful tip: These methods are indispensable when you need to compare case-insensitive strings. For example, when checking user input.

user_input = "Yes"
if user_input.lower() =="yes":
print("The user agrees.")

2. strip(), lstrip() and rstrip()

Remove whitespace characters (spaces, tabs, line breaks) from the beginning and end of the line. lstrip() deletes only on the left, and rstrip() deletes only on the right.

string = "  Hello, World!  "
print(string.strip()) # Output: Hello, World!
print(string.lstrip()) # Output: Hello, World!  
print(string.rstrip()) # Output: Hello, World!

A useful tip: These are extremely important methods for "cleaning" data obtained from files or from users, who often accidentally leave unnecessary spaces. As an argument, you can pass a string of characters that need to be deleted.

dirty_string = "!!!Hello, World!?! "
print(dirty_string.strip(" !?")) # Output: Hello, World

3. split() and join()

split() splits a string into a list of substrings using the specified separator. join() collects a string from the list items by inserting a separator string between them.

string = "Hello, World!"
words =string.split(", ")
print(words) # Output: ['Hello', 'World!']

# Reverse operation
joined_string = " | ".join(words)
print(joined_string) # Output: Hello | World!

A useful tip: split() is a standard method for parsing (parsing) data presented as a string, for example, CSV files. join() is the most efficient ("pythonic") way to assemble one large string from many small ones. It works much faster than adding strings in a loop using +.

4. find() and replace()

find() searches for the first occurrence of a substring and returns its index (position). If the substring is not found, returns -1. replace() replaces all occurrences of one substring with another.

string = "Hello, World!"
index = string.find("World")
print(index) # Output: 7

new_string = string.replace("World", "Python")
print(new_string) # Output: Hello, Python!

A useful tip: There is an analog of the find() index() method.. It does the same thing, but if the substring is not found, it raises the error ValueError. Use find() if the absence of a substring is a normal situation, and index(), if it should interrupt the execution of the program. The replace() method can also accept a third argument - the number of substitutions to be made.

5. startswith() and endswith()

They check whether the string begins or ends with a specific substring. Returns True or False.

string = "Hello, World!"
print(string.startswith("Hello")) # Output: True
print(string.endswith("World!"))   # Output: True
print(string.startswith("Bye"))    # Output: False

A useful tip: It is very convenient for quick checking of file formats (filename.endswith(".txt")) or link types (url.startswith("https")).

6. isdigit(), isalpha() and isalnum()

They check what the string consists of.

  • isdigit(): True if all characters are numbers.
  • isalpha(): True if all characters are letters.
  • isalnum(): True if all characters are letters or numbers.
string1 = "12345"
string2 = "Hello"
string3 = "Hello123"

print(f"'{string1}' consists of digits? {string1.isdigit()}")   # Output: True
print(f"'{string2}' consists of letters? {string2.isalpha()}")   # Output: True
print(f"'{string3}' consists of letters and numbers? {string3.isalnum()}") # Output: True
print(f"'{string2}' consists of digits? {string2.isdigit()}") # Output: False

A useful tip: These methods are ideal for validating user input before attempting to convert a string to a number or using it further in the program.

A complete guide to methods

Below is a table with a more complete list of string methods for quick reference.

Method Purpose
S.find(str, [start],[end]) Search for a substring in a string. Returns the number of the first occurrence or -1.
S.rfind(str, [start],[end]) Search for a substring in a string. Returns the number of the last occurrence or -1.
S.index(str, [start],[end]) Search for a substring in a string. Returns the number of the first occurrence or causes a ValueError.
S.rindex(str, [start],[end]) Search for a substring in a string. Returns the number of the last occurrence or causes a ValueError.
S.replace(template, replacement[, maxcount]) Replace the template with a replacement. maxcount limits the number of substitutions.
S.split(character) Splitting a line by separator.
S.isdigit() Whether the string consists of numbers.
S.isalpha() Whether the string consists of letters.
S.isalnum() Whether the string consists of numbers or letters.
S.islower() Whether the string consists of lowercase characters.
S.isupper() Whether the string consists of uppercase characters.
S.isspace() Whether the string consists of non-displayed characters (space, \f, \n, \r, \t, \v).
S.istitle() Whether words in a line begin with a capital letter.
S.upper() Converting a string to uppercase.
S.lower() Converting a string to lowercase.
S.startswith(str) Does the string S start with the template str.
S.endswith(str) Does line S end with the str template.
S.join(list) Assembling a line from a list separated by S.
ord(character) Converts a character to its ASCII/Unicode code.
chr(number) Converts the ASCII/Unicode code to a character.
S.capitalize() Converts the first character of the string to uppercase, and all the others to lowercase.
S.center(width, [fill]) Returns a centered string with the fill character at the edges (space by default).
S.count(str, [start],[end]) Returns the number of disjoint occurrences of a substring.
S.expandtabs([tabsize]) Replaces the tab characters \t with spaces.
S.lstrip([chars]) Deleting specified characters (or spaces) at the beginning of a line.
S.rstrip([chars]) Deleting specified characters (or spaces) at the end of a line.
S.strip([chars]) Deleting specified characters (or spaces) at the beginning and end of a line.
S.partition(template) Divides a string by the first occurrence of the separator into three parts: before, the separator itself, and after.
S.rpartition(sep) Divides the string by the last occurrence of the separator into three parts: before, the separator itself, and after.
S.swapcase() Converts lowercase characters to uppercase, and uppercase characters to lowercase.
S.title() Translates the first letter of each word to uppercase, and all the others to lowercase.
S.zfill(width) Adds zeros to the left of the string to the length width.
S.ljust(width, fillchar=" ") Aligns the line to the left, adding the characters fillchar to the right.
S.rjust(width, fillchar=" ") Aligns the line to the right, adding fillchar characters to the left.
S.format(*args, **kwargs) String formatting (a more modern alternative to %).
main.py
Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9
Test 10
Entwicklerlösung
# Список стоп-слов, которые нужно удалить. Он задан прямо в коде.
stop_words = ["купить", "цена", "сколько", "стоит", "в", "и", "с", "на"]

# Получаем строку с ключевыми словами от пользователя
keywords_input = input()

# Разделяем полученную строку на отдельные слова по пробелу. Результатом будет список слов.
words = keywords_input.split()

# Создаем пустой список, куда будем складывать "чистые" ключевые слова
clean_words = []

# Запускаем цикл, который перебирает каждое слово в списке 'words'
for word in words:
    # Приводим слово к нижнему регистру для корректного сравнения со списком stop_words
    # и проверяем, НЕ содержится ли оно в списке стоп-слов.
    if word.lower() not in stop_words:
        # Если слово не является стоп-словом, добавляем его в наш список 'clean_words'
        clean_words.append(word)

# Используем метод join, чтобы объединить все элементы списка 'clean_words' в одну строку.
# В качестве разделителя указываем ", " (запятая и пробел).
result = ", ".join(clean_words)

# Выводим итоговую строку на экран
print(result)

🎉 Glückwunsch! 🎉

Du hast die Aufgabe hervorragend gelöst! Es war schwierig, aber du hast die Lösung gefunden. Du bist dem Meister einen Schritt näher! Mach weiter so.

AD

Werbung

red-snake blue-snake green-snake

Führe deinen Code aus...

KI-Assistent

Hallo! Ich bin dein Programmier-Assistent. Stelle mir beliebige Fragen zu Python — ich erkläre Funktionen, Methoden und helfe dir bei der aktuellen Aufgabe!