Python 3.x 收集并将特殊的键和值放在字典中

Python 3.x 收集并将特殊的键和值放在字典中,python-3.x,list,dictionary,set,Python 3.x,List,Dictionary,Set,我希望得到一个非常不寻常的输出,如下所需输出所示: 我一整天都在尝试很多方法,但仍然不知道如何得到它 Two lists: a = ['11', '22', '33','44', '55', '66'] b = ['E', 'AA', 'AA','AA', 'SS', 'SS'] 期望输出: {'11': ['E'], '22': ['AA','33','44'], # order does not matter '33': ['AA','22','44'], '44': ['AA','

我希望得到一个非常不寻常的输出,如下所需输出所示:

我一整天都在尝试很多方法,但仍然不知道如何得到它

Two lists:
a = ['11', '22', '33','44', '55', '66']
b = ['E', 'AA', 'AA','AA', 'SS', 'SS']
期望输出:

{'11': ['E'],
 '22': ['AA','33','44'], # order does not matter
 '33': ['AA','22','44'],
 '44': ['AA','22','33'],
 '55': ['SS','66'],
 '66': ['SS','55']}

For better ubderstanding, if a and b are: 
a = ['11', '22', '33','44', '55', '66']
b = ['E', 'AA', 'AA','AA', 'SS', 'AA']
Desired output:
{'11': ['E'],
 '22': ['AA','33','44','66'], # order does not matter
 '33': ['AA','22','44','66'],
 '44': ['AA','22','33','66'],
 '55': ['SS'],
 '66': ['AA','22','33','44']
}

问题: 哪个适合获得我想要的输出?元组、字典还是集合

我目前的状况:

a = ['11', '22', '33','44', '55', '66']
b = ['E', 'AA', 'AA','AA', 'SS', 'SS']

#put them into a dictionary:
dict_1 = {}
for i, j in zip(a, b):
    dict_1.setdefault(i, []).append(j)    
print(dict_1)

c = list(zip(a,b))
i=0
#find the target tuples
while i<len(c):
    j = i+1
    while i < j < len(c):
        if c[i][1] == c[j][1]:
            print('Got a repeat one')  
            print(c[i], c[j])
        j+=1     
    i+=1
开放性问题

一旦获得了目标元组,我如何组合它们以获得所需的输出,我已经尝试使用.append,但它是一个大批量

尝试收集目标时输出不正确:

['AA', '33', 'AA', '44', 'AA', '44', 'SS', '66']
['AA', '33', '44', 'SS', '66']
如果有人对此有任何提示,我非常感谢,提前谢谢

试试看:

a = ["11", "22", "33", "44", "55", "66"]
b = ["E", "AA", "AA", "AA", "SS", "AA"]

tmp = {}
for i, j in zip(a, b):
    tmp.setdefault(j, []).append(i)

out = {}
for k, v in tmp.items():
    for i in v:
        vc = v.copy()
        vc.remove(i)
        vc.append(k)
        out[i] = vc

print(out)
印刷品:

{'11':['E'],
‘22’:[‘33’、‘44’、‘66’、‘AA’],
‘33’:[‘22’、‘44’、‘66’、‘AA’],
‘44’:[‘22’、‘33’、‘66’、‘AA’],
‘66’:[‘22’、‘33’、‘44’、‘AA’],
'55':['SS']}

这里有一个可能的解决方案

# Assumes the lists are equal length

# Input 1
#a = ['11', '22', '33','44', '55', '66']
#b = ['E', 'AA', 'AA','AA', 'SS', 'SS']

# Input 2
a = ['11', '22', '33','44', '55', '66']
b = ['E', 'AA', 'AA','AA', 'SS', 'AA']

# Dictionary with instances of a that 
# correspond to b - assumes all elements
# of a are unique 
a_in_b = {}
for el_a, el_b in zip(a,b):
    if el_b in a_in_b:
        # If already a key there, append
        a_in_b[el_b].add(el_a)
    else:
        # Initialize a new list
        a_in_b[el_b] = set([el_a])

# Check it
#print(a_in_b)

# Now get the final structure
output = {}
for el_a, el_b in zip(a,b):
    output[el_a] = [el_b]
    rest = a_in_b[el_b] - set([el_a])
    if rest:
        # If not empty
        output[el_a] += list(rest)

print(output)
它首先创建
a
中所有元素的字典,该字典将对应于
b
中的元素,然后构造所需的输出。它使用集合,因为后面使用了集合差异,
-
运算符

请注意代码中突出显示的假设:
a
b
大小相同,
a
条目是唯一的。如果不是,则需要使用列表。根据您的需求,您还必须更改
-
方法


也可以考虑将变量的名称调整为更好地反映你的应用程序。

如果代码> B=(e)、“AA”、“AA”、“AA”、“SS”、“AA”<代码>,输出应该是什么?谢谢,Andrej,我刚刚根据所提到的B更新了所需的输出。非常感谢,这似乎很好地解释清楚了!询问是否还有不清楚的地方。另外,请记住将
set([entry])
-与
[]
一起使用,否则它将创建一组字符串(
set('11')
变为
{1'}
)。谢谢,它工作得很好,我想评论的一件事是,如果列表a和b中的项目类型不是
type
,或者部分是
type
,然后,
out{}
清除/刷新类型。例如,列表b中的
type
实际上是
spacy.tokens.span.span
,而列表a中的
type
type
,但是运行此代码后,
out{}
返回no
type
,我还没有找出原因:)嗨@Andrej,关于代码的问题:你知道吗,为什么我不能用
vc=v
替换
vc=v.copy()
?提前谢谢@Melina,因为您将使用
vc.remove
vc.append
修改列表,如果没有副本,您基本上只修改一个列表-结果错误。
# Assumes the lists are equal length

# Input 1
#a = ['11', '22', '33','44', '55', '66']
#b = ['E', 'AA', 'AA','AA', 'SS', 'SS']

# Input 2
a = ['11', '22', '33','44', '55', '66']
b = ['E', 'AA', 'AA','AA', 'SS', 'AA']

# Dictionary with instances of a that 
# correspond to b - assumes all elements
# of a are unique 
a_in_b = {}
for el_a, el_b in zip(a,b):
    if el_b in a_in_b:
        # If already a key there, append
        a_in_b[el_b].add(el_a)
    else:
        # Initialize a new list
        a_in_b[el_b] = set([el_a])

# Check it
#print(a_in_b)

# Now get the final structure
output = {}
for el_a, el_b in zip(a,b):
    output[el_a] = [el_b]
    rest = a_in_b[el_b] - set([el_a])
    if rest:
        # If not empty
        output[el_a] += list(rest)

print(output)