[英]Python : How to order in a set
So ive been doing this assignment that returns the integer that doesn't have a matching integer or -(integer). 因此,我一直在执行此赋值操作,以返回没有匹配整数或-(integer)的整数。 so far I have: 到目前为止,我有:
def only_once(a):
c = a
z = []
freq = Counter(c)
for key, value in freq.items():
if value == 1:
z.append(key)
return z
The main problem however, is the ordering. 但是,主要问题是订购。 if I put in a large list, and then using the function it will just rearrange the list into a set order. 如果我放了一个大列表,然后使用该功能,它将只是将列表重新排列为固定顺序。 So how do i make the ordering of z be like a but with the removed variables. 因此,如何使z的排序像a一样,但是要删除变量。
def only_once(a):
c = a
z = []
freq = Counter(c)
for key in a:
if freq[key] == 1:
z.append(key)
return z
Or as a list comprehension 或作为列表理解
def only_once(a):
freq = Counter(a)
z = [key for key in a if freq[key] == 1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.