Home > Software engineering >  Making two-dimensional list with the data given
Making two-dimensional list with the data given

Time:01-20

name_list=['William','Laura','Robert','Alicia','Sharon','Jack','Mary','Edward','Jessie','Debra']
day_list=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']

hormones_list=[name_list,day_list]

print(hormones_list[0][0])

enter image description here

I wrote the one-dimensional ones. But how can I write the 2d? When I write hormone_list[0][0] it has to show william's monday data. But I don't know how. Also how can I temporarily store seperate lists? Should I use if structures without writing them one by one? It says shortest so I'm a bit cautious.

CodePudding user response:

You can nest lists, so instead of [value, value, value] you'd use [list, list, list].

So the values from monday-sunday would be a list and then you'd add all these list in a row.

Like:

hormone_list = [[1,2,3,4,5,6,7],[2,3,4,5,6,7,8], ...]

CodePudding user response:

This is likely not how I would solve the problem if presented as a business case but the answer seems to ask for lists and comprehensions so I might do:

## ----------------------------
## Our data
## ----------------------------
name_list=['William', 'Laura', 'Robert', 'Alicia', 'Sharon', 'Jack', 'Mary', 'Edward', 'Jessie', 'Debra']
day_list=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
hormones_list=[
    [1.0, 1.3, 1.7, 1.1, 1.8, 1.3, 1.2], # William's daily readings
    [1.9, 1.9, 1.5, 1.8, 1.9, 1.7, 1.5], # Laura's daily readings
    [3.0, 3.3, 3.1, 2.7, 3.5, 4.5, 4.7], # Robert's daily readings
    # ....
]
## ----------------------------

print(f"Williams reading on Monday was: {hormones_list[0][0]}")

## ----------------------------
## build the list of high days using a list of lists like hormones_list.
## Note: I would probably use a days_high dictionary with something like
## days_high.setdefault(patient_index, []).append(day_index)
## ----------------------------
days_high = [
    [
        day_index
        for day_index, day_reading
        in enumerate(patient_readings)
        if day_reading > 4.0
    ]
    for patient_readings
    in hormones_list
]
## ----------------------------

## ----------------------------
## Print out the warnings for a given patient (if there are any)
## ----------------------------
for patient_index, patient_readings in enumerate(days_high):
    if not patient_readings:
        continue
    patient_name = name_list[patient_index]
    days = " ".join([day_list[i] for i in patient_readings])
    print(f"WARNING: High values for {patient_name}: {days}")
## ----------------------------

giving the following output:

Williams reading on Monday was: 1.0
WARNING: High values for Robert: Saturday Sunday
  •  Tags:  
  • Related