Posts: 21
Threads: 9
Joined: Apr 2017
I'm trying to figure out how to declare a 2d array (rows & columns); where the rows are dynamic
What I've been able to do is define number of rows, and columns are dynamic; which is the opposite of what I'm wanting...
I am looking for a way to have dynamic rows, with set columns...
row1 [value-a, value-b, value-c]
row2 [value-a, value-b, value-c]
.
.
rowX [value-a, value-b, value-c]
but the below code seems to make it 46 rows, columns dynamic (when I set range to 5, I get 5 rows, not 5 columns)
Thoughts on how to make the "rows" variable/dynamic?
import time
import os
import collections
howmany=6
rulenumber=0
my_2d_list =[[] for i in range(46)]
for counter1 in range(howmany): ## number of fw policies
for counter2 in (range(howmany)):
rulenumber +=1
print (rulenumber)
if (rulenumber % 2 == 0):
if (rulenumber % 3 == 0):
my_2d_list[rulenumber]=['even','div by 2','div by 3']
else:
my_2d_list[rulenumber]=['even','div by 2','not by 3']
else:
if (rulenumber % 3 == 0):
my_2d_list[rulenumber]=['odd','not by 2','div by 3']
else:
my_2d_list[rulenumber]=['odd','not by 2','not by 3']
print ("Security policy 1")
print ("Print Row 1")
print (my_2d_list[1])
print ("-----------------")
print (" ")
print ("Print Row 19")
print (my_2d_list[19])
print ("-----------------")
print (" ")
print ("Print Row 36")
print (my_2d_list[36]) Output: Print Row 1
['odd', 'not by 2', 'not by 3', 'extra', 'extra']
-----------------
Print Row 19
['odd', 'not by 2', 'not by 3', 'extra', 'extra']
-----------------
Print Row 36
['even', 'div by 2', 'div by 3']
Many thanks,
PappaBear
Posts: 817
Threads: 1
Joined: Mar 2018
May-03-2019, 12:38 AM
(This post was last modified: May-03-2019, 12:40 AM by scidam.)
Why are you using indexing approach to update the list? I would solve this task using .append method, e.g.
howmany = 6
rulenumber = 0
my_2d_list = [[]]
for rulenumber in range(1, howmany * howmany + 1):
print(rulenumber)
if (rulenumber % 2 == 0):
my_2d_list[-1].append('even')
else:
my_2d_list[-1].append('odd')
if (rulenumber % 3 == 0):
my_2d_list[-1].append('div by 3')
else:
my_2d_list[-1].append('not by 3')
my_2d_list.append(list()) Note: Term 'even' already implies that the number is dividable by 2, so we don't need to append 'div by 2', is it so?
Posts: 21
Threads: 9
Joined: Apr 2017
I figured this out... basically in the first array, you insert the additional array
import time
import os
import collections
howmany=6
rulenumber=0
my_2d_list =[]
for counter1 in range(howmany):
for counter2 in (range(howmany)):
rulenumber +=1
print (rulenumber)
if (rulenumber % 2 == 0):
if (rulenumber % 3 == 0):
my_2d_list.append(['even','div by 2','div by 3'])
else:
my_2d_list.append(['even','div by 2','not by 3'])
else:
iseven = 'odd'
if (rulenumber % 3 == 0):
my_2d_list.append(['odd','not by 2','div by 3'])
else:
my_2d_list.append(['odd','not by 2','not by 3'])
print (my_2d_list[0])
print ("-----------------")
print (" ")
print (my_2d_list[1])
print ("-----------------")
print (" ")
print (my_2d_list[5])
print ("-----------------")
print (" ")
print (my_2d_list[35])
print ("-----------------")
print (" ")
print (len(my_2d_list)) Output: ['odd', 'not by 2', 'not by 3']
-----------------
['even', 'div by 2', 'not by 3']
-----------------
['even', 'div by 2', 'div by 3']
-----------------
['even', 'div by 2', 'div by 3']
-----------------
36
Posts: 8,160
Threads: 160
Joined: Sep 2016
May-03-2019, 03:47 AM
(This post was last modified: May-03-2019, 08:54 AM by buran.)
I see a bit of confusion.
First of all - you don't need to declare anything in advance. Maybe you mean to ininitialize?
Also I am confused given your other thread and discussion there
now we talk just about built-in types
2-dim array would be list of list, list of tuples. Each sub-list/tuple would be row. the elements of the sub-list/tuple would be columns, e.g.
implementation with list of lists and implementation with list of namedtuples
import random
from collections import namedtuple
from pprint import pprint
num_rules = random.randint(1, 10) # simulate reading between 1 and 10 rules from a file
rules = [] # your 2 dim array
for row_num in range(1, num_rules + 1):
# as far as I undersatnd you would read row data from a file
row = [f'rule{row_num}', f'src-intf{row_num}', f'src-zone{row_num}',
f'dst-intf{row_num}', f'dst-zone{row_num}', f'allowed{row_num}',
f'protocol{row_num}', f'port{row_num}']
rules.append(row)
pprint(rules, compact=True)
print()
for rule in rules:
print(f'rule: {rule[0]}, protocol: {rule[-2]}, port: {rule[-1]}')
print()
print('-'*30)
num_rules = random.randint(1, 10) # simulate reading between 1 and 10 rules from a file
rules = [] # your 2 dim array
Rule = namedtuple('Rule', field_names=['rule', 'src_intf', 'src_zone', 'dst_intf', 'dst_zone', 'allowed', 'protocol', 'port'])
for row_num in range(1, num_rules + 1):
# as far as I undersatnd you would read row data from a file
rule = Rule(f'rule{row_num}', f'src-intf{row_num}', f'src-zone{row_num}',
f'dst-intf{row_num}', f'dst-zone{row_num}', f'allowed{row_num}',
f'protocol{row_num}', f'port{row_num}')
rules.append(rule)
pprint(rules, compact=True)
print()
for rule in rules:
print(f'rule: {rule.rule}, protocol: {rule.protocol}, port: {rule.port}') Output: [['rule1', 'src-intf1', 'src-zone1', 'dst-intf1', 'dst-zone1', 'allowed1', 'protocol1', 'port1'],
['rule2', 'src-intf2', 'src-zone2', 'dst-intf2', 'dst-zone2', 'allowed2', 'protocol2', 'port2'],
['rule3', 'src-intf3', 'src-zone3', 'dst-intf3', 'dst-zone3', 'allowed3', 'protocol3', 'port3'],
['rule4', 'src-intf4', 'src-zone4', 'dst-intf4', 'dst-zone4', 'allowed4', 'protocol4', 'port4'],
['rule5', 'src-intf5', 'src-zone5', 'dst-intf5', 'dst-zone5', 'allowed5', 'protocol5', 'port5'],
['rule6', 'src-intf6', 'src-zone6', 'dst-intf6', 'dst-zone6', 'allowed6', 'protocol6', 'port6'],
['rule7', 'src-intf7', 'src-zone7', 'dst-intf7', 'dst-zone7', 'allowed7', 'protocol7', 'port7'],
['rule8', 'src-intf8', 'src-zone8', 'dst-intf8', 'dst-zone8', 'allowed8', 'protocol8', 'port8'],
['rule9', 'src-intf9', 'src-zone9', 'dst-intf9', 'dst-zone9', 'allowed9', 'protocol9', 'port9'],
['rule10', 'src-intf10', 'src-zone10', 'dst-intf10', 'dst-zone10', 'allowed10', 'protocol10', 'port10']]
rule: rule1, protocol: protocol1, port: port1
rule: rule2, protocol: protocol2, port: port2
rule: rule3, protocol: protocol3, port: port3
rule: rule4, protocol: protocol4, port: port4
rule: rule5, protocol: protocol5, port: port5
rule: rule6, protocol: protocol6, port: port6
rule: rule7, protocol: protocol7, port: port7
rule: rule8, protocol: protocol8, port: port8
rule: rule9, protocol: protocol9, port: port9
rule: rule10, protocol: protocol10, port: port10
------------------------------
[Rule(rule='rule1', src_intf='src-intf1', src_zone='src-zone1', dst_intf='dst-intf1', dst_zone='dst-zone1', allowed='allowed1', protocol='protocol1', port='port1'),
Rule(rule='rule2', src_intf='src-intf2', src_zone='src-zone2', dst_intf='dst-intf2', dst_zone='dst-zone2', allowed='allowed2', protocol='protocol2', port='port2'),
Rule(rule='rule3', src_intf='src-intf3', src_zone='src-zone3', dst_intf='dst-intf3', dst_zone='dst-zone3', allowed='allowed3', protocol='protocol3', port='port3'),
Rule(rule='rule4', src_intf='src-intf4', src_zone='src-zone4', dst_intf='dst-intf4', dst_zone='dst-zone4', allowed='allowed4', protocol='protocol4', port='port4'),
Rule(rule='rule5', src_intf='src-intf5', src_zone='src-zone5', dst_intf='dst-intf5', dst_zone='dst-zone5', allowed='allowed5', protocol='protocol5', port='port5'),
Rule(rule='rule6', src_intf='src-intf6', src_zone='src-zone6', dst_intf='dst-intf6', dst_zone='dst-zone6', allowed='allowed6', protocol='protocol6', port='port6')]
rule: rule1, protocol: protocol1, port: port1
rule: rule2, protocol: protocol2, port: port2
rule: rule3, protocol: protocol3, port: port3
rule: rule4, protocol: protocol4, port: port4
rule: rule5, protocol: protocol5, port: port5
rule: rule6, protocol: protocol6, port: port6
|