• Предмет: Информатика
  • Автор: novoskroman666
  • Вопрос задан 5 месяцев назад

Пожалуйста, срочно! 65 БАЛЛОВ
(Python code)

4. Write a Python program to get the smallest number from a list.
5. Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.
6. Write a Python program to remove duplicates from a list.
7. Write a Python program to check a list is empty or not.
8. Write a Python program to clone or copy a list.
9. Write a Python program to find the list of words that are longer than n from a given list of words.
10. Write a Python program that takes two lists and returns True if they have at least one common member.


novoskroman666: ЗАДАЧИ НА СЛОВАРИ И МЕТОДЫ К НИМ***

Ответы

Ответ дал: uzchikzagr
1

Ответ:

1. Получить наименьшее число из списка:

numbers = [5, 3, 8, 1, 9, 6]

print(min(numbers))

2. Подсчитайте количество строк, в которых длина строки равна 2 или более, а первый и последний символы совпадают:

strings = ["hi", "hello", "wow", "ab", "aa", "baa", "cab"]

count = 0

for string in strings:

if len(string) >= 2 and string[0] == string[-1]:

count += 1

print(count)

3. Удалить дубликаты из списка:

original_list = [1, 2, 3, 4, 1, 2, 3]

unique_list = list(set(original_list))

print(unique_list)

4. Проверьте, пуст список или нет:

my_list = []

if not my_list:

print("List is empty")

else:

print("List is not empty")

5. Клонируйте или копируйте список:

original_list = [1, 2, 3, 4, 5]

new_list = original_list.copy()

print(new_list)

6. Найдите список слов длиннее n из заданного списка слов:

words = ["hi", "hello", "wow", "ab", "aa", "baa", "cab"]

n = 2

long_words = [word for word in words if len(word) > n]

print(long_words)

7. Проверьте, есть ли у двух списков хотя бы один общий член:

list1 = [1, 2, 3, 4, 5]

list2 = [4, 5, 6, 7, 8]

common = set(list1) & set(list2)

if common:

print(True)

else:

print(False)

8. def clone_list(lst):

return lst[:]

original_list = [1, 2, 3, 4]

cloned_list = clone_list(original_list)

print(cloned_list) # [1, 2, 3, 4]

9. def find_long_words(words, n):

return [word for word in words if len(word) > n]

words = ['cat', 'dog', 'elephant', 'rabbit']

long_words = find_long_words(words, 4)

print(long_words) # ['elephant']

10. def common_member(list1, list2):

for elem in list1:

if elem in list2:

return True

return False

list1 = [1, 2, 3, 4, 5]

list2 = [4, 5, 6, 7, 8]

result = common_member(list1, list2)

print(result) # True


novoskroman666: Спасибо!!!
Вас заинтересует