Delete the duplicate arrays inside an array in python - python

I want to ignore the duplicates from an array that has multiple array in lowest running cost. For example;
A = [['1','2'],['3','4'],['5','6'],['1','2'],['3','4'],['7','8']]
the expected output should be like as
Output = [['1','2'],['3','4'],['5','6'],['7','8']]
Is it possible to compare arrays inside an array.
I am doing in this way;
A = [['1','2'],['3','4'],['5','6'],['1','2'],['3','4'],['7','8']]
output = set()
for x in A:
output.add(x)
print (output)
But it prompts;
TypeError: unhashable type: 'list'

How about something simple like:
B = list(map(list, set(map(tuple, A))))
Here's my "bakeoff" -- please let me know if I've misrepresented your solution:
import timeit
from random import choice
DIGITS = list("123456789")
# one million elements in list
A = [[choice(DIGITS), choice(DIGITS)] for _ in range(1000000)]
def elena(A): # MrName's solution is identical
B = []
for i in A:
if i not in B:
B.append(i)
return B
def cdlane(A):
return list(map(list, set(map(tuple, A))))
def VikashSingh(A):
uniques = set()
B = []
for x in A:
val = '-'.join([str(key) for key in x])
if val not in uniques:
B.append(x)
uniques.add(val)
return B
def AbhilekhSingh(A):
def unique_elements(l):
last = object()
for item in l:
if item == last:
continue
yield item
last = item
return list(unique_elements(sorted(A)))
# sanity check to make sure everyone one agrees on the answer
B = sorted(elena(A))
assert(B == sorted(cdlane(A)))
assert(B == sorted(VikashSingh(A)))
assert(B == sorted(AbhilekhSingh(A)))
print("elena:", format(timeit.timeit('B = elena(A)', number=10, globals=globals()), ".3"))
print("cdlane:", format(timeit.timeit('B = cdlane(A)', number=10, globals=globals()), ".3"))
print("VikashSingh:", format(timeit.timeit('B = VikashSingh(A)', number=10, globals=globals()), ".3"))
print("AbhilekhSingh:", format(timeit.timeit('B = AbhilekhSingh(A)', number=10, globals=globals()), ".3"))
RESULTS
elena: 17.5
cdlane: 2.04
VikashSingh: 10.0
AbhilekhSingh: 8.83

Here's a simple solution:
In [27]: A = [['1','2'],['3','4'],['5','6'],['1','2'],['3','4'], ['7','8']]
In [28]: new_list = []
In [29]: for i in A:
...: if i not in new_list:
...: new_list.append(i)
...:
In [30]: new_list
Out[30]: [['1', '2'], ['3', '4'], ['5', '6'], ['7', '8']]

You can sort the list and compare every element with it's previous one.
List length: n
Element length: m
Complexity: Sorting(n * log(n) * m) + Comparison(n * m) = Total(n * log(n) * m)
Try this:
def unique_elements(l):
last = object()
for item in l:
if item == last:
continue
yield item
last = item
def remove_duplicates(l):
return list(unique_elements(sorted(l)))

Another potentially simple solution, but not sure how the "cost" would compare to other presented solutions:
A = [['1','2'],['3','4'],['5','6'],['1','2'],['3','4'],['7','8']]
res = []
for entry in A:
if not entry in res:
res.append(entry)

List length: n
Element length: m
Complexity:
Iterate on n
Format key by iterating on m
Check key exists is set `uniques` in O(1)
Total running time is is O(n * m)
one simple way to do this is:
uniques = set()
output = []
for x in A:
val = '-'.join([str(key) for key in x])
if val not in uniques:
output.append(x)
uniques.add(val)
print (output)
output:
[['1', '2'], ['3', '4'], ['5', '6'], ['7', '8']]

Related

Append new values to the same key in a Python dictionary

I am inputting:
5
A 3
A 2
A 4
B 13
B 14
And I want to put them in the dictionary like this.
{A: ['3', '2', '4'] , B['13','14']}
But I got this:
{'A': ['3', '2', '4'], 'B': ['1', '3', '14']}
I tried:
N = int(input())
d = dict()
for i in range(N):
first,second = [j for j in input().split()]
if first in d:
d[first].append(second)
else:
d[first] = list(second)
print(d)
You want
d[first] = [second]
not
d[first] = list(second)
list(string) iterates over string and places each individual character as a separate element in the list.
[string] creates a list with the entire string as an element.
Here after the input it checks whether it is already in the dictionary and if it appends the new value and if it isn't already there then it creates a new list in the dictionary.
N = int(input())
d = dict()
for i in range(N):
i = input()
lst = i.split()
if lst[0] in d:
d[lst[0]].append(lst[1])
else:
d[lst[0]] = [lst[1]]
print(d)

How to add a number to each element in list a that gives a new list b with sum added number

Hi How to add a number to each integer in the list A that generates new list B using python. also I want to generate n number of lists
Example:
a = [1,2,3] and n = 2
add k = 3 to each element in a that gives b = [4,5,6] and add k to list b that gives c = [7,8,9]
I achieved this using map function but I want to generate n number of lists.
def bw(n, k):
test_list = [1,2,3]
if n > 1:
res = list(map(lambda x :x+k, test_list))
return test_list, res
else:
return test_list
print bw(3, 2)
Output b = [4,5,6]
c = [7,8,9]
If you just want the final array:
def bw(n, k):
test_list = [1,2,3]
return [x+n*k for x in test_list]
print(bw(3, 2))
If you want a list of lists as well:
def bw(n, k):
test_list = [1,2,3]
res = []
for i in range(0,n):
test_list = [x+k for x in test_list]
res.append(test_list)
return test_list, res
print(bw(3, 2))
res = [x + k for x in test_list]
This is straight from GeeksforGeeks.
Here is a solution:
test_list = [1,2,3]
def bw(test_list, n, k):
output= []
temp = test_list
for i in range(n):
temp = [j + k for j in temp]
output.append(temp)
return output
print (bw(test_list, 2, 3))

