Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iteration of an array
#4
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"
Reply


Messages In This Thread
Iteration of an array - by PappaBear - May-22-2019, 11:11 PM
RE: Iteration of an array - by micseydel - May-23-2019, 12:13 AM
RE: Iteration of an array - by perfringo - May-23-2019, 05:34 AM
RE: Iteration of an array - by PappaBear - May-23-2019, 05:48 PM
RE: Iteration of an array - by Yoriz - May-23-2019, 06:14 PM
RE: Iteration of an array - by PappaBear - May-23-2019, 06:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Array Iteration/Merging Milo 4 2,909 Jun-13-2018, 02:42 PM
Last Post: Milo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020