简体   繁体   中英

Python: How to refer to the same index value position in the two lists?

A total beginner here hi! I have two lists:

month=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

and

temperature=[-3.5, -4.5, -1.0, 4.0, 10.0, 15.0, 18.0, 16.0, 11.5, 6.0, 2.0, -1.5]

And my task is to get an output that says for example

"The temperature in January was -3.5 degrees".

How do I get that result by defining the variable selectedMonth? No idea how to proceed!

I am able to define the index value to the first list, and the second seperately and via that get the result, but if I want to do it by just one index value (eg for January) for both lists?

Use the zip builtin.

>>> month=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
>>> temperature=[-3.5, -4.5, -1.0, 4.0, 10.0, 15.0, 18.0, 16.0, 11.5, 6.0, 2.0, -1.5]
>>> 
>>> for m, t in zip(month, temperature):
...     print('The temperature in {} was {} degrees.'.format(m, t))
... 
The temperature in January was -3.5 degrees.
The temperature in February was -4.5 degrees.
The temperature in March was -1.0 degrees.
The temperature in April was 4.0 degrees.
The temperature in May was 10.0 degrees.
The temperature in June was 15.0 degrees.
The temperature in July was 18.0 degrees.
The temperature in August was 16.0 degrees.
The temperature in September was 11.5 degrees.
The temperature in October was 6.0 degrees.
The temperature in November was 2.0 degrees.
The temperature in December was -1.5 degrees.

This works because zip gives you an (iterator over) the following list:

>>> list(zip(month, temperature))
[('January', -3.5), ('February', -4.5), ('March', -1.0), ('April', 4.0), ('May', 10.0), ('June', 15.0), ('July', 18.0), ('August', 16.0), ('September', 11.5), ('October', 6.0), ('November', 2.0), ('December', -1.5)]

I think you could do it like this:

month=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
temperature=[-3.5, -4.5, -1.0, 4.0, 10.0, 15.0, 18.0, 16.0, 11.5, 6.0, 2.0, -1.5]

data = dict(zip(month, temperature))

# Usage:
selectedMonth = "October"
print("The temperature in", selectedMonth, "was", data[selectedMonth], "degrees")

You can also do it without using any zip by just using index . month.index(selectedMonth) will return the index of the selectedMonth which will be 0 for 'January' . You will then have, temperature[0] which will give you the required temperature value.

selectedMonth = 'January'
print("The temperature in", selectedMonth, "was", temperature[month.index(selectedMonth)], "degrees")

> The temperature in January was -3.5 degrees

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM