0

New to Python

Path = "C:/Users/Kailash/Downloads/Results_For_Stride-Table.csv"
f = open(Path, "r")  # as infile: #open("C:/Users/Kailash/Downloads/25ft_output.csv", "w") as outfile:
counter = 0
n = 0
distance = float
print("n =", n)
while True:
    counter += 1
    #print("Counter = ", counter)
    lines = f.readline()
    Stride_Length = lines.split(", ")
    # if (distance > 762):
    #    Delta_t = distance
    #    print("Pre-Delta_t = ", Delta_t)
    distance[n] += float(Stride_Length[3])
    #n += 1
    if distance > 762:
        Delta_t = 762 - distance[n - 1]
        print("Delta_t = ", Delta_t)
        Residual_distance = distance - 762
        print("Residual_distance = ", Residual_distance)
        counter = 0
        distance = Residual_distance
        print("Counter & Distance RESET!")
    print(distance)

I am getting a TypeError: 'type' object is not subscriptable in line 15:

distance[n] += float(Stride_Length[3])

Any idea why am I seeing this?

Ghost Ops
  • 1,710
  • 2
  • 13
  • 23
  • 1
    you may want to start with `distance = float(0)` ? then, what the n loop part should do with distance ? – PRMoureu Jul 08 '17 at 00:22
  • this question is answered: take a look at [this](https://stackoverflow.com/questions/26920955/typeerror-type-object-is-not-subscriptable-when-indexing-in-to-a-dictionary) link. – sam-pyt Jul 08 '17 at 00:26
  • @PRMoureu: I tried replacing distance = float with float(0) but that did not solve. 'n' has to increment everytime. Sorry the # has to be removed. – Kailash Mallikarjun Jul 08 '17 at 00:27
  • you want to use distance as a dict, a list or a number ? for a number, you have to remove all `[n]` – PRMoureu Jul 08 '17 at 00:33
  • @PRMoureu: distance is just a variable name I am using. It can be changed to anything I want. I want to use distance as a float number. I am copying float(Stride_Length[3] into distance[0]. – Kailash Mallikarjun Jul 08 '17 at 00:36
  • @9.0: I tried looking into that page and the problem there was solved by reorganizing the statements. But my problem is something different is what I feel. Thanks! – Kailash Mallikarjun Jul 08 '17 at 00:47
  • Your error arises out of this line `distance = float`. It is not clear what you are trying to do here, especially what you then expect `distance[n]` to do. You say you want `distance` to be a float, but what do you expect to happen when you index a float? – juanpa.arrivillaga Jul 08 '17 at 01:19
  • @juanpa.arrivillaga: You are right. The error starts from there. I am trying to achieve this: - Read the column 3 of every line from a text file and add sum them all. - Once the sum goes beyond 762, I should go into "if" statement and do the needful. While doing this, I should be able to access the sum value just before going beyond 762 which is distance[-1]. I am having error while trying to access the previous value. – Kailash Mallikarjun Jul 10 '17 at 21:51
  • @KailashMallikarjun well, that isn't what `distance[-1]` is doing. Variables don't magically keep track of the previous value they held. You have to do that yourself. Again, I have no idea what you meant to do with `distance = float` or what you think `distance[n]` would do. You should read the answers here already, which go into detail. – juanpa.arrivillaga Jul 10 '17 at 21:54
  • @juanpa.arrivillaga: I was getting an error saying int cannot be added to list, so I looked up few Qs here and added distance = float(0) to make distance as floating point variable. I want to know how to get the previous held value. I am having a newbie problems here trying to figure it out. – Kailash Mallikarjun Jul 10 '17 at 22:00
  • Like I said, you can't get the previously held value from a variable. You need to keep track of that previous value in another variable. – juanpa.arrivillaga Jul 10 '17 at 22:02
  • @juanpa.arrivillaga: Thanks man, I got it! – Kailash Mallikarjun Jul 11 '17 at 16:34
  • Does this answer your question? [TypeError: 'type' object is not subscriptable when indexing in to a dictionary](https://stackoverflow.com/questions/26920955/typeerror-type-object-is-not-subscriptable-when-indexing-in-to-a-dictionary) – b4hand Feb 02 '21 at 19:55

1 Answers1

0

For me I go the same error once I try to access model class attributes like if it was dictionary dimension['dimension_id'] though dimension type is app.models.evaluation.DimensionsEvaluation object so I got the error

TypeError: 'DimensionsEvaluation' object is not subscriptable

to fix this I had to get the dictionary field from the model class using .__dict__ and access my attribute normally like above:

...
for dimension in evaluation_dimensions:
    print(f"\n dimension_id: {dimension['dimension_id']}\n") #TypeError: 'DimensionsEvaluation' object is not subscriptable


for dimension in evaluation_dimensions:
    dimension_dic = dimension.__dict__
    print(f"\n dimension_id: {dimension_dic['dimension_id']}\n") #dimension_id: 1
...

.


Complete code:

async def getAssessments(client_id: UUID, db: Session = Depends(get_async_session)):
    evaluation_id_data = await getEvaluationId(client_id, db)
    evaluation_id = evaluation_id_data['data']
    evaluation_dimensions_data = await getEvluationDimensions(evaluation_id, db)
    evaluation_dimensions = evaluation_dimensions_data['data']

    assessment = []
    for dimension in evaluation_dimensions:
        print(f"\n\n dimension {dimension} \n\n")
        print(f"\n\n dimension dict {dimension.__dict__} \n\n")

the result is

dimension <app.models.evaluation.DimensionsEvaluation object at 0x10d8def50> 

 dimension dict {'_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at 0x10d8def90>, 'selected': True, 'dimension_id': 2, 'evaluation_id': 6, 'id': 16, 'questions': '[152,153]'} 
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74