Iteration of an array - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Iteration of an array (/thread-18574.html) |
Iteration of an array - PappaBear - May-22-2019 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 ::: -- howeverI can't figure out how to extract "object2" and "object5" as strings...especially wehn there's potential multiple objectsstoring 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. RE: Iteration of an array - micseydel - May-23-2019 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. RE: Iteration of an array - perfringo - May-23-2019 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 givenTo 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' RE: Iteration of an array - PappaBear - May-23-2019 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" RE: Iteration of an array - Yoriz - May-23-2019 (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)
RE: Iteration of an array - PappaBear - May-23-2019 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! |