Example of using python to calculate the number of adjacent data, and using python to calculate the number of adjacent data

Source: Internet
Author: User

Example of using python to calculate the number of adjacent data, and using python to calculate the number of adjacent data

Preface

This article describes how to use python to calculate the number of adjacent data and share it with you for your reference. I will not talk about it here. Let's take a look at the details:

What is an adjacent number?

For example, 5 is the number of adjacent numbers 4 and 6, and 5 is the number of 1 in a row.

Requirements:

Traverse all numbers in inputList, retrieve all numbers, and determine whether there are adjacent numbers. Non-adjacent numbers and numbers are added to outputList in the form of an array, in addition, the first digit in each "array" is incremented by two digits, and the last digit is incremented by two digits. Each digit cannot be less than 0 or greater than 400.

(Note: In inputList, "" is an adjacent number. It is regarded as a group and must be added to outputList as an array of [10, 11, 12, 13, 14, 15, "3" has no adjacent numbers and is also considered as a group. It must be added to the outputList in the form of an array [1, 2, 3, 4, 5)

Input:

  inputList = [0, 3, 5, 6, 7, 9, 12, 13, 15, 16, 17, 19, 20, 21, 22, 25, 27, 29, 30, 32, 33, 36, 39, 40, 43, 44, 46, 47, 48, 53, 54, 57, 58, 60, 62, 64, 65, 66, 67, 72, 74, 75, 76, 77, 78, 80, 82, 84, 85, 86, 89, 95, 96, 97, 98, 103, 104, 107, 108, 110, 111, 114, 116, 117, 118,   120, 121, 122, 124, 127, 132, 135, 137, 138, 139, 140, 145, 146, 148, 149, 150, 151, 155, 156, 160, 161, 166, 167, 170, 171, 172, 175, 178, 179, 180, 181, 182, 183, 184, 186, 188, 189, 190, 193, 195, 196, 198, 202, 205, 208, 210, 211, 213, 214, 215, 217,   221, 226, 227, 228, 233, 234, 235, 240, 241, 246, 247, 249, 255, 257, 258, 261, 262, 263, 267, 268, 269, 270, 271, 272, 275, 278, 280, 282, 283, 284, 286, 287, 289, 291, 292, 295, 296, 298, 300, 302, 303, 304, 305, 306, 310, 315, 317, 319, 320, 321, 322,   323, 324, 325, 326, 328, 331, 336, 339, 341, 342, 344, 346, 349, 354, 355, 356, 362, 363, 365, 366, 367, 368, 371, 374, 376, 378, 382, 383, 388, 390, 393, 396, 399]

Output:

OutputList = [[0, 1, 2], [1, 2, 3, 4, 5], [3, 4, 5, 6, 7, 8, 9], [7, 8, 9, 10, 11], [10, 11, 12, 13, 14, 15], ...... omitted here]

So, how can we solve this problem?

1. Set a value pointing to index = 0, start_index = 0

2. Initialize an Intermediate List median = [] and a save result list result_l = []

3. The for loop starts. start_index points to the beginning of each adjacent number.

4. Compare the difference between the value pointed to by the index and the value pointed to after the index. If the step is not 1, start_index will be moved to this value.

5. Round Robin to obtain the adjacent list

6. Use the map function to insert two adjacent numbers before and after each adjacent list.

7. Remove adjacent numbers that do not meet the conditions through list resolution.

Sample Code

#! /Usr/bin/python3 _ author _ = 'beimenchuixue '_ blog _ = 'HTTP: // www.cnblogs.com/2bjiujiu/'def go_cha_ru (new_l ): "insert two adjacent numbers to the front and back of the list, and remove numbers smaller than 0 and greater than 400 through list Parsing" "new_l.insert (0, new_l [0]-1) new_l.insert (0, new_l [0]-1) new_l.append (new_l [len (new_l)-1] + 1) new_l.append (new_l [len (new_l)-1] + 1) return [I for I in new_l if 0 <= I <= 400] def go_xiang_lin (raw_l ): "Get adjacent numbers" start_index = 0 result_l = [] median = [] # index starting from start_index to final for raw_index in range (len (raw_l )): # determine whether the for loop is directed to the specified position if start_index = raw_index: # index = 0 while True: # Start value of the pointer to start_value = raw_l [start_index] # if the Pointer Points to the last position, start value = last value if start_index = len (raw_l)-1: end_value = start_value else: # The last value = initial value + location parameter value end_value = raw_l [start_index + index] # whether the initial value + location parameter value is equal to the last value, determine whether it is an adjacent number. if yes, add it to the intermediate list if start_value + index = end_value: median. append (end_value) # position parameter + 1 index + = 1 else: # if not, the initial Pointer Points to the moving position parameter unit start_index + = index # Add the adjacent numbers of each primary node to the result list result_l.append (median) median = [] break # through high-level functions, return map (go_cha_ru, result_l) if _ name _ = '_ main _': input_list = [0, 3, 5, 6, 7, 9, 12, 13, 15, 16, 17, 19, 20, 21, 22, 25, 27, 29, 30, 32, 33, 36, 39, 40, 43, 44, 46, 47, 48, 53, 54, 57, 58, 60, 62, 64, 65, 66, 67, 72, 74, 75, 76, 77, 78, 80, 82, 84, 85, 86, 89, 95, 96, 97, 98,103,104,107,108,110,111,114,116,117,118,120,121,122,124,127,132,135,137,138,139,140,145,146,148,149,150,151,155,156,160,161,166,167,170,171,172,175,178,179,180,181,182,183,184,186,188,189,190,193,195,196,198,202,205,208,210,211,213,214,215,217,221,226,227,228,233,234,235,240,241,246,247,249,255,257,258,261,262,263,267,268,269,270,271,272,275,278,280,282,283,284,286,287,289,291,292,295,296,298,300,302,303,304,305,306,310,315,317,319,320,321,322,323,324,325,326,328,331,336,339,341,342,344,346,349,354,355,356,362,363,365,366,367,368,371,374,376,378,382,383,388,390,393,396,399] # result output_list = list (go_xiang_lin (input_list )) print (output_list)

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.