- Удалить повторы из словаря
- 5 ways to Remove duplicates list of Python dictionaries
- 1. For loop to Remove duplicates list of Python dictionaries
- Program Example
- Output
- 3. Remove duplicate lists of dictionary by convert list to tuple
- Program Example
- Output
- 4. Using package iteration_utilities.unique_everseen
- Program Example
- Output
- 5. frozenset() to Remove duplicates list of dictionaries
- Program Example
- Output
- Conclusion
- Python remove duplicates from dictionary | Example code
- Example remove duplicates from the dictionary in Python
- Using for loop
- Using dictionary comprehension
- Write a Python program to remove duplicates from Dictionary
- Удаление дубликатов из словаря
- 10 ответов
Удалить повторы из словаря
Есть текст, там сплошная строка без пробелов и одни символы, для того чтобы расшифровать по виженеру мне нужно посчитать индекс совпадения для каждой буквы, а затем его сложить и сравнить с общим индексом совпадением, только вот беда, очень хотелось бы получить отсортированный словарь, чтобы потом из него вывести строку, только при сортировке появляются повторы, уже всю голову сломал как решить эту проблему.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
f = open('text2.txt') c='' u=[] g=0 slovar={} for word in f.read(): word c=c+word c=c.upper() u.append(c) print(u) v='' for n in range(0, len(c), 10): v=v+c[n] print(v) for i in v: if i not in slovar: slovar[i]=int(1) else: slovar[i]=slovar[i] +1 for znach in (sorted(val for key,val in slovar.items())): for key,val in slovar.items(): if val==znach: print(key, (val*(val-1))/(len(v)*(len(v)-1)))
Результат:
D 0.0
N 1.2578492940572406e-07
U 1.2578492940572406e-07
N 1.2578492940572406e-07
U 1.2578492940572406e-07
B 7.547095764343444e-07
Z 6.64144427262223e-05
O 9.320663268964154e-05
F 0.00027811047891605593
C 0.00032150627956103073
K 0.0003490531791008843
T 0.00043848626390835413
J 0.000561629709796558
A 0.000561629709796558
Q 0.000561629709796558
J 0.000561629709796558
A 0.000561629709796558
Q 0.000561629709796558
J 0.000561629709796558
A 0.000561629709796558
Q 0.000561629709796558
G 0.0006999931321428545
Y 0.0007959670332794219
P 0.0016403612643800475
H 0.0018069005109132262
L 0.0036075117753561663
V 0.004137821037730699
W 0.004466748628126667
R 0.004773538070947228
S 0.005126364797930285
M 0.005679189562668442
E 0.006420062796868156
X 0.007121439563234474
I 0.015008154636973373
5 ways to Remove duplicates list of Python dictionaries
In this post, we will understand the different 5 ways to Remove duplicates list of Python dictionaries with examples. Here we are using simple for loop, list comprehension,frozenset(), and many more techniques.
1. For loop to Remove duplicates list of Python dictionaries
The simple and easy method comes to our mind to loop over lists of dictionaries and compares the value for a duplicate to remove the duplicate value.
In this code example, we are looping over the lists of dictionaries and removing the duplicate dictionary, and appending it in the new dictionary.
Let us understand with code examples and build our understanding from it.
Program Example
#Program to remove duplicates from a list of dictionaries Langlist = [, , <"C++" : 3>, , ,] #removing the duplicate entry unique_dict = [k for j, k in enumerate(Langlist) if k not in Langlist[j + 1:]] print('after Removing duplicate from list of dictionary =\n',unique_dict)
Output
after Removing duplicate from list of dictionary = [, <'C++': 3>, , ]
3. Remove duplicate lists of dictionary by convert list to tuple
In this code example, we are converting lists of dictionaries into a list of tuples, and further using the set Comprehension to remove the duplicate from the lists of dictionaries.
Let us understand with code examples and build our understanding.
Program Example
#Program to remove duplicates from a list of dictionaries Langlist = [, , <"C++" : 3>, , ,] #removing the duplicate entry unique_dict = [dict(tuple) for tuple in ] print('after Removing duplicate from list of dictionary =\n',unique_dict)
Output
after Removing duplicate from list f dictionary = [, , , <'C++': 3>]
4. Using package iteration_utilities.unique_everseen
In this code example, we are using the third-party package to remove duplicates from lists of dictionaries. We are importing the package iteration_utilities import unique_everseen and using its unique_everseen() function to remove duplicates.
Let us understand with code example below.
Program Example
#Program to remove duplicates from a list of dictionaries from iteration_utilities import unique_everseen Langlist = [, , <"C++" : 3>, , ,] #removing the duplicate entry unique_dict=list(unique_everseen(Langlist)) print('after Removing duplicate from list of dictionary =\n',unique_dict)
Output
after Removing duplicate from list of dictionary = [, , <'C++': 3>, ]
5. frozenset() to Remove duplicates list of dictionaries
frozenset() is an inbuilt function of python it takes iterable as an argument and makes it immutable(unchangeable) or it freezes the iterable object and makes it unchangeable. It returns an immutable frozenset() object initialize with the elements from the iterable.
In this code snippet, we are using the frozenset() to remove duplicates from lists of dictionaries.
Let us understand with the code example below.
Program Example
#Program to remove duplicates from a list of dictionaries Langlist = [, , <"C++" : 3>, , ,] #removing the duplicate entry unique_dict = .values() print("after Removing duplicate from list of dictionary ="+str(unique_dict))
Output
after Removing duplicate from list of dictionary = dict_values([, , <'C++': 3>, ])
Conclusion
In this post, we understood the different ways to remove duplicates from the list of dictionaries. We hope you will find these ways useful and these will help you to solve your problem.
These techniques will be really helpful when you are working on the Data analysis work and they help you clean your data set samples.
Python remove duplicates from dictionary | Example code
We can use loop or dictionary comprehension to remove duplicates from the dictionary in Python. While removing a duplicate value from the dictionary the keys are also removed in the process.
If you don’t care about retaining the original order then set(my_list) will remove all duplicates.
Example remove duplicates from the dictionary in Python
Using for loop
This is the brute force method, where add first of occurred value in a variable, and remove it if it repeats.
dict1 = temp = [] res = dict() for key, val in dict1.items(): if val not in temp: temp.append(val) resУдалить дубликаты python из словаря = val print(res)
Using dictionary comprehension
This method does the same as above but it is a shorthand.
dict1 = temp = res = print(res)
Write a Python program to remove duplicates from Dictionary
Here is a python example that shows how to remove duplicate values from a dictionary.
student_data = , 'id2': , 'id3': , 'id4': , > res = <> for key, value in student_data.items(): if value not in res.values(): resУдалить дубликаты python из словаря = value print(res)
Do comment if you have any doubts and suggestions on this Python dictionary tutorial.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Удаление дубликатов из словаря
У меня есть следующая структура данных словаря Python 2.7 (я не контролирую исходные данные — приходит из другой системы как есть):
, 112762853385: , 112760496444: , 112760496502: , 112765083670: . >
Ключи словаря всегда будут уникальными. Dst, src и alias могут быть дубликатами. Все записи всегда будут иметь dst и src, но не каждая запись обязательно будет иметь псевдоним, как видно в третьей записи.
В примере данных будет удалена любая из первых двух записей (мне не важно, какая из них). Третья запись будет считаться уникальной, поскольку, хотя dst и src одинаковы, в ней отсутствует псевдоним.
Моя цель состоит в том, чтобы удалить все записи, где dst, src и alias были дублированы, независимо от ключа.
Как этот новобранец достиг этого?
Кроме того, мое ограниченное понимание Python интерпретирует структуру данных как словарь со значениями, хранящимися в словарях . диктат, правильно?
10 ответов
Вы можете просмотреть каждый из элементов (пара значений ключа) в словаре и добавить их в словарь результатов, если значение еще не было в словаре результатов.
input_raw = , 112762853385: , 112760496444: , 112760496502: > result = <> for key,value in input_raw.items(): if value not in result.values(): resultУдалить дубликаты python из словаря = value print result
example = < 'id1': , 'id2': , 'id3': , 'id4': , > for item in example: for value in example: if example[item] ==example[value]: if item != value: key = value del exampleУдалить дубликаты python из словаря print "example",example
Еще один вариант обратного дикта:
>>> import pprint >>> >>> data = < . 112762853378: . , . 112762853385: . , . 112760496444: . , . 112760496502: . , . > >>> >>> keep = set(.values()) >>> >>> for key in data.keys(): . if key not in keep: . del dataУдалить дубликаты python из словаря . >>> >>> pprint.pprint(data) , 112760496502L: , 112762853378L: >
Так как способ найти уникальность в соответствиях — это точно использовать словарь, ключом которого является желаемое уникальное значение, можно пойти путем создания обратного диктанта, в котором ваши значения составляются как ключ, а затем воссоздать перевернутый »словарь с использованием промежуточного результата.
dct = , 112762853385: , 112760496444: , 112760496502: , > def remove_dups (dct): reversed_dct = <> for key, val in dct.items(): new_key = tuple(val["dst"]) + tuple(val["src"]) + (tuple(val["alias"]) if "alias" in val else (None,) ) reversed_dct[new_key] = key result_dct = <> for key, val in reversed_dct.items(): result_dct[val] = dct[val] return result_dct result = remove_dups(dct)
input_raw = , 112762853385: , 112760496444: , 112760496502: , 112758601487: , 112757412898: , 112757354733: , > for x in input_raw.iteritems(): print x print '\n---------------------------\n' seen = [] for k,val in input_raw.items(): if val in seen: del input_raw[k] else: seen.append(val) for x in input_raw.iteritems(): print x
(112762853385L, ) (112757354733L, ) (112758601487L, ) (112757412898L, ) (112760496502L, ) (112760496444L, ) (112762853378L, ) --------------------------- (112762853385L, ) (112757354733L, ) (112757412898L, ) (112760496444L, )
Недостатки в том, что это решение сначала создает список input_raw.iteritems () (как в ответе Эндрю Кокса) и требует растущего списка увиденное .
Но первое нельзя избежать (использование iteritems () не работает), а второе менее тяжело, чем воссоздание списка result.values () из растущего списка result за каждый ход цикла.
from collections import defaultdict dups = defaultdict(lambda : defaultdict(list)) for key, entry in data.iteritems(): dups[tuple(entry.keys())][tuple([v[0] for v in entry.values()])].append(key) for dup_indexes in dups.values(): for keys in dup_indexes.values(): for key in keys[1:]: if key in data: del dataУдалить дубликаты python из словаря
dups=<> for key,val in dct.iteritems(): if val.get('alias') != None: ref = "%s%s%s" % (val['dst'] , val['src'] ,val['alias'])# a simple hash dups.setdefault(ref,[]) dups[ref].append(key) for k,v in dups.iteritems(): if len(v) > 1: for key in v: del dctУдалить дубликаты python из словаря
Одним из простых подходов было бы создание обратного словаря с использованием конкатенации строковых данных в каждом внутреннем словаре в качестве ключа. Допустим, у вас есть вышеупомянутые данные в словаре, d :
>>> import collections >>> reverse_d = collections.defaultdict(list) >>> for key, inner_d in d.iteritems(): . key_str = ''.join(inner_d[k][0] for k in ['dst', 'src', 'alias'] if k in inner_d) . reverse_dУдалить дубликаты python из словаря.append(key) . >>> duplicates = Удалить дубликаты python из словаря >>> duplicates [[112762853385, 112762853378]]
Если вам не нужен список дубликатов или что-то в этом роде, но вы просто хотите создать диктат без дубликатов, вы можете просто использовать обычный словарь вместо defaultdict и выполнить обратное обратное преобразование следующим образом:
>>> for key, inner_d in d.iteritems(): . key_str = ''.join(inner_d[k][0] for k in ['dst', 'src', 'alias'] if k in inner_d) . reverse_dУдалить дубликаты python из словаря = key >>> new_d = dict((val, d[val]) for val in reverse_d.itervalues())
Я бы просто сделал набор из списка ключей, а затем перебрал их в новый dict:
input_raw = , 112762853385: , 112760496444: , 112760496502: > filter = list(set(list(input_raw.keys()))) fixedlist = <> for i in filter: fixedlist[i] = logins[i]
Чтобы решить вашу проблему.