I have a data structure as follows,
<code>data = {
'0_0': [('0_0', 0), ('0_1', 1), ('0_2', 2)],
'0_1': [('0_0', 1), ('0_1', 0), ('0_2', 1)],
'0_2': [('0_0', 2), ('0_1', 1), ('0_2', 0)],
}
</code>
<code>data = {
'0_0': [('0_0', 0), ('0_1', 1), ('0_2', 2)],
'0_1': [('0_0', 1), ('0_1', 0), ('0_2', 1)],
'0_2': [('0_0', 2), ('0_1', 1), ('0_2', 0)],
}
</code>
data = {
'0_0': [('0_0', 0), ('0_1', 1), ('0_2', 2)],
'0_1': [('0_0', 1), ('0_1', 0), ('0_2', 1)],
'0_2': [('0_0', 2), ('0_1', 1), ('0_2', 0)],
}
How to delete tuple from lists by given key? For example I wrote a function as follows,
<code>def remove_all_acs(
dictionary,
delete_ac
):
for key in dictionary:
acs = dictionary[key]
for ac in acs:
if ac[0] == delete_ac:
acs.remove(ac)
break
return dictionary
</code>
<code>def remove_all_acs(
dictionary,
delete_ac
):
for key in dictionary:
acs = dictionary[key]
for ac in acs:
if ac[0] == delete_ac:
acs.remove(ac)
break
return dictionary
</code>
def remove_all_acs(
dictionary,
delete_ac
):
for key in dictionary:
acs = dictionary[key]
for ac in acs:
if ac[0] == delete_ac:
acs.remove(ac)
break
return dictionary
It’s my expected output and returns a result as follows,
<code>{
'0_0': [('0_1', 1), ('0_2', 2)],
'0_1': [('0_1', 0), ('0_2', 1)],
'0_2': [('0_1', 1), ('0_2', 0)]
}
</code>
<code>{
'0_0': [('0_1', 1), ('0_2', 2)],
'0_1': [('0_1', 0), ('0_2', 1)],
'0_2': [('0_1', 1), ('0_2', 0)]
}
</code>
{
'0_0': [('0_1', 1), ('0_2', 2)],
'0_1': [('0_1', 0), ('0_2', 1)],
'0_2': [('0_1', 1), ('0_2', 0)]
}
It works but is not effective on large lists. Do you have any idea? Thanks in advance.
In addition you can create large dataset using this code,
<code>def generate(size, max_connections):
data = {}
keys = [f'{i}_{j}' for i in range(size) for j in range(size)]
for key in keys:
connections = random.randint(1, min(max_connections, 10))
data[key] = [
(random.choice(keys), random.randint(1, 10))
for _ in range(connections)
]
return data
data = generate(200, 10)
</code>
<code>def generate(size, max_connections):
data = {}
keys = [f'{i}_{j}' for i in range(size) for j in range(size)]
for key in keys:
connections = random.randint(1, min(max_connections, 10))
data[key] = [
(random.choice(keys), random.randint(1, 10))
for _ in range(connections)
]
return data
data = generate(200, 10)
</code>
def generate(size, max_connections):
data = {}
keys = [f'{i}_{j}' for i in range(size) for j in range(size)]
for key in keys:
connections = random.randint(1, min(max_connections, 10))
data[key] = [
(random.choice(keys), random.randint(1, 10))
for _ in range(connections)
]
return data
data = generate(200, 10)