For loop inside another for loop Python

How do you add one list to another, I keep running into the problem of the second list in my for loop going through the whole list.
If aList was [1, 2, 3, 4], I want the output be 1hello, 2good, 3what... so on.
def function(aList):
myList = ['hello','good','what','tree']
newList = []
for element in aList:
for n in myList:
newList.append[element+n]
Input:
[1, 2, 3, 4]
Expected output:
['1hello', '2good', '3what', '4tree']
You want zip:
def function(aList):
myList = ['hello', 'good', 'what', 'tree']
return [str(a) + b for a, b in zip(aList, myList)]
Output:
In [4]: function([1, 2, 3, 4])
Out[4]: ['1hello', '2good', '3what', '4tree']
You also need to cast the passed in value to a string to make sure you can concatenate to the strings in your myList.
Read about List Comprehensions
aList = ['1', '2', '3']
print [element + n for element in aList for n in myList]
['1hello', '1good', '1what', '1tree', '2hello', '2good', '2what', '2tree', '3hello', '3good', '3what', '3tree']
or zip
aList = ['1', '2', '3']
print [element + n for element, n in zip(aList, myList)]
['1hello', '2good', '3what']
In Your question I feel no need to add two for loop. One loop itself sufficient.
let me give two cases, which one will match your need use that.
Case 1: - with one for loop
aList = [1,2,3,4]
def function(aList):
myList = ['hello','good','what','tree']
newList = []
for i in range(len(aList)):
newList.append(str(aList[i]) + myList [i] )
return newList
this will return 1hello , 2good ...
Case 2: - with two for loop
aList = [1,2,3,4]
def function(aList):
myList = ['hello','good','what','tree']
newList = []
for element in aList:
for i in range(len(myList )):
newList.append(str(element) + myList [i] )
return newList
this will return 1hello , 1good ... 2hello, 2good
I hope this will help...

python numpy parse array to get items

Hi I tried taking distinct items of every sublist and making an array
My input is a 2D list
alist = [['1','2'], ['3','5','2'], ['15','1'], ['5','657','3','1']]
And output I want is an array of distinct items
out = [1,2,3,5,15,657]
I tried
from numpy import np
alist = [['1','2'], ['3','5','2'], ['15','1'], ['5','657','3','1']]
anarray = np.array(alist)
newarray = []
for i in anarray:
for j in i:
if j in newarray:
pass
else:
print j
You could make use of a set:
out = set()
for inner in alist:
out.update(inner)
out = map(int, out) # in your example you have a list of ints
>>> print out
[1, 2, 3, 5, 15, 657]
from itertools import chain
alist = [['1', '2'], ['3', '5', '2'], ['15', '1'], ['5', '657', '3', '1']]
# Flatten the list into a single-level list of all the values
flattened_list = list(chain.from_iterable(alist))
# Use a set to remove the duplicates
uniques = set(flattened_list)
# Sort the unique values by the numeric value of each value
sorted_results = sorted(uniques, key=lambda value: int(value))
print sorted_results
Modifying your code slightly you can store your result in newarray:
alist = [['1','2'], ['3','5','2'], ['15','1'], ['5','657','3','1']]
newarray = []
for i in alist:
for j in i:
if j in newarray:
pass
else:
newarray.append(j)
Your result will be stored in newarray
To make it slightly better:
alist = [['1','2'], ['3','5','2'], ['15','1'], ['5','657','3','1']]
newarray = []
for i in alist:
for j in i:
if j not in newarray:
newarray.append(j)

How to split an iterable into two lists with alternating elements

I want to split an iterable into two lists with alternating elements. Here is a working solution. But is there a simpler way to achieve the same?
def zigzag(seq):
"""Return two sequences with alternating elements from `seq`"""
x, y = [], []
p, q = x, y
for e in seq:
p.append(e)
p, q = q, p
return x, y
Sample output:
>>> zigzag('123456')
(['1', '3', '5'], ['2', '4', '6'])
If seq is a sequence, then:
def zigzag(seq):
return seq[::2], seq[1::2]
If seq is a totally generic iterable, such as possibly a generator:
def zigzag(seq):
results = [], []
for i, e in enumerate(seq):
results[i%2].append(e)
return results
This takes an iterator and returns two iterators:
import itertools
def zigzag(seq):
t1,t2 = itertools.tee(seq)
even = itertools.islice(t1,0,None,2)
odd = itertools.islice(t2,1,None,2)
return even,odd
If you prefer lists then you can return list(even),list(odd).
def zigzag(seq):
return seq[::2], seq[1::2]
I just wanted to clear something. Say you have a list
list1 = list(range(200))
you can do either :
## OPTION A ##
a = list1[1::2]
b = list1[0::2]
or
## OPTION B ##
a = list1[0:][::2] # even
b = list1[1:][::2] # odd
And get have alternative elements in variable a and b.
But OPTION A is twice as fast

Categories

Resources