Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Better than an array
#1
Using Linux Mint 18.2 and Python 3.5.2 as before I'm old school basic.
Need help with this array type problem.
tia
oldcity

 #!/usr/bin/python3
 #AT&T      
 #FP&L     
 #WATER       
 #GAS    
 #PLAN-D      
 #AARP      
 #DIAZ     
 #DRUGS     
 #HOUSE       
 #CAR
 #MISC
 lcnt = 0
 with open('COL-HDRS-Sel4', 'r') as catgs:
     for line in catgs:
         lcnt+=1
         dfiles = line.strip(",")
         dcatagory = dfiles.rstrip("\n")
         mcatg[lcnt] = dcatagory





 catg = 5  # catg value to be assigned later
 print ("\n")
 print(mcatg[catg])
 print("bye ")
Reply
#2
What is your question / problem?
Reply
#3
You can use enumerate as a counter:
    for lcnt, line in enumerate(catgs, 1) :
        dfiles = line.strip(",")
        dcatagory = dfiles.rstrip("\n")
        mcatg[lcnt] = dcatagory
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
As mentioned earlier my lifetime programming language was Basic.
I ran a test of your suggestion and it gives an error message I have
no idea how to deal with.
Any suggestion most helpful.
tia
oldcity

 #!/usr/bin/python3
 dcatagory = ''
 lcnt = 0
 with open('COL-HDRS-Sel4', 'r') as catgs:
     for lcnt, line in enumerate(catgs, 1) :
         dfiles = line.strip(",")
         dcatagory = dfiles.rstrip("\n")
         mcatg[lcnt] = dcatagory





 catg = 5  # catg value to be assigned later
 print ("\n")
 print(mcatg[catg])
 print("bye ")
 
Code produces this error message.
 @oldcity ~/MyPython $ python3 fix-here
 Traceback (most recent call last):
   File "fix-here", line 8, in <module>
     mcatg[lcnt] = dcatagory
 @oldcity ~/MyPython $ 
Reply
#5
It's a name error. You have not defined mcatg before that point, so you can't index it. If this is meant to be a list, you should define it as mcatg = [], and use append: mcatg.append(dcategory). If it is meant to be a dictionary you would define it as mcatg = {}, but then you don't need to make any other changes to the code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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