Producing array of which its elements are indices - python

I'm trying to remove values from a large data set that are inconsistent in my analysis.
Here is the current method i'm started with.
For example, lets say I have an array a that consist of an number of elements.
a = [30, 40, 200, 324, 8, 67, 789, 9, 567, 2143, 13]
idx = [(i,value) for i,value in enumerate(a) if value<=10]
print idx
>>> [(4, 8), (7, 9)]
How do i go about to where I can only just create an array that consist of only its indices
print idx
>>> [4, 8]

Don't include the value in the comprehension result:
idx = [i for i, v in enumerate(a) if v <= 10]

You are almost there, just only use the index:
>>> a = [30, 40, 200, 324, 8, 67, 789, 9, 567, 2143, 13]
>>> idx = [i for i, value in enumerate(a) if value<=10]
>>> idx
[4, 7]

Related

Python: nested list of lists with elements grouped by 3

From an initial list:
starting_list = [
[1,2,3, 4,5,6, 7,8,9 ],
[11,22,33, 44,55,66, 77,88,99 ],
[111,222,333, 444,555,666, 777,888,999]
]
I would like to create a new list (either using a for loop or list comprehension) whose elements are the sub-lists of starting_list, grouped by three elements and starting at every third index:
grouped_by_3_elements = [
[1,2,3, 11,22,33, 111,222,333],
[4,5,6, 44,55,66, 444,555,666],
[7,8,9, 77,88,99, 777,888,999]
]
I have managed the following, but not exactly what I am looking for
[ [1, 2, 3], [11, 22, 33], [111, 222, 333], [4, 5, 6], [44, 55, 66], [444, 555, 666], [7, 8, 9], [77, 88, 99], [777, 888, 999] ]
Thanks
••• UPDATE •••
For those who want to know what I did to arrive at my current solution:
lst = [
[1,2,3,4,5,6,7,8,9],
[11,22,33,44,55,66,77,88,99],
[111,222,333,444,555,666,777,888,999]
]
new_lst = []
for col in range(0, len(lst[0]), 3):
for row in range(3):
new_lst.append(lst[row][col:col+3])
print(new_lst)
>>> [[1, 2, 3], [11, 22, 33], [111, 222, 333], [4, 5, 6], [44, 55, 66], [444, 555, 666], [7, 8, 9], [77, 88, 99], [777, 888, 999]]
Can I somehow arrive at my desired
[
[1,2,3, 11,22,33, 111,222,333],
[4,5,6, 44,55,66, 444,555,666],
[7,8,9, 77,88,99, 777,888,999]
]
without having to run my result (new_lst) through a separate loop in order to repack ?
Changing yours just a little:
new_lst = []
for col in range(0, len(lst[0]), 3):
inner = []
for row in range(3):
inner += lst[row][col:col+3]
new_lst.append(inner)
Changing it a bit more:
new_lst = []
for col in range(0, len(lst[0]), 3):
inner = []
for row in lst:
inner += row[col:col+3]
new_lst.append(inner)
Older solution from before there was an indication for other sizes:
Similar to an ordinary transposition of a 3×3 matrix, just with slices in the rows:
a = starting_list
for i in range(3):
si = slice(i*3, i*3+3)
for j in range(i):
sj = slice(j*3, j*3+3)
a[i][sj], a[j][si] = a[j][si], a[i][sj]
You can use sub ranges in a nested comprehension:
L = [
[1,2,3, 4,5,6, 7,8,9 ],
[11,22,33, 44,55,66, 77,88,99 ],
[111,222,333, 444,555,666, 777,888,999]
]
r = [ [ n for r in L for n in r[c:c+3] ] for c in range(0,len(L[0]),3) ]
print(r)
[[1, 2, 3, 11, 22, 33, 111, 222, 333],
[4, 5, 6, 44, 55, 66, 444, 555, 666],
[7, 8, 9, 77, 88, 99, 777, 888, 999]]
end_list = []
sublist_len = min(len(lst) for lst in starting_list)
for i in range(0, sublist_len, 3):
group = []
for lst in starting_list:
group.append(lst[i:i+3])
end_list.append([y for x in group for y in x])
First we start with an empty list. We calculate the length of the sublists so we don't get any out of bounds errors indexing the sublists.
We then iterate over indexes for "chunks" of these sublists. 0, 1, 2, 3, 4, 5, etc.
For each of these chunks of indexes, we create a group by appending a slice of the list to group for each sublist. At the end of that loop, we use some list comprehension logic to "flatten" the list of lists we've created, and append that list to the end_list list.
End result?
[[1, 2, 3, 11, 22, 33, 111, 222, 333], [4, 5, 6, 44, 55, 66, 444, 555, 666], [7, 8, 9, 77, 88, 99, 777, 888, 999]]
We can also use a list comprehension to accomplish this, using the same sublist_len as before. The logic is the same as before, but more direct. for i in range(0, sublist_len, 3) will yield 0, 3, and 6. We then map each of these to a list created by the nested list comprehension.
end_list = [[x for lst in starting_list for x in lst[i:i+3]] for i in range(0, sublist_len, 3)]

