Related
Using Python, write a function, which takes three lists as input: left, middle and right. These lists will already have been sorted into decreasing order. Merge these three lists into one list, with elements sorted from largest to smallest. Return the resulting list. Not allowed to use sort().
def Merge3Way(left,middle,right):
"""Takes three lists that are sorted in decreasing order and merges them into
one list ordered largest to smallest"""
res = []
while len(left) > 0 and len(right)> 0 and len(middle)>0:
if left[0] > right[0] and left[0] > middle[0]:
res.append(left.pop(0))
elif middle[0] > right[0] and middle[0] > left[0]:
res.append(middle.pop(0))
else:
res.append(right.pop(0))
if left:
res.extend(left)
if middle:
res.extend(middle)
if right:
res.extend(right)
return res
This is the code that I have made.
But when the input is [3,2,1],[6,5,4],[9,8,7], the result is not correct.
What’s the problem in my code? Or any other ways to make the function?
This should help you!
def Merge3Way(left, middle, right):
"""Takes three lists that are sorted in decreasing order and merges them into
one list ordered largest to smallest"""
# We get one big list, for example [1, 2] + [3, 4] = [1, 2, 3, 4]
L = left + middle + right
result = []
while len(L) > 0:
el = L[0]
# by default, we think that it should be inserted at the end of
index = len(result)
# if there is a smaller item in the list, then insert before it
for k in range(len(result)):
if el > result[k]:
index = k
break
result.insert(index, el)
del L[0]
return result
l1 = [3,2,1]
l2 = [6,5,4]
l3 = [9,8,7]
Merge3Way(l1, l2, l3)
# [9, 8, 7, 6, 5, 4, 3, 2, 1]
did not initially see what was necessary in the order of decreasing =)
Following code based upon:
1st merging left and right to get an intermediate result
Next merging intermediate result with middle
Avoid use of lst.pop(0) since this i an expensive operation i.e. O(len(lst))
Code
def Merge3Way(left,middle,right):
"""Takes three lists that are sorted in decreasing order and merges them into
one list ordered largest to smallest"""
# Step 1: Merge left & right
res_1 = []
left_ind, right_ind = 0, 0
while left_ind < len(left) and right_ind < len(right):
if left[left_ind] > right[right_ind]:
res_1.append(left[left_ind])
left_ind += 1
else:
res_1.append(right[right_ind])
right_ind += 1
for left_ind in range(left_ind, len(left)):
res_1.append(left[left_ind])
for right_ind in range(right_ind, len(right)):
res_1.append(left[right_index])
# Step 2: Merge intermediate result and middle
res_2 = []
res_ind, middle_ind = 0, 0
while res_ind < len(res_1) and middle_ind < len(middle):
if res_1[res_ind] > middle[middle_ind]:
res_2.append(res_1[res_ind])
res_ind += 1
else:
res_2.append(middle[middle_ind])
middle_ind += 1
for res_ind in range(res_ind, len(res_1)):
res_2.append(res_1[res_ind])
for middle_index in range(middle_ind, len(middle)):
res_2.append(middle[middle_ind])
return res_2
Test
result = Merge3Way([3,2,1],[6,5,4],[9,8,7])
print(result)
# Output: [9, 8, 7, 6, 5, 4, 3, 2, 1]
You could use a dict to manage dups and take the repetitive max of the keys:
def merge(l,m,r):
rtr=[]
cnt={}
# first count the entries in all three lists as merged:
for x in l+m+r:
cnt[x]=cnt.get(x,0)+1
# now take the max of those entries and delete one by one
# exit when all entries of cnt have been used
while cnt:
x=max(cnt.keys())
if cnt[x]>1:
cnt[x]=cnt[x]-1
else:
del cnt[x]
rtr.append(x)
return rtr
Test it:
>>> merge([3,2,1],[6,5,4],[9,8,7])
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> merge([3,2,1],[6,5,3],[9,8,7])
[9, 8, 7, 6, 5, 3, 3, 2, 1]
This method handles lists that are not the same length as well:
>>> merge([3,2,1],[6,5,4],[9,8,7,5,0])
[9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 0]
Or if the sublists are not sorted at all:
>>> merge([3,2,1],[6,5,4],[9,8,7])
[9, 8, 7, 6, 5, 4, 3, 2, 1]
The statement that each of the three list is already sorted is somewhat misleading; you still have to reorder the entire merged list since the lists may have duplicates or interweaving values. It is faster and easier to ignore the sorting of the three lists and just concentrate on the most efficient way to order the combination of the three.
I have a random list like this
X = [0, 1, 5, 6, 7, 10, 15]
and need to find and replace every climbing sequence with its average.
In the end it should look like this:
X = [0, 6, 10, 15] #the 0 and 1 to 0; and the 5,6,7 to 6
I tried to find the sequence by subtracting the second value from the first like this:
y = 0
z = []
while X[y +1] -X[y] == 1:
z.append(X[y])
y = y +1
And now I dont know how to delete for example 5,6 and 7 and replace it with the average 6.
You can use itertools.groupby on the list with a key function that returns each item's difference with an incremental counter:
from itertools import groupby, count
from statistics import mean
X = [0, 1, 5, 6, 7, 10, 15]
c = count()
X = [int(mean(g)) for _, g in groupby(X, key=lambda i: i - next(c))]
X becomes:
[0, 6, 10, 15]
You can iterate and group in the same list each climbing sequence for then taking the mean.
>>> res = [[x[0]]]
>>> for i in range(1, len(x)):
... if x[i] == x[i-1] + 1:
... res[-1].append(x[i])
... else:
... res.append([x[i]]
>>> res
[[0, 1], [5, 6, 7], [10], [15]]
>>> [int(sum(l)/len(l)) for l in res]
[0, 6, 10, 15]
Here's a starting technique: make a new list that's the difference of adjacent elements in the list:
diff = [X[i] - X[i-1] for i in range(1, len(X)) ]
There are more "Pythonic" ways to do this, but I want to make sure this is accessible to newer programmers.
You now have diff as
[1, 4, 1, 1, 3, 5]
Where you have a 1 in diff, you have a climbing pair in X. Iterate through diff to find a sequence of 1 values. Where you find this, take the slice of X that corresponds to the 1 values. The middle element of that slice is your mean.
If the value is not 1, then you simply take the corresponding element of X, as you've been doing.
append the identified values to z, and there's your desired result.
Can you take it from there?
Not really to answer the question, which is a fairly basic CS 101 question that people should try to figure out themselves, but what I noticed about the nice answer of #blhsing was that it appeared fairly slow. I found that mean() is incredibly slow!
from itertools import groupby, count
from statistics import mean
from timeit import timeit
def generate_1step_seq1(xs):
result = []
n = 0
while n < len(xs):
# sequences with step of 1 only
if not result or xs[n] == result[-1] + 1:
result += [xs[n]]
else:
# int result, rounding down
yield sum(result) // len(result)
result = [xs[n]]
n += 1
if result:
yield sum(result) // len(result)
def generate_1step_seq2(xs):
c = count()
return [int(sum(xs) // len(xs)) for xs in [list(g) for _, g in groupby(xs, key=lambda i: i - next(c))]]
def generate_1step_seq3(xs):
c = count()
return [int(mean(g)) for _, g in groupby(xs, key=lambda i: i - next(c))]
values = [0, 1, 5, 6, 7, 10, 15]
print(list(generate_1step_seq1(values)))
print(generate_1step_seq2(values))
print(generate_1step_seq3(values))
print(timeit(lambda: list(generate_1step_seq1(values)), number=10000))
print(timeit(lambda: list(generate_1step_seq2(values)), number=10000))
print(timeit(lambda: list(generate_1step_seq3(values)), number=10000))
Initially I figured that was probably due to the tiny list size, but even for large lists, mean() is horribly slow. Anyone happen to know why? It appears due to the very safe nature of statistics _sum, trying to avoid float rounding errors?
Im trying to write a code to say that if a condition is met of a value in a list then in the next iteration make that value a different one. the problem is i dont know what the code is to tell python to do it in the next iteration
my code.
list_ = [4, 5, 6, 6, 4, 5]
for i in range(0, 10):
for j in range(0, len(list_)):
if list_[j] == 5:
if list_[(j-1) % len(list)] == 4:
list_[j][i+1] = 3
but this doesnt work as i get a int' object does not support item assignment error. also i know some of the indentation maybe wrong its because i struggled to put them into here
Your exact expected result is not so clear, but anyway I suggest you this solution that seems solving your problem:
list_ = [4, 5, 6, 5, 4, 5]
for j in range(0, len(list_)):
if (list_[j] == 5) and (list_[j-1] == 4):
list_[j] = 3
print(list_)
Note: on the first iteration, list_[j-1] refers to the last element of the list.
Perhaps this will suffice, building a new list according to your logic:
list_ = [4, 5, 6, 6, 4, 5]
lst = []
for i, x in enumerate(list_):
if (x == 5) and (list_[i-1] == 4) and (i-1 > 0):
lst.append(3)
else:
lst.append(x)
lst
# [4, 3, 6, 6, 4, 3]
As a list comprehension:
[3 if (x == 5) and (list_[i-1] == 4) and (i-1 > 0) else x for i, x in enumerate(list_)]
# [4, 3, 6, 6, 4, 3]
The (i-1 > 0) prevents searching the end if 5 is in the first position.
Three main pythonic themes of this implementation:
create a new iterable instead of mutating it
enumerate an iterable to reduce the use of indices
use booleans to chain conditionals rather than nesting conditionals
def merge (l1,l2):
if l1 and l2:
if l1 == [] and l2 == []:
return []
if l1[0] > l2[0]:
l1, l2 = l2, l1
return [l1[0]] + merge(l1[1:], l2)
return l1 + l2
def sort(l):
x = len(l) / 2
x = int(x)
y = merge(l[0:x], l[x+1:])
return y
I need to write a recursive function named sort; it is passed any unordered list (all int or all str) and it returns a new list that contains every value from its argument list, but in sorted/non-descending order. But I cannot call any of Python’s functions/methods that perform sorting.
also, For any list that has at least 2 values, I have to break the list in half and recursively call sort to sort each smaller list, I have to use the merge function, written above, to merge these two sorted lists returned from these recursive calls
merge is a function to combine and sort two list
merge([1,3,5,8,12],[2,3,6,7,10,15]) returns [1,2,3,3,5,6,7,8,10,12,15].
For example, calling sort([4,5,3,1,6,7,2]) would call sort recursively on the lists [4,5,3] and [1,6,7,2]), returning the lists [3,4,5] and [1,2,6,7] respectively, which when merged would return the list [1,2,3,4,5,6,7].
My function got the following error
39 *Error: sort([1,2,3,4,5,6,7]) -> [1, 2, 3, 5, 6, 7] but should -> [1, 2, 3, 4, 5, 6, 7]
40 *Error: sort([7,6,5,4,3,2,1]) -> [3, 2, 1, 7, 6, 5] but should -> [1, 2, 3, 4, 5, 6, 7]
41 *Error: sort([4,5,3,1,2,7,6]) -> [2, 4, 5, 3, 7, 6] but should -> [1, 2, 3, 4, 5, 6, 7]
42 *Error: sort([1,7,2,6,3,5,4]) -> [1, 3, 5, 4, 7, 2] but should -> [1, 2, 3, 4, 5, 6, 7]
What is wrong with me sort method? can someone help me to fix it? thanks in advance.
Three problems:
Your y = merge(l[0:x], l[x+1:]) loses l[x], make it y = merge(l[:x], l[x:]).
It doesn't sort the halves, so make it y = merge(sort(l[:x]), sort(l[x:])).
You have no base case, stopping the recursion when there's nothing to do.
Corrected and simplified a bit:
def sort(l):
if len(l) <= 1:
return l
x = len(l) // 2
return merge(sort(l[:x]), sort(l[x:]))
The // is integer division so you don't need the extra int(...). And no point in creating that y variable.
Btw, the if l1 == [] and l2 == []: test inside if l1 and l2: is pointless (if l1 and l2 were [], you wouldn't get inside the if l1 and l2: block in the first place), so you can remove it.
One more thing: While your merge function isn't wrong, it's slow. Every l1[1:] takes time proportional to the length of l1. You'd better uses indexes, like for example in Huy Vo's answer.
Ok basically everything you're doing is redundant.
list1 = [1,3,5,8,12]
list2 = [2,3,6,7,10,15]
list3 = list1 + list2 # Merges lists
list3_sorted = sorted(list3) # Sorts them
Also a little bonus, if you have a list of lists or tuples and you want to sort by an index of each of those
from operator import itemgetter
list = [(2,6), (3,4)]
list_sorted = sorted( list, key=itemgetter(1) ) # Will sort by index 1 of each item.
Edit: I now realise that you can't use any built in functions, give me a little to mess around and see if I can figure something out
What you need is a merge sort, I believe there are multiple merge sort pseudocodes on the internet.
Anyway, here is a version of mine in Python 3:
def mergesort(lst):
if len(lst) < 2:
return lst
else:
middle = len(lst) // 2
# recursion, baby
left_half = mergesort(lst[:middle])
right_half = mergesort(lst[middle:])
return merge(left_half, right_half)
def merge(left, right):
result = []
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
elif left[i] > right[j]:
result.append(right[j])
j += 1
result += left[i:] + right[j:]
return result
I have a list with duplicate elements:
list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
I have used the above code to find the duplicate elements in the list_a. I don't want to remove the elements from list.
But I want to use for loop here.
Normally C/C++ we use like this I guess:
for (int i=0;i<=list_a.length;i++)
for (int j=i+1;j<=list_a.length;j++)
if (list_a[i]==list_a[j])
print list_a[i]
how do we use like this in Python?
for i in list_a:
for j in list_a[1:]:
....
I tried the above code. But it gets solution wrong. I don't know how to increase the value for j.
Just for information, In python 2.7+, we can use Counter
import collections
x=[1, 2, 3, 5, 6, 7, 5, 2]
>>> x
[1, 2, 3, 5, 6, 7, 5, 2]
>>> y=collections.Counter(x)
>>> y
Counter({2: 2, 5: 2, 1: 1, 3: 1, 6: 1, 7: 1})
Unique List
>>> list(y)
[1, 2, 3, 5, 6, 7]
Items found more than 1 time
>>> [i for i in y if y[i]>1]
[2, 5]
Items found only one time
>>> [i for i in y if y[i]==1]
[1, 3, 6, 7]
Use the in operator instead of calling __contains__ directly.
What you have almost works (but is O(n**2)):
for i in xrange(len(list_a)):
for j in xrange(i + 1, len(list_a)):
if list_a[i] == list_a[j]:
print "duplicate:", list_a[i]
But it's far easier to use a set (roughly O(n) due to the hash table):
seen = set()
for n in list_a:
if n in seen:
print "duplicate:", n
else:
seen.add(n)
Or a dict, if you want to track locations of duplicates (also O(n)):
import collections
items = collections.defaultdict(list)
for i, item in enumerate(list_a):
items[item].append(i)
for item, locs in items.iteritems():
if len(locs) > 1:
print "duplicates of", item, "at", locs
Or even just detect a duplicate somewhere (also O(n)):
if len(set(list_a)) != len(list_a):
print "duplicate"
You could always use a list comprehension:
dups = [x for x in list_a if list_a.count(x) > 1]
Before Python 2.3, use dict() :
>>> lst = [1, 2, 3, 5, 6, 7, 5, 2]
>>> stats = {}
>>> for x in lst : # count occurrences of each letter:
... stats[x] = stats.get(x, 0) + 1
>>> print stats
{1: 1, 2: 2, 3: 1, 5: 2, 6: 1, 7: 1} # filter letters appearing more than once:
>>> duplicates = [dup for (dup, i) in stats.items() if i > 1]
>>> print duplicates
So a function :
def getDuplicates(iterable):
"""
Take an iterable and return a generator yielding its duplicate items.
Items must be hashable.
e.g :
>>> sorted(list(getDuplicates([1, 2, 3, 5, 6, 7, 5, 2])))
[2, 5]
"""
stats = {}
for x in iterable :
stats[x] = stats.get(x, 0) + 1
return (dup for (dup, i) in stats.items() if i > 1)
With Python 2.3 comes set(), and it's even a built-in after than :
def getDuplicates(iterable):
"""
Take an iterable and return a generator yielding its duplicate items.
Items must be hashable.
e.g :
>>> sorted(list(getDuplicates([1, 2, 3, 5, 6, 7, 5, 2])))
[2, 5]
"""
try: # try using built-in set
found = set()
except NameError: # fallback on the sets module
from sets import Set
found = Set()
for x in iterable:
if x in found : # set is a collection that can't contain duplicate
yield x
found.add(x) # duplicate won't be added anyway
With Python 2.7 and above, you have the collections module providing the very same function than the dict one, and we can make it shorter (and faster, it's probably C under the hood) than solution 1 :
import collections
def getDuplicates(iterable):
"""
Take an iterable and return a generator yielding its duplicate items.
Items must be hashable.
e.g :
>>> sorted(list(getDuplicates([1, 2, 3, 5, 6, 7, 5, 2])))
[2, 5]
"""
return (dup for (dup, i) in collections.counter(iterable).items() if i > 1)
I'd stick with solution 2.
You can use this function to find duplicates:
def get_duplicates(arr):
dup_arr = arr[:]
for i in set(arr):
dup_arr.remove(i)
return list(set(dup_arr))
Examples
print get_duplicates([1,2,3,5,6,7,5,2])
[2, 5]
print get_duplicates([1,2,1,3,4,5,4,4,6,7,8,2])
[1, 2, 4]
If you're looking for one-to-one mapping between your nested loops and Python, this is what you want:
n = len(list_a)
for i in range(n):
for j in range(i+1, n):
if list_a[i] == list_a[j]:
print list_a[i]
The code above is not "Pythonic". I would do it something like this:
seen = set()
for i in list_a:
if i in seen:
print i
else:
seen.add(i)
Also, don't use __contains__, rather, use in (as above).
The following requires the elements of your list to be hashable (not just implementing __eq__ ).
I find it more pythonic to use a defaultdict (and you have the number of repetitions for free):
import collections
l = [1, 2, 4, 1, 3, 3]
d = collections.defaultdict(int)
for x in l:
d[x] += 1
print [k for k, v in d.iteritems() if v > 1]
# prints [1, 3]
Using only itertools, and works fine on Python 2.5
from itertools import groupby
list_a = sorted([1, 2, 3, 5, 6, 7, 5, 2])
result = dict([(r, len(list(grp))) for r, grp in groupby(list_a)])
Result:
{1: 1, 2: 2, 3: 1, 5: 2, 6: 1, 7: 1}
It looks like you have a list (list_a) potentially including duplicates, which you would rather keep as it is, and build a de-duplicated list tmp based on list_a. In Python 2.7, you can accomplish this with one line:
tmp = list(set(list_a))
Comparing the lengths of tmp and list_a at this point should clarify if there were indeed duplicate items in list_a. This may help simplify things if you want to go into the loop for additional processing.
You could just "translate" it line by line.
c++
for (int i=0;i<=list_a.length;i++)
for (int j=i+1;j<=list_a.length;j++)
if (list_a[i]==list_a[j])
print list_a[i]
Python
for i in range(0, len(list_a)):
for j in range(i + 1, len(list_a))
if list_a[i] == list_a[j]:
print list_a[i]
c++ for loop:
for(int x = start; x < end; ++x)
Python equivalent:
for x in range(start, end):
Just quick and dirty,
list_a=[1,2,3,5,6,7,5,2]
holding_list=[]
for x in list_a:
if x in holding_list:
pass
else:
holding_list.append(x)
print holding_list
Output [1, 2, 3, 5, 6, 7]
Using numpy:
import numpy as np
count,value = np.histogram(list_a,bins=np.hstack((np.unique(list_a),np.inf)))
print 'duplicate value(s) in list_a: ' + ', '.join([str(v) for v in value[count>1]])
In case of Python3 and if you two lists
def removedup(List1,List2):
List1_copy = List1[:]
for i in List1_copy:
if i in List2:
List1.remove(i)
List1 = [4,5,6,7]
List2 = [6,7,8,9]
removedup(List1,List2)
print (List1)
Granted, I haven't done tests, but I guess it's going to be hard to beat pandas in speed:
pd.DataFrame(list_a, columns=["x"]).groupby('x').size().to_dict()
You can use:
b=['E', 'P', 'P', 'E', 'O', 'E']
c={}
for i in b:
value=0
for j in b:
if(i == j):
value+=1
c[i]=value
print(c)
Output:
{'E': 3, 'P': 2, 'O': 1}
Find duplicates in the list using loops, conditional logic, logical operators, and list methods
some_list = ['a','b','c','d','e','b','n','n','c','c','h',]
duplicates = []
for values in some_list:
if some_list.count(values) > 1:
if values not in duplicates:
duplicates.append(values)
print("Duplicate Values are : ",duplicates)
Finding the number of repeating elements in a list:
myList = [3, 2, 2, 5, 3, 8, 3, 4, 'a', 'a', 'f', 4, 4, 1, 8, 'D']
listCleaned = set(myList)
for s in listCleaned:
count = 0
for i in myList:
if s == i :
count += 1
print(f'total {s} => {count}')
Try like this:
list_a=[1,2,3,5,6,7,5,2]
unique_values = []
duplicates = []
for i in list_a:
if i not in unique_values:
unique_values.append(i)
else:
found = False
for x in duplicates:
if x.get("key") == i:
found = True
if found:
x["occurrence"] += 1
else:
duplicates.append({
"key": i,
"occurrence": 1
})
some_string= list(input("Enter any string:\n"))
count={}
dup_count={}
for i in some_string:
if i not in count:
count[i]=1
else:
count[i]+=1
dup_count[i]=count[i]
print("Duplicates of given string are below:\n",dup_count)
A little bit more Pythonic implementation (not the most, of course), but in the spirit of your C code could be:
for i, elem in enumerate(seq):
if elem in seq[i+1:]:
print elem
Edit: yes, it prints the elements more than once if there're more than 2 repetitions, but that's what the op's C pseudo code does too.