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)
Related
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)
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']]
I have lots of lists which contain integers as strings. e.g. scored_h0=['2','3','4']. I'm trying to convert each entry of each list from a string to an integer with the code below, but for some reason it's not working. There is no error, it's just when I print (scored_h0) (for example) after running the code below, the entries haven't been converted.
power_list = [scored_h0, scored_h1, conceded_h0, conceded_h1, scored_a0, scored_a1, conceded_a0, conceded_a1]
for list1 in power_list:
list1 = list(map(int, list1))
you are assigning a new value to the variable list1, but not mutating the actual array. If you want to mutate all these arrays you'll have to change each value in each of them, like so:
for list1 in power_list:
for i in range(len(list1)):
list1[i] = int(list1[i])
Are you looking for something like this?
scored_h0 = ['1', '2', '3']
scored_h1 = ['4', '5', '6']
power_list = [scored_h0, scored_h1]
for i in range(len(power_list)):
for j in range(len(power_list[i])):
power_list[i][j] = int(power_list[i][j])
print(scored_h0)
print(scored_h1)
It prints:
[1, 2, 3]
[4, 5, 6]
The following method do the job
def to_int(s):
for l in s:
for id, val in enumerate(l):
l[id] = int(val)
So you call it as to_int(power_list)
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...
I was reading this, but I'm not sure how to do the same for looking at the length of individual items in a list. I have the following bulky code below, and I was wondering if there's a more pythonic way to do this?
indexdf = [['1','','1',], ['1', '']]
listy = []
for ilist in indexdf:
listx = [ ]
#print ilist
for x in ilist:
#print x
if len(x) > 0:
listx.append(x)
listy.append(listx)
output
listy = [['1','1']. ['1']]
listy = [ [x for x in ilist if len(x) > 0] for ilist in indexdf]
There's a further trick since the "certain length" in your example happens to exclude precisely the empty strings:
[x for x in ilist if len(x) > 0]
could be replaced with
list(filter(None, ilist))
in Python 3 or just
filter(None, ilist)
in Python 2, or
[x for x in ilist if x]
in either of them.
You can use a nested list comprehension here. Note that empty strings will return False in a boolean context. The same is true for empty lists, tuples, etc.
>>> lst = [['1','','1',], ['1', '']]
>>> lst = [[x for x in sl if x] for sl in lst]
>>> lst
[['1', '1'], ['1']]