Форум сайта python.su
0
Вопрос такой: как создать двумерный массив, если изначально не знаю сколько элементов в каждом измерении массива?
Пример: есть группа Group, есть студенты Students.
arr={} Group = 1 arr[Group] = {} Students = 1 arr[Group][Students] = u'Иванов' Group = 1 arr[Group] = {} Students = 2 arr[Group][Students] = u'Петров' Group = 1 arr[Group] = {} Students = 3 arr[Group][Students] = u'Сидоров' for stud in arr[1]: print(arr[1][stud])
Офлайн
88
arr={} Group = 1 arr[Group] = {} Students = 1 arr[Group][Students] = u'Иванов' Group = 1 #arr[Group] = {} Students = 2 arr[Group][Students] = u'Петров' Group = 1 #arr[Group] = {} Students = 3 arr[Group][Students] = u'Сидоров' for stud in arr[1]: print(arr[1][stud])
arr={} Group = 1 arr[Group] = {} Students = 1 arr[Group][Students] = u'Иванов' Group = 2 arr[Group] = {} Students = 2 arr[Group][Students] = u'Петров' Group = 3 arr[Group] = {} Students = 3 arr[Group][Students] = u'Сидоров' for stud in arr[1]: print(arr[1][stud])
Отредактировано Shaman (Июль 4, 2015 00:42:45)
Офлайн
568
Не очень понятно, чего вы хотите сделать, но обычно для подобных конструкций используют defaultdict
#!/usr/bin/env python # -* coding: utf-8 -*- from collections import defaultdict d = defaultdict(list) d["Students"].append(u"Иванов") d["Students"].append(u"Петров") d["Groups"].append(u"НТ-981") d["Groups"].append(u"НТ-982") print d
Офлайн
0
Спасибо большое всем за ответы!
Офлайн