Replace 0 values in list with increment numbers, starting from the highest number in that list

If I have a list
l = [0,1,2,3,4,5,0,23,34,0,45,0,21,58,98,76,68,0]
I want to replace all the 0 with an increment value starting from the highest value in the list l. So in this case the highest value is 98 so the 0 should be replace with 99,100,101,102 and 103.
This is my solution and it works fine
for ix,i in enumerate(l):
m = max(l)
if i == 0:
l[ix] = (m+1)
But I would like to know what is the best way to solve this problem.
You can use a list comphrension, incrementing via itertools.count():
>>> from itertools import count
>>> lst = [0,1,2,3,4,5,0,23,34,0,45,0,21,58,98,76,68,0]
>>> c = count(max(lst)+1)
>>> [x or next(c) for x in lst]
[99, 1, 2, 3, 4, 5, 100, 23, 34, 101, 45, 102, 21, 58, 98, 76, 68, 103]

How to get the values in the dictionary into the list form and then square them into list

I have a dictionary of values in tuple form, how to get the values in list form.
I want to get values from the tuples and create new lists and create another 3 lists with squares from them.
dictionary={1:(1,2,3),2:(3,4,5),3:(6,7,8),4:(9,10,11),5:(12,13,14)}
s=list(d.values())
d=[item for t in s for item in t]
print(d)
I used list comprehension i got this output:
[1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Using list comprehension
Expected_output:
[1,3,6,9,12],
[2,4,7,10,13],
[3,5,8,11,14],
squares**2 output above three list :
[1,9,36,81,144],
[4,16,49,100,169],
[9,25,64,121,196]
Provided with a Dictionary
First take a empty list and assign it to a variable “l”
Using list comprehension separate the values and store that in a variable
Iterate the values and append the empty list “l”
Now iterate the “l” using index values i[o], i[1], i[2] and store in various variables respectively
Using map function square the variables and store the values and print them using the list of variables
x = {
1:(1,2,3),
2:(4,5,6),
3:(7,8,9),
4:(10,11,12),
5:(13,14,15)
}
l = []
y = [i for i in x.values()]
for i in y:
l.append(i)
print(l)
m = [i[0] for i in l]
n = [i[1] for i in l]
o = [i[2] for i in l]
m1 = map(lambda i:i**2, m)
n1 = map(lambda i:i**2, n)
o1 = map(lambda i:i**2, o)
print(m)
print(list(m1))
print(n)
print(list(n1))
print(o)
print(list(o1))
you can use zip to collect the index elements of each list together, then use list comprehension to square them
dictionary={1:(1,2,3),2:(3,4,5),3:(6,7,8),4:(9,10,11),5:(12,13,14)}
list_vals = list(zip(*dictionary.values()))
squares = [[num ** 2 for num in nums] for nums in list_vals]
print(list_vals)
print(squares)
OUTPUT
[(1, 3, 6, 9, 12), (2, 4, 7, 10, 13), (3, 5, 8, 11, 14)]
[[1, 9, 36, 81, 144], [4, 16, 49, 100, 169], [9, 25, 64, 121, 196]]
Thanks to comments from #roganjosh highlighting that the dict will only be assured to be ordered if the pythong version is 3.6 or higher. If your python version is less than that you would first need to sort the values by order of the keys. Below is an example.
dictionary={2:(3,4,5),3:(6,7,8),4:(9,10,11),5:(12,13,14),1:(1,2,3)}
ordered_key_val = sorted(dictionary.items(), key=lambda items: items[0])
list_vals = list(zip(*[val for key, val in ordered_key_val]))
squares = [[num ** 2 for num in nums] for nums in list_vals]
print(list_vals)
print(squares)
You can use numpy to transpose the entire list once the values of the dictionary are obtained. You can use the below program
import numpy as np
dictionary={1:(1,2,3),2:(3,4,5),3:(6,7,8),4:(9,10,11),5:(12,13,14)}
list_out= []
for i in dictionary.keys():
list_out.append(dictionary[i])
tran_list = np.transpose(list_out)
out_list = tran_list*tran_list
Output of this is:
>>> out_list
array([[ 1, 9, 36, 81, 144],
[ 4, 16, 49, 100, 169],
[ 9, 25, 64, 121, 196]])
This is an array output! Anyway if you want it only in the list, ofcourse , you can play with it!
You can do this way:
>>> temp = list(zip(*dictionary.values()))
>>> [list(i) for i in temp]
[[1, 3, 6, 9, 12], [2, 4, 7, 10, 13], [3, 5, 8, 11, 14]]
>>> [[i**2 for i in elem] for elem in temp]
[[1, 9, 36, 81, 144], [4, 16, 49, 100, 169], [9, 25, 64, 121, 196]]
I have one dictionary here
d={1:(1,2,3),2:(4,5,6),3:(7,8,9),4:(10,11,12),5:(13,14,15)}
first I want to get values in tuple in three lists then I used list comprehension here The below code gives the tuple values in three lists
myList1 = [d [i][0] for i in (d.keys()) ]
print(myList1)
myList2 = [d [i][1] for i in (d.keys()) ]
print(myList2)
myList3 = [d [i][2] for i in (d.keys()) ]
print(myList3)
Here all the tuple values converted into list form
[1, 4, 7, 10, 13]
[2, 5, 8, 11, 14]
[3, 6, 9, 12, 15]
Now I want to squares the elements in three lists here I Used lambda expression the below code squares the elements in the lists
a1= list(map(lambda x: x**2 ,myList1))
print(a1)
a2= list(map(lambda x: x**2 ,myList2))
print(a2)
a3= list(map(lambda x: x**2 ,myList3))
print(a3)
The output is:
[1, 16, 49, 100, 169]
[4, 25, 64, 121, 196]
[9, 36, 81, 144, 225]

how to remove composite numbers from a list in python 3?

I am not able to remove composite numbers from a list in python 3 .Can you help?
Example input:
list1 = [2, 3, 6, 7, 14, 21, 23, 42, 46, 69, 138, 161, 322, 483]
Expected output:
list1 = [2, 3, 7, 23]
Thanks in advance.
You can use a list comprehension with all:
list1 = [2, 3, 6, 7, 14, 21, 23, 42, 46, 69, 138, 161, 322, 483]
new_result = [i for i in list1 if all(i%c != 0 for c in range(2, i))]
Output:
[2, 3, 7, 23]
Ajax1234's solution is correct, but instead of using range(2, i), I would add the modification that range(2, i) becomes range(2, 1+math.ceil(math.sqrt(i))), where the math module has been imported. For very large lists, this reduces the execution time since all composite numbers have factors less than or equal to 1+math.ceil(math.sqrt(i)).

Removing common values from two lists in python

Hi let's say that I have two lists in python and I want to remove common values from both lists. A potential solution is:
x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [43, 3123, 543, 76, 879, 32, 14241, 342, 2, 3, 4]
for i in x:
if i in y:
x.remove(i)
y.remove(i)
it seems correct but it is not. The reason, I guess, is because by removing an item from the list the index continues to iterate. Therefore, for two common values in the lists where the values are near each other we will be missing the later values (the code will not iterate through it).
The result would be:
>>> x
[1, 3, 5, 6, 8, 9, 10]
>>> y
[43, 3123, 543, 76, 879, 32, 14241, 342, 3]
So we are missing the value '3'.
Is the reason of that behaviour the one that I mentioned? or am I doing something else wrong?
Just slight change your code,Iterate through the copy of x it's x[:].You are modifying the list while iterating over it. So that's why you are missing value 3
for i in x[:]:
if i in y:
x.remove(i)
y.remove(i)
And alternative method
x,y = [i for i in x if i not in y],[j for j in y if j not in x]
You can also use difference of set objects.
a = list(set(y) - set(x))
b = list(set(x) - set(y))
z=[i for i in x if i not in y]
w=[i for i in y if i not in x]
x=z
y=w
That should do the trick? It's a bit less memory efficient.
If you use numpy, then all you need is:
x, y = np.setdiff1d(x, y), np.setdiff1d(y, x)
and if you don't want to use numpy:
x, y = list(set(x).difference(y)), list(set(y).difference(x))
I personally think Python's set data type is the way to go:
You can do something like this:
>>> x = [1, 2, 3, 4, 5, 6, 7, 8]
>>> y = [43, 3123, 543, 76, 879, 32, 14241, 342, 2, 3, 4]
>>> sx = set(x)
>>> sy = set(y)
>>> sx.union(sy)
set([1, 2, 3, 4, 5, 6, 7, 8, 32, 43, 76, 342, 543, 879, 3123, 14241])
Or you can reduce it to a one liner:
list(set(x).union(set(y)))
You can use set method to remove elements in list.
s1=input()
s2=input()
str=list(set(s1).symmetric_difference(set(s2)))
print(str)

Categories