Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Python中的列表列表中删除元素_Python_List - Fatal编程技术网

从Python中的列表列表中删除元素

从Python中的列表列表中删除元素,python,list,Python,List,我如何才能从列表列表中删除每个列表,其中“5”放在“3”之前,并给出如下列表的初始列表 [('3', '3', '3'), ('3', '3', '5'), ('3', '5', '3'), ('3', '5', '5'), ('5', '3', '3'), ('5', '3', '5'), ('5', '5', '3'), ('5', '5', '5')] 我试过了 for i in list_ck: for j in range(0,2): if (i[j]=='5' a

我如何才能从列表列表中删除每个列表,其中“5”放在“3”之前,并给出如下列表的初始列表

[('3', '3', '3'), ('3', '3', '5'), ('3', '5', '3'), ('3', '5', '5'), ('5', '3', '3'), ('5', '3', '5'), ('5', '5', '3'), ('5', '5', '5')]
我试过了

for i in list_ck:
   for j in range(0,2):
      if (i[j]=='5' and i[j+1]=='3'):
         list_ck.remove(i)

但是它不起作用

如果是字符串,那么测试5是否出现在3之前比测试元组更容易;因此,我们可以使用
'.join
将它们转换为字符串

数据=[('3','3','3','5'),('3','5','3'),('3','5','5'),('5','3','3'),('5','3','5'),('5','5','3'),('5','5','5')] >>>[r代表数据中的r,如果“53”不在“”。则加入(r)] [('3', '3', '3'), ('3', '3', '5'), ('3', '5', '5'), ('5', '5', '5')] 这假设您只想在3之前测试5,而不适用于元组中的字符串本身可能是
'53'
的更一般情况。但这对于你的例子来说已经足够了

一个更通用的解决方案是使用正则表达式,并在一个字符(如
)上进行连接,其中任何字符串都不包含:

数据=[('5','3','1'),('53','1','1'),('5','1','3')] >>>进口稀土 >>>pattern=re.compile(“(^ |,)5,(.*)*3(,|$)” >>>[r代表数据中的r,如果不是模式。搜索(','。连接(r))] [('53', '1', '1')]
在这里,模式
(^ |,)5,(.+,)*3(,|$)
在开头或后面匹配一个5,后跟一个逗号,后跟以逗号结尾的任意数量的事物,后跟一个逗号之前或字符串末尾的3。

您可以使用条件列表理解:

# List of Tuples (lot).
list_of_tups = [('3', '3', '3'), ('3', '3', '5'), ('3', '5', '3'), ('3', '5', '5'), ('5', '3', '3'), ('5', '3', '5'), ('5', '5', '3'), ('5', '5', '5')]

>>> [tup for tup in list_of_tups 
     if not any((a == '5' and b == '3') for a, b in zip(tup, tup[1:]))]
[('3', '3', '3'), ('3', '3', '5'), ('3', '5', '5'), ('5', '5', '5')]
要就地修改列表而不是创建新列表,请创建需要删除的项目的索引,然后按相反顺序将其弹出

idx = [n for n, tup in enumerate(list_of_tups) 
       if any((a == '5' and b == '3') for a, b in zip(tup, tup[1:]))]
for i in reversed(idx):
    list_of_tups.pop(i)

>>> list_of_tups
[('3', '3', '3'), ('3', '3', '5'), ('3', '5', '5'), ('5', '5', '5')]

尝试创建另一个具有相同参数的列表(不要复制它们,创建新的),并从第二个列表中删除()

tab = [('3', '3', '3'), ('3', '3', '5'), ('3', '5', '3'), ('3', '5', '5'), ('5', '3', '3'), ('5', '3', '5'), ('5', '5', '3'), ('5', '5', '5')]    
tab2 = [('3', '3', '3'), ('3', '3', '5'), ('3', '5', '3'), ('3', '5', '5'), ('5', '3', '3'), ('5', '3', '5'), ('5', '5', '3'), ('5', '5', '5')]
for i in tab:
    for j in range(0,2):
        if i[j]=='5' and i[j+1]=='3':
            tab2.remove(i)
            break

欢迎来到StackOverflow。看见在您发布MRE代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您指定的问题。由于未定义变量,给定代码无法运行;“不工作”不是问题规范。什么是
ck
?您的代码中没有定义该变量。很抱歉,我指的是列表。删除。。。我更正了错误不要在代码中重新定义
列表。这是一个内置的。我认为你犯了在迭代时删除DWI的错误。看到了,谢谢,是的,我只有单曲“5”和“3”谢谢,这很有效。我是python新手