Related
I am new to Python and I am quite struggling with how to split the array according to user-defined rules.
Say I have an array with elements inside:
A=[0,1,2,6,7,8,9,14,15,16]
I want to use python to split the above array into several groups.
So if the interval between the current element and the next element is smaller than 2, then they belong to one group, else belong to a different group.
So essentially I want my new array to look like this:
B = [[0,1,2] [6,7,8,9] [14,15,16]]
I was thinking to use for loop and loop through array A, but not quite sure how to do so....
A basic for loop works
a=[0,1,2,6,7,8,9,14,15,16]
g = []
tmp = []
for i in range(len(a)):
tmp.append(a[i])
if i==len(a)-1 or a[i+1] - a[i] >= 2:
g.append(tmp)
tmp = []
print(g)
Output
[[0, 1, 2], [6, 7, 8, 9], [14, 15, 16]]
You can just iterate over your A list, starting a new sub-list in B each time the difference between the current value in A and the previous one is more than 1:
A=[0,1,2,6,7,8,9,14,15,16]
B = [[A[0]]]
Bindex = 0
last = A[0]
for i in range(1, len(A)):
if A[i] - last >= 2:
B.append([A[i]])
Bindex += 1
else:
B[Bindex].append(A[i])
last = A[i]
print(B)
Output:
[[0, 1, 2], [6, 7, 8, 9], [14, 15, 16]]
A = [0, 1, 2, 6, 7, 8, 9, 14, 15, 17]
temp = []
B = []
for index, value in enumerate(A):
if index != 0 and value - A[index-1] >= 2:
B.append(temp)
temp = [value]
continue
temp.append(value)
B.append(temp)
How the code works:
Enumerate takes a list and makes it into tuple format.
So enumerate will make your list look like this:
(0, 0) (1, 1) (2, 2) (3, 6) (4, 7) etc....
index != 0 condition ensures that you do not do access an element before 0 in the next part of the if statement. I.e. A[index-1], if index was 0 then this would be A[0-1] = A[-1] = NOT what we want (because this will give the last element)
temp = [value] makes a new list. I.e. the next list to be appended into B.
continue goes back to the for loop skipping any code below it for that particular iteration.
I think an elegant and much faster way is to use itertools.groupby like:
from itertools import groupby
from operator import itemgetter
A=[0,1,2,6,7,8,9,14,15,16]
B = [list(map(itemgetter(1), group)) for key, group in groupby(enumerate(A), lambda x: x[0] - x[1])]
A = [0, 1, 2, 6, 7, 8, 9, 14, 15, 17]
B = []
start = 0
end = 0
for i, val in enumerate(A):
if i != 0 and val - A[i-1] >= 2:
B.append(A[start:end])
start = end
end += 1
B.append(A[start:end])
I want to store even number and odd number in a separate list. But, here I am facing a unique problem. I am able to store it in sets but not in lists. Is there a way wherein I can store these in a List without repetition.
I have tried this in Jupyter notebook
list_loop=[1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]
for i in list_loop:
if i % 2 == 0 :
list_even = list_even + [i]
else:
list_odd = list_odd + [i]
print(set(list_even))
print(set(list_odd))
Expected output:
[2,4,6,8,10,12]
[1,3,5,7,9,11,13,17,51]
Define list_odd and list_even as lists and don't convert them to sets before printing. Note that you can use list comprehension to fill list_odd and list_even:
list_odd = []
list_even = []
list_loop=[1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]
list_odd = [elem for elem in list_loop if elem % 2 != 0]
list_even = [elem for elem in list_loop if elem % 2 == 0]
print(list_even)
print(list_odd)
Output:
[2, 4, 6, 8, 10, 12, 4, 6]
[1, 3, 5, 7, 9, 11, 13, 1, 1, 51, 17]
Edit: for uniqueness, turn list_loop into a set:
list_loop=set([1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,])
Output:
[2, 4, 6, 8, 10, 12]
[1, 3, 5, 7, 9, 11, 13, 17, 51]
Use a comprehension
>>> list_loop=[1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]
>>> print(list(set(_ for _ in list_loop if _ % 2)))
[1, 3, 5, 7, 9, 11, 13, 17, 51]
Similarly for even numbers.
There are a couple of ways you could do this. You could use the OrderedDict in the collections library, or you could just sort the set and get a list,
...
print(sorted(set(list_even)))
print(sorted(set(list_odd)))
Also, I would personally create those lists using a set comprehension
list_even = sorted({x for x in list_loop if x % 2 == 0})
list_odd = sorted({x for x in list_loop if x % 2 == 1})
You can solve this using a list comprehension with a filter condition - but you then iterate your list twice.
By using a simple for loop you only need to touch any number once at it will conserve the original order - what putting your numbers through a set might not do - order in a set is not guaranteed:
Keep a set of seen numbers, only add anything if your current number was not yet seen.
list_loop = [1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]
list_even = []
list_odd = []
seen = set()
trick = [list_even, list_odd] # even list is at index 0, odd list at index 1
for i in list_loop:
if i in seen:
continue
else:
seen.add(i)
# the trick eliminates the need for an if-clause
trick[i%2].append(i) # you use i%2 to get either the even or odd index
print(list_even)
print(list_odd)
Output:
[2, 4, 6, 8, 10, 12]
[1, 3, 5, 7, 9, 11, 13, 51, 17]
You can apply the list function to your set object in order to
convert it to a list.
list_from_set = list(set(list_even))
>>> print(list_from_set)
[2, 4, 6, 8, 10, 12]
Say I had a list:
lis = [4, 8, 2, 4, 6]
And I want to go through each value in the list and double it but If I come across the number 2, after I double it I should skip the next number and only double the on after. For example, in the end my list should look like this.
lis = [8, 16, 4, 4, 12]
Can this be possible with a for loop?
The algorithm boils down what number you are using to double the items in the list (1 or 2). Here is my take on this problem:
lis = [4, 8, 2, 4, 6]
def double_with_my_condition(l, doubler=2):
for i in l:
yield i * doubler
if i == 2:
doubler = 1
continue
doubler = 2
new_lis = [*double_with_my_condition(lis)]
print(new_lis)
Outputs:
[8, 16, 4, 4, 12]
I wrote out a really simple solution that should be easy to understand since it appears you are a beginner
lis = [4, 8, 2, 4, 6]
new_lis = []
i = 0
while (i < len(lis)):
new_lis.append(lis[i] * 2)
if (lis[i] == 2):
if (i+1 < len(lis)):
new_lis.append(lis[i+1])
i = i+1
i = i+1
print(new_lis)
This creates a new list, loops through the old list, appends the doubled value to the new list, skips a number if the value at the index is 2.
This will work!
Method-1:
lis = [4, 8, 2, 4, 6]
for i in range(len(lis)-1, -1, -1):
if(lis[i-1] == 2):
continue
else:
lis[i] = lis[i]*2
lis
Method-2:
lis1 = [4, 8, 2, 4, 6]
indices = [i+1 for i, x in enumerate(lis1) if x == 2] #Storing indices of next to 2
lis2 = [i*2 for i in lis1]
for i in indices:
lis2[i] = lis1[i] # replacing the previous values
print(lis2)
You can also use list comprehensions
lis = [4, 8, 2, 4, 6]
print([lis[x] if lis[x - 1] == 2 else lis[x] * 2 for x in range(len(lis))])
I am trying to write a program that takes an input number T(number of test cases), and then asks for the numbers N.
This is my code:
T = int(raw_input())
L = [int(raw_input()) for i in range(T)]
L1 = []
for i in range(0,L[i]):
if (i%3 == 0 or i%5 ==0):
L1.append(i)
print L1
Input: 2 10 20
Output: [0, 3, 5, 6, 9, 10, 12, 15, 18]
I would like the output to be of the following format:
[[0, 3, 5, 6, 9], [0, 3, 5, 6, 9, 10, 12, 15, 18]]
Here [0, 3, 5, 6, 9] is the list that has elements with both multiples of 3 and 5 for number 10
[0, 3, 5, 6, 9, 10, 12, 15, 18] is the list that has elements with both multiples of 3 and 5 for number 20
I am new to python. kindly let me know how I should proceed on this.
The following will produce a list of lists containing all the multiples of 3 and 5 that are less than the given number.
L = [10,20]
L1 = []
for i in L:
L2 = [] # initialize a new list
for j in range(i):
if not (j%3 and j%5): # use falsy values and DeMorgan's Law
L2.append(j) # append to this list
if L2: # use this if you don't want to keep empty lists
L1.append(L2)
>>> L1
[[0, 3, 5, 6, 9], [0, 3, 5, 6, 9, 10, 12, 15, 18]]
I think what you want is splitting a list by input values. Hope it helps
num = int(raw_input())
upperBounds= [int(raw_input()) for i in range(num)]
res= []
for upperBound in upperBounds:
res.append([i for i in range(0,upperBound) if not (i % 3 and i % 5)])
output:
2
10
20
[[0, 3, 5, 6, 9], [0, 3, 5, 6, 9, 10, 12, 15, 18]]
This can be easily done by applying appropriate logic:
if the element at index 0 we have to iterate from 0 to that element
else we have to iterate form L[index-1] to L[index]
T = int(raw_input())
L = [int(raw_input()) for i in range(T)]
L1 = []
for j in xrange(len(L)):
temp = []
get = 0 if not j else L[j-1]
# if j==0:
# get = 0
# else:
# get = L[j-1]
for i in range(get, L[j]):
if (i%3 == 0 or i%5 ==0):
temp.append(i)
L1.append(temp)
print L1
>>> [[0, 3, 5, 6, 9], [10, 12, 15, 18]]
Or a more Pythonic and compacted version may look like:
T = int(raw_input())
L = [int(raw_input()) for i in range(T)]
L1 = []
for j in xrange(len(L)):
get = 0 if not j else L[j-1]
L1.append([i for i in range(get, L[j]) if (i%3 == 0 or i%5 ==0)])
print L1
You can simply generate a list of multiples with range(l,u,s) with l the lower bounds, u the upper bound and d the step.
Now if we want to generate multiples of i for a given range, we can use the following function:
def multiples(factor, lower, upper) :
return set(range(lower+(factor-lower)%factor,upper,factor))
We thus manipulate the lower bound as lower+(factor-lower)%factor in order to search - in constant time - the first multiple that is greater than or equal to lower.
Next we need to multiples of 3 and 5:
def multiples35(lower, upper):
return sorted(list(multiples(3,lower,upper)|multiples(5,lower,upper)))
Now we only need to iterate over the list of values and generate the list of multiples for each two numbers:
def func(B):
return [multiples35(0,upper) for upper in B]
Or as full code:
import sets
def multiples(factor, lower, upper) :
return set(range(lower+(factor-lower)%factor,upper,factor))
def multiples35(lower, upper):
return sorted(list(multiples(3,lower,upper)|multiples(5,lower,upper)))
def func(B):
return [multiples35(0,upper) for upper in B]
The main function reads then:
T = int(raw_input())
B = [int(raw_input()) for i in range(T)]
print func(B)
Title is definitely confusing, so here's an example: Say I have a list of values [1,2,3,2,1,4,5,6,7,8]. I want to remove between the two 1s in the list, and by pythonic ways it will also end up removing the first 1 and output [1,4,5,6,7,8]. Unfortunately, due to my lack of pythonic ability, I have only been able to produce something that removes the first set:
a = [1,2,3,2,1,4,5,6,7]
uniques = []
junks = []
for value in a:
junks.append(value)
if value not in uniques:
uniques.append(value)
for value in uniques:
junks.remove(value)
for value in junks:
a.remove(value)
a.remove(value)
a[0] = 1
print(a)
[1,4,5,6,7]
Works with the first double occurrence and will not work with the next occurrence in a larger list. I have an idea which is to remove between the index of the first occurrence and the second occurrence which will preserve the second and not have me do some dumb thing like a[0] = 1 but I'm really not sure how to implement it.
Would this do what you asked:
a = [1, 2, 3, 2, 1, 4, 5, 6, 7, 8]
def f(l):
x = l.copy()
for i in l:
if x.count(i) > 1:
first_index = x.index(i)
second_index = x.index(i, first_index + 1)
x = x[:first_index] + x[second_index:]
return x
So the output of f(a) would be [1, 4, 5, 6, 7, 8] and the output of f([1, 2, 3, 2, 1, 4, 5, 6, 7, 8, 7, 6, 5, 15, 16]) would be [1, 4, 5, 15, 16].
if you want to find unique elements you can use set and list
mylist = list(set(mylist))
a = [1, 2, 3, 2, 1, 4, 5, 6, 7, 8, 7, 6, 5, 15, 16]
dup = [x for x in a if a.count(x) > 1] # list of duplicates
while dup:
pos1 = a.index(dup[0])
pos2 = a.index(dup[0], pos1+1)
a = a[:pos1]+a[pos2:]
dup = [x for x in a if a.count(x) > 1]
print a #[1, 4, 5, 15, 16]
A more efficient solution would be
a = [1, 2, 3, 2, 1, 4, 5, 6, 7, 8, 7, 6, 5, 15, 16]
pos1 = 0
while pos1 < len(a):
if a[pos1] in a[pos1+1:]:
pos2 = a.index(a[pos1], pos1+1)
a = a[:pos1]+a[pos2:]
pos1 += 1
print a #[1, 4, 5, 15, 16]
(This probably isn't the most efficient way, but hopefully it helps)
Couldn't you just check if something appears twice, if it does you have firstIndex, secondIndex, then:
a=[1,2,3,4,5,1,7,8,9]
b=[]
#do a method to get the first and second index of the repeated number then
for index in range(0, len(a)):
print index
if index>firstIndex and index<secondIndex:
print "We removed: "+ str(a[index])
else:
b.append(a[index])
print b
The output is [1,1,7,8,9] which seems to be what you want.
To do the job you need:
the first and the last position of duplicated values
all indexes between, to remove them
Funny thing is, you can simply tell python to do this:
# we can use a 'smart' dictionary, that can construct default value:
from collections import defaultdict
# and 'chain' to flatten lists (ranges)
from itertools import chain
a = [1, 2, 3, 2, 1, 4, 5, 6, 7]
# build dictionary where each number is key, and value is list of positions:
index = defaultdict(list)
for i, item in enumerate(a):
index[item].append(i)
# let's take first only and last index for non-single values
edges = ((pos[0], pos[-1]) for pos in index.values() if len(pos) > 1)
# we can use range() to get us all index positions in-between
# ...use chain.from_iterable to flatten our list
# ...and make set of it for faster lookup:
to_remove = set(chain.from_iterable(range(start, end)
for start, end in edges))
result = [item for i, item in enumerate(a) if i not in to_remove]
# expected: [1, 4, 5, 6, 7]
print result
Of course you can make it shorter:
index = defaultdict(list)
for i, item in enumerate([1, 2, 3, 2, 1, 4, 5, 6, 7]):
index[item].append(i)
to_remove = set(chain.from_iterable(range(pos[0], pos[-1])
for pos in index.values() if len(pos) > 1))
print [item for i, item in enumerate(a) if i not in to_remove]
This solution has linear complexity and should be pretty fast. The cost is
additional memory for dictionary and set, so you should be careful for huge data sets. But if you have a lot of data, other solutions that use lst.index will choke anyway, because they are O(n^2) with a lot of dereferencing and function calls.