简体   繁体   English

有没有办法为 python 中的数字邻居创建可能的对?

[英]Is there a way to create the possible pairs for number neighbors in python?

I'm attempting to create a new list with all the possible pairs in a list but only want to have the numbers that are neighbors be possible pairs.我正在尝试创建一个包含列表中所有可能对的新列表,但只想让相邻的数字成为可能的对。

For example, I have already created this list from a file:例如,我已经从一个文件创建了这个列表:

[1, 8, 10, 16, 19, 22, 27, 33, 36, 40, 47, 52, 56, 61, 63, 71, 72, 75, 81, 81, 84, 88, 96, 98, 103, 110, 113, 118, 124, 128, 129, 134, 134, 139, 148, 157, 157, 160, 162, 164] [1, 8, 10, 16, 19, 22, 27, 33, 36, 40, 47, 52, 56, 61, 63, 71, 72, 75, 81, 81, 84, 88, 96, 98, 103 , 110, 113, 118, 124, 128, 129, 134, 134, 139, 148, 157, 157, 160, 162, 164]

Im trying to create a list that outputs like this:我正在尝试创建一个输出如下的列表:

[(1,8), (8,10), (10,16), (16, 19), (19, 22), (22, 27), (27, 33), (33, 36), (36, 40), (40, 47), (47, 52), (52, 56), (56, 61), (61, 63), (63, 71), (71, 72), (72, 75), (75, 81), (81, 81), (81, 84), (84, 88), (88,96).... (162, 164)] [(1,8), (8,10), (10,16), (16, 19), (19, 22), (22, 27), (27, 33), (33, 36), ( 36, 40), (40, 47), (47, 52), (52, 56), (56, 61), (61, 63), (63, 71), (71, 72), (72, 75), (75, 81), (81, 81), (81, 84), (84, 88), (88,96) .... (162, 164)]

I was attempting to use the import itertools but that is giving be all the possible combinations not just the number neighbors.我试图使用 import itertools 但它给出了所有可能的组合,而不仅仅是数字邻居。

import itertools
for A, B in itertools.combinations(newl, 2):
            print(A, B)

Use zip() with a slice of the list offset by one place.使用zip()将列表的一部分偏移一个位置。

result = list(zip(newl, newl[1:]))

Just use this simple for loop:只需使用这个简单的 for 循环:

newl = [1, 8, 10, 16, 19, 22, 27, 33, 36, 40, 47, 52, 56, 61, 63, 71, 72, 75, 81, 81, 84, 88, 96, 98, 103, 110, 113, 118, 124, 128, 129, 134, 134, 139, 148, 157, 157, 160, 162, 164]

lst = []

for i in range(len(newl)):
    try:
        lst.append((newl[i], newl[i+1]))
    except:
        pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 python 中查找邻居的最有效方法 - Most efficient way to find neighbors of neighbors in python 如何在python中创建一个包含所有可能对的次数相等的列表序列? - how to create a list sequence in python containing all possible pairs an equal number of times? 尝试使用 Tkinter 在 python 中创建游戏扫雷,但在查找瓷砖周围的邻居数量时遇到问题 - Trying to create the game minesweeper in python using Tkinter but have a problem finding the number of neighbors around a tile 有没有更简洁的方法来从 python 列表中创建对? - Is there a more concise way to create pairs from a python list? python 中是否有任何方法可以使用键值对创建列表 - is there any way in python to create list using key value pairs 在双向网络中选择共享共同邻居的所有顶点对的有效方法 - Efficient way to select all pairs of vertices that share common neighbors in a bipartite network Python - 如何找到与其所有邻居相比的最大数字 - Python - How to find the maximum number comparing to all its neighbors 最快的算法可以选择数字对 - Fastest algorithm possible to pick number pairs 在原始列表中创建邻居和自身总和的 Python 代码 - Python Code to Create Sum of Neighbors and itself in the original list 如何在 Python 中创建列对? - How to create column pairs in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM