简体   繁体   English

如何根据每个元组中第一个数字的值从元组列表中弹出项目?

[英]How do I pop item off a list of tuples, based on the value of the first number in each tuple?

I am trying to create a list of tuples that consists of all the variations of a set of numbers.我正在尝试创建一个包含一组数字的所有变体的元组列表。 However I want to remove any variants from the list that are the same sequence but offset by a position or two.但是,我想从列表中删除任何序列相同但偏移一两个位置的变体。 For example: (-1,1,2) , (1,2,-1) & (2,-1,1) I would only want the first one.例如: (-1,1,2) , (1,2,-1) & (2,-1,1)我只想要第一个。

Here's where I'm up to:这是我要做的:

import itertools as it

list = [-1, 0, 1, 2]
cycles = []

list_cycle_3 = it.permutations(cycles, 3)
list_cycle_4 = it.permutations(cycles, 4)

for item in list_cycle_3:
    cycles.append(item)

for item in list_cycle_4:
    cycles.append(item)

print(cycles)

This results in:这导致:

[(-1, 0, 1), (-1, 0, 2), (-1, 1, 0), (-1, 1, 2), (-1, 2, 0), (-1, 2, 1), 
(0, -1, 1), (0, -1, 2), (0, 1, -1), (0, 1, 2), (0, 2, -1), (0, 2, 1), 
(1, -1, 0), (1, -1, 2), (1, 0, -1), (1, 0, 2), (1, 2, -1), (1, 2, 0), 
(2, -1, 0), (2, -1, 1), (2, 0, -1), (2, 0, 1), (2, 1, -1), (2, 1, 0), 
(-1, 0, 1, 2), (-1, 0, 2, 1), (-1, 1, 0, 2), (-1, 1, 2, 0), (-1, 2, 0, 1), (-1, 2, 1, 0), (0, -1, 1, 2), (0, -1, 2, 1), (0, 1, -1, 2), (0, 1, 2, -1), 
(0, 2, -1, 1), (0, 2, 1, -1), (1, -1, 0, 2), (1, -1, 2, 0), (1, 0, -1, 2), 
(1, 0, 2, -1), (1, 2, -1, 0), (1, 2, 0, -1), (2, -1, 0, 1), (2, -1, 1, 0), 
(2, 0, -1, 1), (2, 0, 1, -1), (2, 1, -1, 0), (2, 1, 0, -1)]

So what do I do next to filter the results so I only have the results I want, which are:那么接下来我要做什么来过滤结果,以便我只有我想要的结果,它们是:

[(-1, 0, 1), (-1, 0, 2), (-1, 1, 0), (-1, 1, 2), (-1, 2, 0), (-1, 2, 1), 
(0, 1, 2), (0, 2, 1), (-1, 0, 1, 2), (-1, 0, 2, 1), (-1, 1, 0, 2), 
(-1, 1, 2, 0), (-1, 2, 0, 1), (-1, 2, 1, 0)]

If it helps a simple difference between the lists are that the list I want is all the tuples starting with -1, and the tuples where there is no -1 starting with 0如果它有助于列表之间的简单区别是我想要的列表是所有以 -1 开头的元组,以及没有 -1 以 0 开头的元组

    l=[(-1, 0, 1), (-1, 0, 2), (-1, 1, 0), (-1, 1, 2), (-1, 2, 0), (-1, 2, 1),                                                                                           
    (0, -1, 1), (0, -1, 2), (0, 1, -1), (0, 1, 2), (0, 2, -1), (0, 2, 1),
    (1, -1, 0), (1, -1, 2), (1, 0, -1), (1, 0, 2), (1, 2, -1), (1, 2, 0),
    (2, -1, 0), (2, -1, 1), (2, 0, -1), (2, 0, 1), (2, 1, -1), (2, 1, 0),
    (-1, 0, 1, 2), (-1, 0, 2, 1), (-1, 1, 0, 2), (-1, 1, 2, 0), (-1, 2, 0, 1), (-1, 2, 1, 0), (0, -1, 1, 2), (0, -1, 2, 1), (0, 1, -1, 2), (0, 1, 2, -1),
    (0, 2, -1, 1), (0, 2, 1, -1), (1, -1, 0, 2), (1, -1, 2, 0), (1, 0, -1, 2),
    (1, 0, 2, -1), (1, 2, -1, 0), (1, 2, 0, -1), (2, -1, 0, 1), (2, -1, 1, 0),
    (2, 0, -1, 1), (2, 0, 1, -1), (2, 1, -1, 0), (2, 1, 0, -1)]

   l2=[]

   for i in l:
       if i[0] == -1 :
           l2.append(i)

   print(l2)

this code works此代码有效

output输出

[(-1, 0, 1), (-1, 0, 2), (-1, 1, 0), (-1, 1, 2), (-1, 2, 0), (-1, 2, 1), (-1, 0, 1, 2), (-1, 0, 2, 1), (-1, 1, 0, 2), (-1, 1, 2, 0), (-1, 2, 0, 1), (-1, 2, 1, 0)]

Start with a list that does not contain the number that you want to filter against:从不包含要过滤的数字的列表开始:

For example, you'll only need the ones that starts with 0. Then your list is例如,您只需要以 0 开头的那些。那么您的列表是

l = [-1, 1, 2]

Find all the two element permutatations and filter them as you like.找到所有的两个元素排列并根据需要过滤它们。 Ant then add 0 as the first element by mapping your result set. Ant 然后通过映射您的结果集添加 0 作为第一个元素。

Example:例子:

In [2]: from itertools import permutations
In [3]: l = [-1, 1, 2]
In [4]: p = permutations(l, 2)
In [5]: [(0, *t) for t in p]
Out[5]: [(0, -1, 1), (0, -1, 2), (0, 1, -1), (0, 1, 2), (0, 2, -1), (0, 2, 1)]

You could do a similar trick for, say, the ones that does not have -1 in them and start with 0 which is omit -1 and 0 from your list and then add 0 as the first element into your result sets items.你可以做一个类似的技巧,比如,那些没有 -1 并从 0 开始的,从你的列表中省略 -1 和 0,然后将 0 作为第一个元素添加到你的结果集项目中。

And do not override reserved keywords in your code:并且不要覆盖代码中的保留关键字:

list =  [...] # do not do that

暂无
暂无

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

相关问题 如何在 Python 中的元组列表中对每个元组中的第一个值求和? - How do I sum the first value in each tuple in a list of tuples in Python? 如何使用每个元组的第一个值作为键将六个元组列表连接到 Pandas 数据框中? - How do I join six list of tuples into a pandas dataframe using the first value of each tuple as the key? 给定一个元组列表,如果第一个元素相同,我如何在每个相似的元组中添加第二个元素 - Given a list of tuples, if the first element is the same, how do i add the second element inside each similar tuple 如何在 Python 中的元组列表中对每个元组中的前两个值求和? - How do I sum the first two values in each tuple in a list of tuples in Python? 如何将元组列表转换为 Pandas 数据框,以便每个元组的第一个值代表一列? - How can I transform a list of tuples into a pandas dataframe so that the first value of each tuple represents a column? 如何在python中修改每个列表/元组中的第一个值? - How do I modify the first value in each list / tuple in python? 如何用元组中每个元组的第一个值构成一个新元组? - How to form a new tuple with the first value of each tuple of a tuple of tuples? 将列表转换为元组列表,每个元组中的项目重复值 - Convert list to list of tuples with repeated value of item in each tuple 当每个项目也包含在另一个元组中时,如何从元组列表中获取第一项 - How to get the first items from a list of tuples when each item is also wrapped in another tuple 基于每个元组中的值的元组分区列表 - Partition list of tuples based on a value within each tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM