Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在列表中查找元素,使元素不应具有相似的编号_Python_Python 3.x_Logic - Fatal编程技术网

Python 在列表中查找元素,使元素不应具有相似的编号

Python 在列表中查找元素,使元素不应具有相似的编号,python,python-3.x,logic,Python,Python 3.x,Logic,考虑一个list=[23,52,44,32,78] 23,52,32所有这些元素都至少有一个公共数字,因此我要筛选的集合是[44,78],因为它们没有任何公共数字 另一个例子:[52,12255211223123,64,87999]将被过滤为[64,87999] 我目前的想法是将所有的数字转换成列表,如[2,3],[5,2]。。。取这些的交叉点,但我不知道如何比较所有这些子列表并过滤出所需的数字 def convert_into_sublist(i): sublist = [int(x)

考虑一个
list=[23,52,44,32,78]
23,52,32
所有这些元素都至少有一个公共数字,因此我要筛选的集合是
[44,78]
,因为它们没有任何公共数字

另一个例子:
[52,12255211223123,64,87999]
将被过滤为
[64,87999]

我目前的想法是将所有的数字转换成列表,如[2,3],[5,2]。。。取这些的交叉点,但我不知道如何比较所有这些子列表并过滤出所需的数字

def convert_into_sublist(i):
    sublist = [int(x) for x in str(i)] 

def intersection(l1, l2): 
    l3 = [value for value in l1 if value in l2]
    if(len(l3)==0):
        return 1

将数字作为字符列表处理,使用
设置
转换并测试是否不相交。也许不是性能最好的单衬里,但它可以:

lst = [23,52,44,32,78]
# optional: remove duplicates:
lst = set(lst)

unique = [l for l in lst if all(set(str(x)).isdisjoint(str(l)) for x in lst if x != l)]
结果:

>>> unique
[44, 78]
可能会稍微快一点:转换为字符串一次,处理字符串,最后转换回整数:

lst = [str(x) for x in lst]
unique = [int(l) for l in lst if all(set(x).isdisjoint(l) for x in lst if x != l)]

将数字作为字符列表处理,使用
设置
转换并测试是否不相交。也许不是性能最好的单衬里,但它可以:

lst = [23,52,44,32,78]
# optional: remove duplicates:
lst = set(lst)

unique = [l for l in lst if all(set(str(x)).isdisjoint(str(l)) for x in lst if x != l)]
结果:

>>> unique
[44, 78]
可能会稍微快一点:转换为字符串一次,处理字符串,最后转换回整数:

lst = [str(x) for x in lst]
unique = [int(l) for l in lst if all(set(x).isdisjoint(l) for x in lst if x != l)]

您可以使用
计数器
查找非常用数字:

from collections import Counter
from itertools import chain
from operator import itemgetter

lst = [23, 52, 44, 32, 78]

sets = [set(str(i)) for i in lst]
# [{'3', '2'}, {'5', '2'}, {'4'}, {'3', '2'}, {'7', '8'}]

c = Counter(chain.from_iterable(sets))
# Counter({'2': 3, '3': 2, '5': 1, '4': 1, '7': 1, '8': 1})

new_lst = []
for num, set_ in zip(lst, sets):
    counts = itemgetter(*set_)(c)
    if counts == 1 or set(counts) == {1}:
        new_lst.append(num)

print(new_lst)
# [44, 78]

您可以使用
计数器
查找非常用数字:

from collections import Counter
from itertools import chain
from operator import itemgetter

lst = [23, 52, 44, 32, 78]

sets = [set(str(i)) for i in lst]
# [{'3', '2'}, {'5', '2'}, {'4'}, {'3', '2'}, {'7', '8'}]

c = Counter(chain.from_iterable(sets))
# Counter({'2': 3, '3': 2, '5': 1, '4': 1, '7': 1, '8': 1})

new_lst = []
for num, set_ in zip(lst, sets):
    counts = itemgetter(*set_)(c)
    if counts == 1 or set(counts) == {1}:
        new_lst.append(num)

print(new_lst)
# [44, 78]

您如何定义
公共数字
,它们需要定义为
公共
的最小出现次数是多少?基于此,我们可以思考一种逻辑out@DeveshKumarSingh即使有一个数字是公共的,那么它在过滤列表中的所有数字都应该是不常见的。您如何定义
公共数字
,它们需要最少出现多少次才能定义为
公共
?基于此,我们可以思考一种逻辑out@DeveshKumarSingh即使一个数字是常见的,那么它对过滤列表中所有数字的重击也应该是不常见的。这是一个很好的解决方案。一个小问题似乎是由于
x!=l
测试
[23,52,44,32,78,44]
导致
[44,78,44]
不确定这是否是OP的问题。啊,是真的,在这种情况下,只需将输入设置为
即可删除重复项。编辑这是一个很好的解决方案。一个小问题似乎是由于
x!=l
测试
[23,52,44,32,78,44]
导致
[44,78,44]
不确定这是否是OP的问题。啊,是真的,在这种情况下,只需将输入设置为
即可删除重复项。编辑