是否有内置功能可以从Python中的列表中删除重复项,同时保留顺序?我知道我可以使用一个集来删除重复项,但这会破坏原始顺序.我也知道我可以像这样滚动自己:
def uniq(input):
output = []
for x in input:
if x not in output:
output.append(x)
return output
Run Code Online (Sandbox Code Playgroud)
但是如果可能的话,我想利用内置或更多的Pythonic习语.
我知道如何获得两个平面列表的交集:
b1 = [1,2,3,4,5,9,11,15]
b2 = [4,5,6,7,8]
b3 = [val for val in b1 if val in b2]
Run Code Online (Sandbox Code Playgroud)
要么
def intersect(a, b):
return list(set(a) & set(b))
print intersect(b1, b2)
Run Code Online (Sandbox Code Playgroud)
但是当我必须找到嵌套列表的交集时,我的问题就开始了:
c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
Run Code Online (Sandbox Code Playgroud)
最后我想收到:
c3 = [[13,32],[7,13,28],[1,6]]
Run Code Online (Sandbox Code Playgroud)
你能帮我个忙吗?
例如,给定列表['one', 'two', 'one'],算法应该返回True,而给定['one', 'two', 'three']它应该返回False.
我试图像这样导入izip模块:
from itertools import izip
Run Code Online (Sandbox Code Playgroud)
然而,在最近从Python 2.7转换到3之后 - 它似乎不起作用.
我想写一个csv文件:
writer.writerows(izip(variable1,2))
Run Code Online (Sandbox Code Playgroud)
但我没有运气.仍然会遇到错误.
是否可以使用python在列表中获取哪些值是重复的?
我有一个项目列表:
mylist = [20, 30, 25, 20]
Run Code Online (Sandbox Code Playgroud)
我知道删除重复项的最佳方法是set(mylist),但是有可能知道重复的是什么值吗?如您所见,在此列表中,重复项是第一个和最后一个值.[0, 3].
是否有可能在python中获得此结果或类似的东西?我试图避免做出一个荒谬的大if elif条件陈述.
我正在尝试用pytest进行一些单元测试.
我在考虑这样做:
actual = b_manager.get_b(complete_set)
assert actual is not None
assert actual.columns == ['bl', 'direction', 'day']
Run Code Online (Sandbox Code Playgroud)
ok中的第一个断言但是第二个我有一个值错误.
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud)
我假设这不是用pytest断言两个不同列表相等的正确方法.
如何断言数据帧列(列表)是否等于预期的列?
谢谢
我扩展并添加了一个新问题.
我有一个清单:
li = [2, 3, 1, 4, 2, 2, 2, 3, 1, 3, 2]
Run Code Online (Sandbox Code Playgroud)
然后我认识到哪个值最常出现,我保留在变量中的值i2:
f = {}
for item in li:
f[item] = f.get(item, 0) + 1
for i in f:
if f[i]==int(max(f.values())):
i2 = i
Run Code Online (Sandbox Code Playgroud)
之后,重复的所有值都会增加10,但除了最大值之外.这是我使用的代码:
for i in range(len(li)):
for x in range(i + 1, len(li)):
if li[i] == li[x] and li[i] != i2:
li[x] = li[x] + 10
Run Code Online (Sandbox Code Playgroud)
完成此操作后,我得到:
li = [2, 3, 1, 4, 2, 2, 2, 13, 11, 23, 2]
Run Code Online (Sandbox Code Playgroud)
如您所见,最常见的值是2,因此它保持不变.例如,3发生三次,并且从所有三个新值创建3,3 …
我有两个这样的清单
found = ['CG', 'E6', 'E1', 'E2', 'E4', 'L2', 'E7', 'E5', 'L1', 'E2BS', 'E2BS', 'E2BS', 'E2', 'E1^E4', 'E5']
expected = ['E1', 'E2', 'E4', 'E1^E4', 'E6', 'E7', 'L1', 'L2', 'CG', 'E2BS', 'E3']
Run Code Online (Sandbox Code Playgroud)
我想找到两个列表之间的差异.
我已经做好了
list(set(expected)-set(found))
Run Code Online (Sandbox Code Playgroud)
和
list(set(found)-set(expected))
Run Code Online (Sandbox Code Playgroud)
返回['E3']和['E5']分别.
但是,我需要的答案是:
'E3' is missing from found.
'E5' is missing from expected.
There are 2 copies of 'E5' in found.
There are 3 copies of 'E2BS' in found.
There are 2 copies of 'E2' in found.
Run Code Online (Sandbox Code Playgroud)
欢迎任何帮助/建议!
我知道这段代码的效率不是最优的(尤其是巨大的输入),我知道有一种方法可以改变这种算法来处理其他数据类型,而不仅仅是字符串中的重复(显然只有这样)要搜索的很多字符).
有什么方法可以提高效率吗?
我尝试使用字典,并且函数保持返回'none',所以我尝试了一个列表,事情很好.
提前感谢任何可以帮助我的人!
def find_repeater(string):
my_list = []
my_list.append(string[0])
for i in range (1, len(string)):
if string[i] in my_list:
print 'repetition found'
return (string[i])
else:
my_list.append(string[i])
print find_repeater('abca')
Run Code Online (Sandbox Code Playgroud)
现在用字典....(它一直打印'无'到控制台)
def find_repeater(string):
my_dict = {}
my_dict[0] = string[0]
for i in range (1, len(string)):
if string[i] in my_dict:
print 'repetition found'
return string[i]
else:
my_dict[i] = string[i]
print find_repeater('abca')
Run Code Online (Sandbox Code Playgroud) 我正在研究代码片段以从列表中提取重复项。我在此站点上看到了几种实现/解决方案。但是,我无法正确理解这一行-我认为在语法上比较明智。排序后,将index(x)与index(x + 1)进行比较。如果已添加到集合中。
print(set([i for i in a if (a[i] == a[i+1]))
a = [1,2,3,2,1,5,6,5,5,5]
print(a)
print(set(sorted(a)))
# l1[i] == l1[i+1]
print(set([i for i in a if (a[i] == a[i+1]))
print(set([i for i in a if sum([1 for item in a if item == i]) > 1]))
Run Code Online (Sandbox Code Playgroud)
预期结果:{1、2、5}
我有两个非唯一的值列表,例如
["a", "b", "a", "c"]
Run Code Online (Sandbox Code Playgroud)
和
["a", "b", "b", "f"]
Run Code Online (Sandbox Code Playgroud)
我想找到第二个列表中哪些元素没有出现在第一个列表中.
我可以手动编写代码,但更喜欢使用内置函数.我无法弄清楚是怎么回事,因为我一直碰到可洗/不可用的障碍.
是否有任何内置方法可以打印 python 列表中存在的重复元素。
我可以为此编写程序。
我正在寻找的只是是否有任何内置方法或相同的方法。
例如:
对于输入 [4,3,2,4,5,6,4,7,6,8]
我需要操作 4,6
python ×12
list ×8
duplicates ×4
python-3.x ×3
python-2.7 ×2
string ×2
arrays ×1
compare ×1
intersection ×1
izip ×1
performance ×1
pytest ×1
unique ×1
unit-testing ×1