Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate a list based on daily temperature in Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to generate a list according to the daily temperature in Python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Today's Python tutorial: generate a list according to the daily temperature, which can also be regarded as a hands-on training, partners can practice together!

According to the daily temperature list, please generate a new list, and the input for the corresponding position is how much longer you need to wait before the temperature will rise beyond the number of days on that day. If it does not rise later, replace it with 0 at that location.

For example, given a list temperatures = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].

Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.

For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].

Hint: the range of temperature list length is [1, 30000]. The value of each temperature is Fahrenheit, an integer in the range of [30,100].

Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].

Ideas for solving the problem:

The easiest thing to think of and understand is violent exhaustion. Two pointers, one pointer pointing to the current temperature, the second pointer traversing backwards to find the nearest temperature higher than the current temperature, and recording the index difference between the two pointers. It can be said that the efficiency is very low.

Another method is to traverse the original array in reverse stack order and store the index with a higher temperature, which is also easy to understand, and the time complexity is O (n). The implementation logic is as follows:

The original array: [73, 74, 75, 71, 69, 72, 76, 73], pointer I traverses forward from the end and returns the array res= [0be0j0j0j0j0re0] traverses for the first time: I = 7, T [I] = 73, stack = [] stack is empty, index 7 with res [7] = 0,73 enters the stack. Stack = [7] the second traversal: I = 6, T [I] = 76, stack = [7] the temperature of index 7 at the top of the stack T [7] = 76, 76 > 73, index 7 leaves the stack, when the stack is empty, res [6] = 0. Index 6 enters the stack, stack = [6] traverses for the third time: I = 5, T [I] = 72, stack = [6] corresponds to the temperature of index 6, T [6] = 76, 7271, and the elements at the top of the stack are off the stack. Stack = [6, 5] the temperature of the index at the top of the stack is T [5] = 72, 75 > 72, and the elements at the top of the stack are out of the stack. Stack = [6] the temperature of the corresponding index at the top of the stack T [6] = 76 index 75 = 0; iMuk -) {while (! stack.isEmpty () & & T [stack.peek ()] = 0; iMel -) {while (index > = 0 & & T [index] = 0) res [I] = stack [index]-I; Stack [+ + index] = I;} return res;}

Python:

Python does not have the data structure of queues and stacks, because arrays can do operations such as first-in, first-out, LIFO, etc.

Class Solution: def dailyTemperatures (self, T: List [int])-> List [int]: tLen = len (T) stack = [] res = [0] * tLen for i in range (tLen-1,-1,-1): while stack and T [I] > = T [stack [- 1]]: stack.pop () if stack: res [I] = stack [- 1]-I stack.append (I) return res about how to generate a list in Python based on daily temperature I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report