Найти - Пользователи
Полная версия: Помощь со словарями
Начало » Центр помощи » Помощь со словарями
1
CrusaderOfPlague
Всем привет!
Всех приветствую.
Есть у меня задание, в котором требуется составить второй словарь из элементов первого словаря. Причём во втором словаре длина тестовых значений должна быть меньше средней. Прикреплю файл, чтобы было понимание с чего был написан код к первому словарю.

Кто может помочь с написанием кода для второго и третьего словарей?

Ссылка на файл с заданием: https://docs.google.com/document/d/1XRSvyyiQedlP820VfjfNPADeNOX8miVs/edit?usp=sharing&ouid=114892183426046153478&rtpof=true&sd=true

Код к первому словарю:
 import csv
f=open('countries.csv')
r=csv.reader(f)
d={}
for row in r:
    l=row[0].split(';')
    d.update({l[0]:l[1]})
print(d)
xam1816
 import csv
#
def csv_to_dict(file):
    reader = csv.DictReader(open(file), delimiter=';')
    out = {}
    for i in reader:
        out[i['Страна']] = i['Столица']
    return out
#
def get_average_len(words):
    len_words = []
    for i in words:
        len_words.append(len(i))
    out = sum(len_words)//len(len_words)
    return out
#
dict_1 = csv_to_dict('Страны.txt')
print(dict_1)
#
average_len = get_average_len(dict_1.values())
#
dict_2 = dict(filter(lambda i: len(i[1]) < average_len, dict_1.items()))
#
print(dict_2)
#
dict_3 = dict(sorted(dict_2.items(), key=lambda i: i[1])[:10])
#
print(dict_3)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB