Posts: 21
Threads: 9
Joined: Apr 2017
May-22-2019, 11:11 PM
(This post was last modified: May-22-2019, 11:42 PM by Yoriz.)
I have an two-dimm array, with the "internal" array as a named tuple.
EG: I have
obj_list= []
my_Object = namedtuple('my_Object', 'name attribute1 attribute2') then throughout my program
obj_list.append(my_Object (name,attribute1 ,attribute1 ) ) however, when I try to unpack, I'm getting "lost" in how to do so
when I print
print (obj_list[2]))
print (obj_list[6])) I get :::
Output: ['list-name-a', ['ojbect2', 'object5']]
['list-name-f', ['ojbect1', 'object2' 'object4' 'object7']]
-- howeverI can't figure out how to extract "object2" and "object5" as strings...especially wehn there's potential multiple objects
storing the multi-dim array made total sense, unpacking (extracting) it -- has me scratching my head
thanks in advance,
Pappa Bear
p.s. not even sure what to google for this one.
Posts: 2,344
Threads: 62
Joined: Sep 2016
I don't know what you mean by "unpack" or "extract". Can you elaborate? If you have a variable length list, and you need to do something with its contents, a loop is the typical thing.
Posts: 1,936
Threads: 8
Joined: Jun 2018
I don't get it. One can't have different size of namedtuple object, there will be TypeError:
>>> Person = nametuple('Person', 'name gender age')
>>> Bob = Person('Bob', 'male', 22)
>>> Bob
Person(name='Bob', gender='male', age=22)
>>> Sam = Person('Sam', 'male', 20, 'student')
TypeError: __new__() takes 4 positional arguments but 5 were given To access values in list of namedtuples:
>>> Person = nametuple('Person', 'name gender age')
>>> data = [['Bob', 'male', 22], ['Alice', 'female', 21]]
>>> persons = [Person(*row) for row in data)
>>> persons
[Person(name='Bob', gender='male', age=22),
Person(name='Alice', gender='female', age=21)]
>>> [person.name for person in persons if person.gender == 'male']
['Bob']
>>> [person.name for person in persons if person.gender == 'male'][0]
'Bob'
>>> persons[0].name
'Bob'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 21
Threads: 9
Joined: Apr 2017
May-23-2019, 05:48 PM
(This post was last modified: May-23-2019, 05:48 PM by PappaBear.)
Sorry, here's some better formatting, and hopefully a better explanation:::
import time
import os
import collections
from collections import namedtuple
grp_list=[]
obj_list=[]
nt_object = namedtuple('nt_object', 'name att1 att2')
# This for loop is just to populate data into object list
for increment in range(1,7):
att1="nope"
att2="nope"
if increment % 2 ==0:
att1 ="div by 2"
if increment % 3 ==0:
att2 ="div by 3"
obj_list.append(nt_object("object-"+str(increment),att1,att2))
# obj_list now is 6 items --
# [ nt_object(name='object-1', att1='nope', att2='nope'),
# nt_object(name='object-2', att1='div by 2', att2='nope'),
# nt_object(name='object-3', att1='nope', att2='div by 3'),
# nt_object(name='object-4', att1='div by 2', att2='nope'),
# nt_object(name='object-5', att1='nope', att2='nope'),
# nt_object(name='object-6', att1='div by 2', att2='div by 3') ]
## because I can't append multiple items, I put them into a list
for increment in range(1,7):
if (increment % 2 ==0) and (increment %3 == 0):
grp_list.append(["group-"+str(increment),obj_list[0],obj_list[1],obj_list[2],obj_list[3], obj_list[4]])
elif increment % 2 ==0:
grp_list.append(["group-"+str(increment),obj_list[0],obj_list[2],obj_list[4]])
else:
grp_list.append(["group-"+str(increment),obj_list[1],obj_list[3],obj_list[5]])
# grp_list now is 6 items --
# [ ['group-1', nt_object(name='object-2', att1='div by 2', att2='nope'), nt_object(name='object-4', att1='div by 2', att2='nope'), nt_object(name='object-6', att1='div by 2', att2='div by 3')],
# ['group-2', nt_object(name='object-1', att1='nope', att2='nope'), nt_object(name='object-3', att1='nope', att2='div by 3'), nt_object(name='object-5', att1='nope', att2='nope')],
# ['group-3', nt_object(name='object-2', att1='div by 2', att2='nope'), nt_object(name='object-4', att1='div by 2', att2='nope'), nt_object(name='object-6', att1='div by 2', att2='div by 3')],
# ['group-4', nt_object(name='object-1', att1='nope', att2='nope'), nt_object(name='object-3', att1='nope', att2='div by 3'), nt_object(name='object-5', att1='nope', att2='nope')],
# ['group-5', nt_object(name='object-2', att1='div by 2', att2='nope'), nt_object(name='object-4', att1='div by 2', att2='nope'), nt_object(name='object-6', att1='div by 2', att2='div by 3')],
# ['group-6', nt_object(name='object-1', att1='nope', att2='nope'), nt_object(name='object-2', att1='div by 2', att2='nope'), nt_object(name='object-3', att1='nope', att2='div by 3'), nt_object(name='object-4', att1='div by 2', att2='nope'), nt_object(name='object-5', att1='nope', att2='nope')]]
## trying to figure out how to directly reference that
## group-5 has "object-2", "object-4" and "object-6"
## group-2 has "object-1", "object-3" and "object-5"
for z in range(len(grp_list)):
a=grp_list[z]
counter=0
print(a[0])
for increment in a:
if counter >0:
print (increment.name)
counter +=1
# This works, but I'm first having to extract it to a temp list
# I was hoping to be able reference it directly using "grp_list"
Posts: 2,164
Threads: 35
Joined: Sep 2016
(May-23-2019, 05:48 PM)PappaBear Wrote: ## trying to figure out how to directly reference that
## group-5 has "object-2", "object-4" and "object-6"
## group-2 has "object-1", "object-3" and "object-5"
print(grp_list[4][1].name, grp_list[4][2].name, grp_list[4][3].name)
print(grp_list[1][1].name, grp_list[1][2].name, grp_list[1][3].name) Output: object-2 object-4 object-6
object-1 object-3 object-5
Posts: 21
Threads: 9
Joined: Apr 2017
OMG!!!!! @ Yoriz that's exactly what I was looking for!! So elegant!
and I know as a beginner, my code is far from "elegant" but that is so much better than my for loop!
-- it makes sense [array1][array2] but a) didnt' know you could reference it like that; So, what is that called ? (the double brackets ) -- as in what should i have googled?
I do my best not to post here, and RTFM etc...but this one was kicking me in the teeth;
Huge thanks!
|