如何从列表中打印出差异为2的元素

时间:2019-02-03 17:19:34

标签: python python-3.x list element

假设我们有一个仅包含整数的列表:

list = [1, 7, 12, 14, 22, 24, 29, 31, 39, 45, 77, 79, 85, 100]

现在,我想从该列表中打印出所有差异为2的元素对。因此,我希望我的程序下一个打印出来:

12, 14

22, 24

29, 31

77, 79

我尝试了很多事情,但似乎找不到解决方法。

8 个答案:

答案 0 :(得分:3)

您可以使用itertools.combinations

stream = ['I love tweet 1', 'I loves kt',] # Your input steam if tweets

kv = {'love': 1,'tweet': 2} # Your key value matching pair

print ([x for x in stream if any(j in kv for j in x.split())]) # o/p prints only those stream where atleast a single match is present in stream kv.

# output: ['I love tweet 1']

输出

from itertools import combinations

lst = [1, 7, 12, 14, 22, 24, 29, 31, 39, 45, 77, 79, 85, 100]

result = [(x, y) for x, y in combinations(lst, r=2) if abs(x - y) == 2]

for first, second in result:
    print(first, second)

答案 1 :(得分:2)

简单的列表理解可以解决此问题:

l = [1, 7, 12, 14, 22, 24, 29, 31, 39, 45, 77, 79, 85, 100]
l1 = [(x1,x2) for x1 in l for x2 in l if (x1-x2 == 2)]
print(l1)
# [(14, 12), (24, 22), (31, 29), (79, 77)]

答案 2 :(得分:1)

希望下面的代码满足您的要求。

list1 = [1, 7, 12, 14, 22, 24, 29, 31, 39, 45, 77, 79, 85, 100]

for x in list1:
    if x+2 in list1:
        print(x,", ",x+2)

答案 3 :(得分:1)

如果您的列表已排序且没有重复的元素,则只能在三个连续元素的组中找到2的差值:

l = [1, 7, 12, 14, 22, 24, 29, 31, 39, 45, 77, 79, 85, 100]

l = sorted(set(l)) # if l is not sorted and has repeating elements

for a, b, c in zip(l, l[1:], l[2:]):
    if b - a == 2:
        print(a, b)
    elif c - a == 2:
        print(a, c)
# 12 14
# 22 24
# 29 31
# 77 79

答案 4 :(得分:0)

与其尝试直接遍历循环,不如尝试使用 integer 遍历循环,而使用hasNextLine

range(len(my_list))

输出:

my_list = [1, 7, 12, 14, 22, 24, 29, 31, 39, 45, 77, 79, 85, 100]
for i in range(len(my_list) - 1):
    if my_list[i+1] - my_list[i] == 2:
        print("%d, %d" % (my_list[i], my_list[i+1]))

答案 5 :(得分:0)

使用itertools.combinations

from itertools import combinations as c
mlst = [1, 7, 12, 14, 22, 24, 29, 31, 39, 45, 77, 79, 85, 100]
print ([(i,j) for i,j in c(mlst,2) if abs(i-j) == 2])

# output: [(12, 14), (22, 24), (29, 31), (77, 79)]

答案 6 :(得分:0)

您可以定义e方法,该方法根据条件返回连续对:

def each_cons(predicate, iterable):
  i, size = 0, len(iterable)
  while i < size-2:
    if predicate(iterable[i], iterable[i+1]):
      yield iterable[i:i+2]
    i += 1

然后将其设置为条件:

array = [1, 7, 12, 14, 22, 24, 29, 31, 39, 45, 77, 79, 85, 100]

each_cons(lambda x, y: y - x == 2, array)
#=>  [[12, 14], [22, 24], [29, 31], [77, 79]]

答案 7 :(得分:0)

@bobojmaj,您也可以这样尝试。

>>> 
>>> l = [1, 7, 12, 14, 22, 24, 29, 31, 39, 45, 77, 79, 85, 100]
>>> output = [(l[i], l[j]) for i in range(len(l) - 1) for j in range(i + 1, len(l)) if abs(l[i] - l[j]) == 2]
>>> output
[(12, 14), (22, 24), (29, 31), (77, 79)]
>>> 
>>> s = '\n'.join([str(output[i][0]) + ', ' + str(output[i][1]) for i in range(len(output))])
>>> print(s)
12, 14
22, 24
29, 31
77, 79
>>> 

如果您的元素位于示例2中,则为2组。

>>> 
>>> l = [1, 7, 12, 14, 22, 24, 29, 31, 39, 45, 77, 79, 85, 100]
>>> 
>>> n = len(l) - 1
>>> output = [(l[i], l[i + 1]) for i in range(0, n, 2) if abs(l[i] - l[i + 1]) == 2]
>>> output
[(12, 14), (22, 24), (29, 31), (77, 79)]
>>>