我创建了一个简单的程序,使用in
方法至少两次从另一个列表中删除一个列表的元素:
def remove(l_list,s_list):
"""Removes items of s_list from l_list and returns the rest"""
res=[]
for item in l_list:
if item not in s_list:
res.append(item)
return res
我想用循环“while”替换if not in s_list
操作,并比较这些函数的复杂性。
所以,我做了以下代码(不起作用):
def remove2(l_list,s_list):
res=[]
for item in l_list:
found=False
i=0
while len(s_list)>i and not found:
if item==s_list[i]:
found=True
if not found:
res.append(item)
i+=1
return res
示例:
>>> remove2([1,2,3],[1,2])
[3]
>>> remove2([1,2,3],[1])
[2,3]
我做错了什么?我的逻辑出了什么问题?
答案 0 :(得分:2)
你应该在while循环后附加项目:
def remove2(l_list,s_list):
res=[]
for item in l_list:
found=False
i=0
while len(s_list)>i and not found:
if item==s_list[i]:
found=True
i+=1
if not found:
res.append(item)
return res
如果您使用found
,则可以简化此操作,而不是使用break
变量:
def remove2(l_list,s_list):
res=[]
for item in l_list:
i=0
while len(s_list)>i:
if item==s_list[i]:
break
i+=1
else:
# this else belongs to the while loop and is executed if and only if
# the loop wasn't terminated by "break".
res.append(item)
return res
答案 1 :(得分:0)
这里肯定存在一个逻辑缺陷:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.dropper</groupId>
<artifactId>dropper-web</artifactId>
<version>0.1</version>
<packaging>war</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- <dependency>
<groupId>dropper-web</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/postgresql-9.4.1212.jre6.jar</systemPath>
</dependency> -->
<dependency>
<groupId>dropper-web</groupId>
<artifactId>bootstrap</artifactId>
<version>1.0.10</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/bootstrap-1.0.10.jar</systemPath>
</dependency>
<dependency>
<groupId>dropper-web</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/commons-io-2.5.jar</systemPath>
</dependency>
<dependency>
<groupId>dropper-web</groupId>
<artifactId>cupertino</artifactId>
<version>1.0.10</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/cupertino-1.0.10.jar</systemPath>
</dependency>
<dependency>
<groupId>dropper-web</groupId>
<artifactId>primefaces-6.0</artifactId>
<version>6.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/primefaces-6.0.jar</systemPath>
</dependency>
</dependencies>
<build>
<finalName>dropper-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
发现 >> >> / p>
此外,您需要进行这项艰苦的检查吗?你可以轻松地&#34; :-)使用单行命令执行此操作。只有列表理解: 您也可以将它们变成集合,取出差异,然后转换回列表:function sellsGrapefruit(store) {
return store.foods.some(function(food) {
return food.name === 'grapefruit';
});
};
var grapefruitStores = stores.filter(sellsGrapefruit);
if item==s_list[i]:
found=True
if not found:
res.append(item)
i+=1