есть вопросы ( помогите, только начал учить python 2.7 3 дня как)
почему
for n in nums: if nums.count(n)>1: nums.remove(n)
[nums.remove(n) for n in nums if nums.count(n) > 1]
2. есть ли в python аналог функции ?(1=1,y,n) которая вернет “y” в данном случае?
3.мне непонятно задание
# E. Given two lists sorted in increasing order, create and return a merged
# list of all the elements in sorted order. You may modify the passed in lists.
# Ideally, the solution should work in “linear” time, making a single
# pass of both lists.
def linear_merge(list1, list2):
# +++your code here+++
return
# Note: the solution above is kind of cute, but unforunately list.pop(0)
# is not constant time with the standard python list implementation, so
# the above is not strictly linear time.
# An alternate approach uses pop(-1) to remove the endmost elements
# from each list, building a solution list which is backwards.
# Then use reversed() to put the result back in the correct order. That
# solution works in linear time, but is more ugly.
я написал его как
(понятно что можно заоптимайзить вызовы через “.” но дело не в этом)
i=0 m = [] while list2.count()+list1.count()>0: i -= 1 if list1.count()>0: m.append(list1.pop(i)) if list2.count()>0: m.append(list2.pop(i))
Но для обеих функций сортировка не поидет, для заданных условий:
test(linear_merge(['aa', 'xx', 'zz'], ['bb', 'cc']), ['aa', 'bb', 'cc', 'xx', 'zz']) test(linear_merge(['aa', 'xx'], ['bb', 'cc', 'zz']), ['aa', 'bb', 'cc', 'xx', 'zz']) test(linear_merge(['aa', 'aa'], ['aa', 'bb', 'bb']), ['aa', 'aa', 'aa', 'bb', 'bb'])