TypeError: string indices must be integers, not str // working with dict [duplicate]

2024/11/19 1:55:18

I am trying to define a procedure, involved(courses, person), that takes as input a courses structure and a person and returns a Dictionary that describes all the courses the person is involved in.

Here is my involved(courses, person) function:

def involved(courses, person):for time1 in courses:for course in courses[time1]:for info in time1[course]:print info

Here is my dictionary:

courses = {'feb2012': { 'cs101': {'name': 'Building a Search Engine','teacher': 'Dave','assistant': 'Peter C.'},'cs373': {'name': 'Programming a Robotic Car','teacher': 'Sebastian','assistant': 'Andy'}},'apr2012': { 'cs101': {'name': 'Building a Search Engine','teacher': 'Dave','assistant': 'Sarah'},'cs212': {'name': 'The Design of Computer Programs','teacher': 'Peter N.','assistant': 'Andy','prereq': 'cs101'},'cs253': {'name': 'Web Application Engineering - Building a Blog','teacher': 'Steve','prereq': 'cs101'},'cs262': {'name': 'Programming Languages - Building a Web Browser','teacher': 'Wes','assistant': 'Peter C.','prereq': 'cs101'},'cs373': {'name': 'Programming a Robotic Car','teacher': 'Sebastian'},'cs387': {'name': 'Applied Cryptography','teacher': 'Dave'}},'jan2044': { 'cs001': {'name': 'Building a Quantum Holodeck','teacher': 'Dorina'},'cs003': {'name': 'Programming a Robotic Robotics Teacher','teacher': 'Jasper'},}}

When I'm trying to test my code:

>>>print involved(courses, 'Dave')

Python give me an error:

for info in time1[course]:
TypeError: string indices must be integers, not str

How can I fix that?

Thanks.

Answer

time1 is the key of the most outer dictionary, eg, feb2012. So then you're trying to index the string, but you can only do this with integers. I think what you wanted was:

for info in courses[time1][course]:

As you're going through each dictionary, you must add another nest.

https://en.xdnf.cn/q/26500.html

Related Q&A

Pandas: create dataframe from list of namedtuple

Im new to pandas, therefore perhaps Im asking a very stupid question. Normally initialization of data frame in pandas would be column-wise, where I put in dict with key of column names and values of li…

Closest equivalent of a factor variable in Python Pandas

What is the closest equivalent to an R Factor variable in Python pandas?

Temporarily Disabling Django Caching

How do you disable Django caching on a per checkout basis?Back before Django 1.3, I could disable caching for my local development checkout by specifying CACHE_BACKEND = None, in a settings_local.py i…

How to get a complete exception stack trace in Python

The following snippet:import tracebackdef a():b()def b():try:c()except:traceback.print_exc()def c():assert Falsea()Produces this output:Traceback (most recent call last):File "test.py", line …

python - should I use static methods or top-level functions

I come from a Java background and Im new to python. I have a couple scripts that share some helper functions unique to the application related to reading and writing files. Some functions associated …

Draw graph in NetworkX

Im trying to draw any graph in NetworkX, but get nothing, not even errors:import networkx as nx import matplotlib.pyplot as plt g1=nx.petersen_graph() nx.draw(g1)

Django 1.7 migrations wont recreate a dropped table, why?

Using Django 1.7 migrations.I accidentally dropped a table in my database. I assumed that by running migration again this would recreate the table but no, Django states "No migrations to apply&quo…

Reset ipython kernel

I was wondering if there is a way to restart the ipython kernel without closing it, like the kernel restart function that exists in the notebook. I tried %reset but that doesnt seem to clear the import…

What is the best way to remove a dictionary item by value in python? [duplicate]

This question already has answers here:Removing entries from a dictionary based on values(4 answers)Closed 4 years ago.I wonder if there is simple way to remove one or more dictionary element(s) from a…

How do I run a Python script on my web server?

Ive just started learning Python, and Im pretty lost right now. I want to run my script on my server that is hosted through hosting24.com. Their FAQ says they support Python, but I have no clue where t…