[英]print unique numbers from a sorted list python
I'm trying to print all elements in a sorted list that only occur once . 我正在尝试打印仅出现一次的排序列表中的所有元素。
My code below works but I'm sure there is a better way: 我的以下代码可以正常工作,但是我确定有更好的方法:
def print_unique(alist):
i = 0
for i in range(len(alist)):
if i < (len(alist)-1):
if alist[i] == alist[i+1]:
i+=1
if alist[i] == alist[i-1]:
i+=1
elif alist[i] == alist[i-1]:
i+=1
else:
print alist[i]
else:
if alist[-1]!= alist[-2]:
print alist[-1]
randomlist= [1,2,3,3,3,4,4,5,6,7,7,7,7,8,8,8,9,11,12,14,42]
print_unique(randomlist)
This produces 这产生
1
2
5
6
9
11
12
14
42
eg all values that only appear once in a row. 例如,所有值仅连续出现一次。
You could use the itertools.groupby()
function to group your inputs and filter on groups that are one element long: 您可以使用itertools.groupby()
函数对输入进行分组,并根据一个元素长的组进行过滤:
from itertools import groupby
def print_unique(alist):
for elem, group in groupby(alist):
if sum(1 for _ in group) == 1: # count without building a new list
print elem
or if you want to do it 'manually', track the last item seen and if you have seen it more than once: 或者,如果您想“手动”执行此操作,请跟踪所看到的最后一项,以及是否多次查看过:
def print_unique(alist, _sentinel=object()):
last, once = _sentinel, False
for elem in alist:
if elem == last:
once = False
else:
if once:
print last
last, once = elem, True
if last is not _sentinel and once:
print last
You may want to replace the print
statements with yield
and leave printing to the caller: 您可能要用yield
替换print
语句,然后将打印留给调用方:
def filter_unique(alist):
for elem, group in groupby(alist):
if sum(1 for _ in group) == 1: # count without building a new list
yield elem
for unique in filter_unique(randomlist):
print unique
This question seems to have duplicates. 这个问题似乎有重复。
If you do not wish to preserve the order of your list, you can do 如果您不希望保留列表的顺序,则可以执行
print list(set(sample_list))
You can also try, 您也可以尝试
unique_list = []
for i in sample_list:
if i not in unique_list:
unique_list.append(i)
EDIT: 编辑:
In order to print all the elements in the list so that they appear once in a row, you can try this 为了打印列表中的所有元素以便它们连续出现一次,您可以尝试以下操作
print '\n'.join([str(i) for i in unique_list])
And, as @martijn-pieters mentioned it in the comments, the first code was found to be very fast compared to the second one, when I did a small benchmark. 而且,正如@ martijn-pieters在评论中提到的那样,发现第一个代码与第二个代码相比非常快,而第二个代码却做了一个小型基准测试。 On a list of 10^5 elements, the second code took 63.66 seconds to complete whereas the first one took a mere 0.2200 seconds. 在10 ^ 5个元素的列表上,第二个代码花了63.66秒完成,而第一个代码仅花了0.2200秒。 (on a list generated using random.random()
) (在使用random.random()
生成的列表上)
you can do by this: 您可以这样做:
print (set(YOUR_LIST))
or if you need a list use this: 或者,如果您需要列表,请使用以下命令:
print (list(set(YOUR_LIST)))
Sets are lists containing unique items. 集是包含唯一项的列表。 If you construct a set from the array, it will contain only the unique items: 如果从数组构造一个集合,它将仅包含唯一项:
def print_unique(alist):
print set( alist )
The input list does not need to be sorted. 输入列表不需要排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.