This is the 19th day of my participation in the Genwen Challenge

A multidimensional array resolves to a one-dimensional array

If you have the following array, parse it into a one-dimensional array:

[1[2[3[8888.Awesome!], [4.6[45.78[99]], [45.67.89[999[4543.90]]]]], 45]]
Copy the code

To:

[1.2.3.4.6.45.78.99.45.67.89.999.4543.90.45]
Copy the code

Here, we can easily solve the problem by thinking about it this way:

  1. If it is a list, further parsing is required
  2. If it’s not a list, put it directly into the result

Here, you see the shadow of recursion, yeah, recursion, very easy to understand the image

def flatten(sample) :
    if isinstance(sample, list):
        result = []
        for item in sample:
            result += flatten(item)
    else:
        return [sample]
    
    return result
Copy the code

Look at the results:

if __name__ == "__main__":
    sample = [1[2[3[8888.Awesome!], [4.6[45.78[99]], [45.67.89[999[4543.90]]]]], 45]]
    print flatten(sample)
# [1, 2, 3, 8888, 666, 4, 6, 45, 78, 99, 45, 67, 89, 999, 4543, 90, 45]
Copy the code

In this way, no matter how many dimensions of nesting, can be solved.

UTC time strings interconvert

Background development often needs to record time and return time stamps to the front end.

For generality, we typically store the UTC timestamp.

However, the timestamp field is usually inserted into the database as a formatted time string, for example:

insert into table_name(user_id, login_time) values ("RidingRoad", "2018-11-13 12:19:39");
Copy the code

The business scenario

If a user’s UTC time stamp is sent from the front end and the background needs to record it into the database, the front end will return the UTC time stamp when requested.

Business analysis

The state of this UTC timestamp changes:

  1. From the front end: double timestamp

  2. Insert into database: Formatted time string

  3. Returns the timestamp of type double to the front end

So we need a function that converts time to a formatted time string

Conversion to realize

import time

# UTC is converted to a formatted time string
def u2s(utc_timestamp) :
   return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(utc_timestamp))

Convert the formatted time string to UTC
def s2u(utc_time_string) :
   return time.mktime(time.strptime(utc_time_string, "%Y-%m-%d %H:%M:%S"))\
          - time.mktime(time.gmtime(0Example))if __name__ == "__main__":
   Get the current UTC timestamp
   timestamp = time.time()
   print(timestamp)

   Convert the UTC timestamp to a date-format string
   utc_time_string = u2s(timestamp)
   print(utc_time_string)

   Convert the date string back to the UTC timestamp
   utc_timestamp = s2u(utc_time_string)
   print(utc_timestamp)

   Convert the converted UTC timestamp to a date string
   # Look at the error, the number of seconds is the same, generally does not affect
   after_conver = u2s(utc_timestamp)
   print(after_conver)
Copy the code

Results output

The error between the two timestamps is at the level of zero millisecond, and the date format string before and after the timestamp is the same. Ordinary business can accept such an error. There’s a more precise way, let me know in the comments below.

1542112115.95
2018-11-13 12:28:35
1542112115.0
2018-11-13 12:28:35
Copy the code