我在Python中有两个列表,如下所示:
temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']
Run Code Online (Sandbox Code Playgroud)
我需要创建第三个列表,其中包含第一个列表中不存在于第二个列表中的项目.从我必须得到的例子:
temp3 = ['Three', 'Four']
Run Code Online (Sandbox Code Playgroud)
有没有循环和检查的快速方法?
例如,我有两个列表
A = [6, 7, 8, 9, 10, 11, 12]
subset_of_A = [6, 9, 12]; # the subset of A
the result should be [7, 8, 10, 11]; the remaining elements
Run Code Online (Sandbox Code Playgroud)
在python中是否有内置函数来执行此操作?
我只是在寻找计算两个列表中的差异的方法时阅读了另一个用户问题.
我的问题是我为什么要这样做
def diff(a,b):
b = set(b)
return [aa for aa in a if aa not in b]
Run Code Online (Sandbox Code Playgroud)
而不是做
def diff(a,b):
tmp = []
for i in a:
if(i not in b):
tmp.append(i)
return tmp
Run Code Online (Sandbox Code Playgroud)
编辑:刚注意到第二个diff函数实际上返回了相似之处.现在应该是正确的.
我正在开发一个程序的一部分,将陈述变成问题。
当我尝试删除它时x
,它会返回None
。我希望它打印删除该项目的句子,我做错了什么?
def Ask(Question):
Auxiliary = ("will", "might", "would", "do", "were", "are", "did")
for x in Auxiliary:
if x in Question:
Question_l = Question.lower()
Question_tk_l = word_tokenize(Question)
Aux_Rem = Question_tk_l.remove(x)
print (Aux_Rem)
Run Code Online (Sandbox Code Playgroud)
想要的行为示例:
"what we are doing in the woods"
Run Code Online (Sandbox Code Playgroud)
应该成为
"what we doing in the woods"
Run Code Online (Sandbox Code Playgroud)
我想从问题中删除任何辅助词。
说我有一个清单:
main_list = ['bacon', 'cheese', 'milk', 'cake', 'tomato']
Run Code Online (Sandbox Code Playgroud)
和另一个清单:
second_list = ['cheese', 'tomato']
Run Code Online (Sandbox Code Playgroud)
我想从主列表中删除第二个列表中找到的所有元素?
先感谢您
亚当
所以我想知道如何使用Python 2.7,最有效地获取用于表示这样的索引的值列表:(但长度最多为250,000+)
indices = [2, 4, 5]
Run Code Online (Sandbox Code Playgroud)
并从更大的列表中删除索引列表,如下所示:(3,000,000多项)
numbers = [2, 6, 12, 20, 24, 40, 42, 51]
Run Code Online (Sandbox Code Playgroud)
得到这样的结果:
[2, 6, 20, 42, 51]
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种有效的解决方案.我知道有很多方法可以做到这一点,但这不是我的问题.效率是.此外,此操作必须多次完成,列表将以指数方式变小.我没有一个等式来表示它们随着时间的推移会变小多少.
编辑:
数字必须在整个时间内保持在列表中排序,或者在删除索引后返回到排序.名为indices的列表可以排序也可以不排序.它甚至不必在列表中.
我有3个列表,我想找到第1 /第2和第2 /第3之间的差异并打印它们.
这是我的代码:
n1 = [1,1,1,2,3]
n2 = [1,1,1,2] # Here the 3 is not found ("3" is not present in n1 at all)
n3 = [1,1,2] # here 1 is not found (there are fewer "1"s in n3 than in n2)
for x in n1:
if x not in n2:
print x
for m in n2:
if m not in n3:
print m
Run Code Online (Sandbox Code Playgroud)
但我得到的输出只有3.
如何使它输出1和3?我也尝试使用set
s,但它只是3
再次打印.
我正在寻找一种方法,可以让我在比较 2 个列表时检查缺少哪些元素。很像在这个线程中,但我想用 NumPy Python 编写它。
import numpy as np
numbers = np.array([1,2,3,4,5,6,7,8,9])
A = np.array([2,5,6,9])
def listComplementElements(list1, list2):
storeResults = []
for i in list1:
for j in list2:
if i != j:
#write to storeResults if 2 numbers are different
storeResults.append(i)
else:
#if numebrs are equal break out of the loop
break
return storeResults
result = listComplementElements(numbers, A)
print(result) #expected result [1,3,4,7,8]
Run Code Online (Sandbox Code Playgroud)
目前输出如下所示: [1, 1, 1, 1, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6, …
我有类似的东西:
a = [4,3,1,6,3,5,3]
b = [4,2,6]
Run Code Online (Sandbox Code Playgroud)
我想从a中删除b的3个元素.我试图这样做:
c = a - b
Run Code Online (Sandbox Code Playgroud)
但我认为merge(+
)的逆可能不是一个东西,并且是正确的: 不支持的操作数类型 - :list和list.我正在考虑绕过它们,但这并不是真的听起来像pythony.
我的最终状态将是: c = [3,1,3,5,3]
如果您没有注意到,则b不是a的子集,并且这些是无序的.2个不同的集合,这些集合也不是唯一的,但只想删除每个实例i in b
,而不是所有实例i in b
编辑似乎目前的答案并没有解决我的问题.
a = [1,1,2,2,2,3,4,5]
b = [1,3]
c = [x for x in a if x not in b]
#c result is [2,2,2,4,5]
Run Code Online (Sandbox Code Playgroud)
我希望c返回: [1,2,2,2,4,5]
为了速度打字,我只是把排序的数字放在一起,但列表仍然没有排序,但为了清洁起见,我们可以对它们进行排序.
它们只是在init处未分类. 由于列表项中有重复项,因此我无法根据set的定义使用集合.
我有 list1= [com.x1 ,com.x2 com.x3] ,我还有一个列表 list2= [com.x3] 。
我需要另一个列表,如 list3=[com.x1 , com.x2 ],如 list1-list2。
请帮助我。
我有两个清单:
listOne = ['John', 'James', Daniel', 'Peter', 'Luke']
listTwo = ['Daniel', 'Peter', Kate', 'Jenny']
Run Code Online (Sandbox Code Playgroud)
我想比较这两个列表并返回不匹配的内容,如果需要,请将其保存到另一个列表,因此输出应该是:
Non-Matches: 'Kate', 'Jenny'
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现这个目标?我考虑过将列表更改为集合,但没能让它正常工作
python ×11
list ×6
python-2.7 ×3
compare ×2
python-3.x ×2
complement ×1
filter ×1
indexing ×1
numpy ×1
performance ×1
set ×1
string ×1