Question

Use python to write a code Problem 1: Environmental Monitoring You are given a nested list...

Use python to write a code

Problem 1: Environmental Monitoring

You are given a nested list in which the sublists contain tuples!

Example:

samples = [ [('frog','H'), ('toad','D') ], [('trout','H'), ('salmon','S'), ('catfish','H')] ]

Here each species is either healthy H, dead D or sick S. Access the health status of a specified animal using multi-indexing.

Count how many are healthy using two nested for loops. List will be bigger and different on each version.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Code:

""" Python program for Environmental Monitoring """

# Dictionary that holds the health status of animals
samples = [ [('frog','H'), ('toad','D') ], [('trout','H'), ('salmon','S'), ('catfish','H')] ]

# Access the health status of a specified animal using multi-indexing
# Accessing health status of salmon
print("\n Health status of salmon: " + samples[1][1][1])


# Count how many are healthy using two nested for loops
# Variable that holds the count
cnt=0;

# Outer loop to iterate over sub lists
for i in range(len(samples)):
    # Inner for loop for iterating over each tuple in sublist
    for j in range(len(samples[i])):
        # Checking health status
        if samples[i][j][1] == 'H':
            cnt = cnt + 1

# Printing results
print(" As a total %d animals are healthy.. " %(cnt))
      


______________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
Use python to write a code Problem 1: Environmental Monitoring You are given a nested list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT