How to print out a list with missing numbers? - python

I have a piece of code that is supposed to print out a list of numbers and also # for every time there is a duplicate number on the list, printing it out should look like this:
list = [6,7,7,9]
6#
7##
8
9#
Here is what i have so far:
def print_list(list1):
list2 = [int(i) for i in list1]
full_set = [x for x in range(list2[0], list2[-1] + 1)]
list2.extend(full_set)
list3 = list(set(list2))
result = sorted([(x, list2.count(x)) for x in list3], key=lambda y: y[1])
result.sort()
for elem in result:
print("{0:>2}".format(elem[0]), "{}".format(elem[1]*"#"))
And it comes out wrong:
6 ##
7 ###
8 #
9 ##
Any help is appreciated!

I think this works:
def print_list(list1):
list2 = [x for x in range(list1[0], list1[-1] + 1)]
list3 = list(set(list2))
result = sorted([(x, list1.count(x)) for x in list3], key=lambda y: y[1])
result.sort()
for elem in result:
print("{0:>2}".format(elem[0]), "{}".format(elem[1]*"#"))
print_list([6,7,7,9])
Output:
6 #
7 ##
8
9 #
The issue with your current code is that you add your set of the numbers you're expecting to list2, so counting the amount of each number in list2 yields one more than you expect.
This is what list2 looks like with your original code:
[6, 7, 7, 9, 6, 7, 8, 9]
This is because your call to the extend method takes list2 ( [6, 7, 7, 9] ), and adds full_set ( [6, 7, 8, 9] ) to it. Thus when you count the occurrences of each number in list2, you are counting the original number, and then an extra one, because you've extended list2 with full_set.

list2.extend(full_set) adds one extra number for each number in the range. You need to reduce 1 from the count of elements in result. You can do it in the list creation
result = sorted([(x, list2.count(x) - 1) for x in list3], key=lambda y: y[1])
or in the print
for elem in result:
print("{0:>2}".format(elem[0]), "{}".format((elem[1] - 1)*"#"))
output
6 #
7 ##
8
9 #

Related

Creating code to make sum of array elements with variable

I want to create a code that has the variable how many elements you take from an list and sum those. For example, if I type in 3, it takes the first 3 elements of an array and addes them together.
Preferably in some for loop but who knows what kind of creative solutions I get :)
x = [1, 3, 4, 2, 6, 9, 4]
Amount = 3
Sum = 0
Sum += x[Amount-3] + x[Amount-2] + x[Amount-1]
Desired result: 8
Amount_2 = 4
Sum = 0
Sum += x[Amount-4] + x[Amount-3] + x[Amount-2] + x[Amount-1]
Desired result: 11
Hope this explains it well.
x = [2, 6, 7, 7, 12] # your list here
amount = int(input("Enter amount: "))
print(f'desired result: {sum(x[:amount])}')

Find 3 maximum elements of list with python

How can I search for the three maximum elements of a list and replace it at same index with its result when divided by 2.
Please what am I doing wrong:
input is: 2 5 8 19 1 15 7 20 11
output should be : 2 5 8 9.5 1 7.5 7 10 11
Index out of range is the result displayed
def numberInput(line):
lineInput = [int(i) for i in line.split()]
min1 = min(lineInput)
for j in range(len(lineInput)):
if lineInput[j]>min1 and lineInput[j] > lineInput[j+1]:
max1 = lineInput[j]/float(2)
else:
max1 = lineInput[j]/float(2)
lineInput[j] = max1
lineInput[j] = max1
return(lineInput)
number = '2 5 8 19 1 15 7 20 11'
print(numberInput(number))
If the order of list isn't important, you can simply sort the list in descending order and then replace the first 3 elements as
a = [2, 5, 8, 19, 1, 15, 7, 20, 11]
a.sort(reverse = True)
a[0] = a[0] / 2
a[1] = a[1] / 2
a[2] = a[2] / 2
Output
[10.0, 9.5, 7.5, 11, 8, 7, 5, 2, 1]
If the order is important,
import heapq
largest = heapq.nlargest(3, a)
for i in range(len(a)):
if a[i] in largest:
a[i] = a[i] / 2
Output
[2, 5, 8, 9.5, 1, 7.5, 7, 10.0, 11]
heapq.nlargest() is a function of heapq which can give you n largest numbers from a list. Since lists in Python do not have a replace function, the list had to be traversed once, and then replaced manually. Hope this resolves your issue.
Why wouldn't you just walk through the list once (it maybe a bit longer code, but definitely efficient)
def numberInput(line):
lineInput = [int(i) for i in line.split()]
n = len(lineInput)
if n <= 3:
return [i / 2 for i in lineInput]
max_pairs = [(lineInput[i], i) for i in range(3)] # keep track of 3 max's and their indices
max_pairs.sort(key = lambda x: -x) # sort in descending order
for i in range(3, n):
if lineInput[i] >= max_pairs[0][0]: # greater than the largest element
max_pairs = [(lineInput[i], i)] + max_pairs[:2]
elif lineInput[i] >= max_pairs[1][0]: # greater than second element
max_pairs = [max_pairs[0], (lineInput[i], i), max_pairs[1]]
elif lineInput[i] >= max_pairs[2][0]: # greater than third element
max_pairs = max_pairs[:2] + [(lineInput[i], i)]
for pair in max_pairs:
lineInput[pair[1]] = lineInput[pair[0]] / 2
return lineInput
Explanation: max_pairs is a set of three tuples, containing the maximum three elements and their indices
Note: I think the above is easiest to understand, but you can do it in a loop if you don't like all those ifs

Adding to a specific variable in a list

I'm trying to add two lists. If the last variable is greater than 10, it needs to carry over to the previous variable in the list. For example :
1 / 2 / 3 (List 1)
7 / 8 / 9 (List 2)
Should equal
9 / 1 / 2 not 8/10/12
So far, I have
list1 = [1, 2, 3]
list2 = [7, 8, 9]
SumOfLists = [x+y for x,y in zip(list1, list2)]
That adds the lists together, but I'm not sure how to make the number carry over.
You can try this code.
list1 = [1, 2, 3]
list2 = [7, 8, 9]
def add_list(a,b):
carry = 0
res_list = []
for i,j in zip(a[::-1],b[::-1]): # Iterate through the lists in reverse
val = (i+j+carry)%10 # Store the sum in val
carry = (i+j+carry)//10 # Store the carry
res_list.append(val) # Append to the returning list
return res_list[::-1] # Return the list
print add_list(list1,list2)
Wil print
[9, 1, 2]
Algorithm
Loop through each of the values in reverse. Add each corresponding values. If the values are above 10 then find the exceeding value and put it to carry. Finally return the reverse of the list.
list1 = [1, 2, 3]
list2 = [7, 8, 9]
cur = 0 # num to carry over
result = []
for x,y in zip(reversed(list2),reversed(list1)):
if x + y + cur > 10: # if sum greater than 10, remember to add 1 on
t = x+y + cur # the next loop
d = str(t)[1] # get the rightmost digit
result.append(int(d))
cur = 1
else: # nothing to curry over, but still add cur,
# it may be 1
result.append(x+y+cur)
cur = 0
print(list(reversed(result)) )
[9, 1, 2]
just subtract 10 if it's more then 10 and add 1 to it's previous element. Do this proccess for all element in sum list
if SumOfLists[2] >= 10:
SumOfLists[2] -= 10
SumOfLists[1] += 1
And at last check
if SumOfLists[0] >= 10:
for i in range(len(SumOfLists)-1,0,-1):
SumOfLists[i] = SumOfLists[i-1]
SumOfLists[0] = 1

Find second maximum number in a list [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 12 months ago.
First line contains N. Second line contains list of N integers each separated by a space. I need to find the second largest number in list.
My code:
N = int(raw_input())
L = map(int, raw_input().split())
for i in L:
if i == max(L):
L.remove(i)
print L
print max(L)
If input is [2, 6, 9, 9, 5], this still prints maximum value: 9, as only one 9 is getting removed from the list.
So, how to remove all the 1st maximum values in the list?
The reason that it's still returning 9 is because you're mutating the list as you iterate over it. Essentially the steps are:
1. 2 6 9 9 5
^idx0
2. 2 6 9 9 5
^idx1
3. 2 6 9 9 5
^idx2
3a. 2 6 9 5
^idx2
4. 2 6 9 5
^
See how it skips evaluating the second 9 when it deletes the first 9? You'll notice if your list is [2, 6, 9, 5, 9], it works appropriately.
The minimal change to your code that will make it function is to iterate over a copy of the list, rather than the list itself.
L = [2, 6, 9, 9, 5]
maxL = max(L) # gotta move this out!!
for i in L[:]: # the slice creates a copy
if i == maxL:
L.remove(i)
print(max(L))
However it's probably easier to make a set (ensuring uniqueness), sort it, and return the second-to-last entry.
second_max = sorted(set(L))[-2]
try
N = 5
L = map(int, "2 6 9 9 5".split())
maxL = max(L)
#list comprehension for remove all occurrences of max(L)
L_filter = [e for e in L if e!=maxL]
print L
#print max of L_filter, second maximum of L
print max(L_filter)
you get:
[2, 6, 9, 9, 5]
6
Remove duplicated elements by converting to a set:
values = set(L)
Then remove the maximum:
values.discard(max(values))
you could also do this
L = [2, 6, 9, 9, 5]
L_tuples = zip(L, range(len(L))) #need tuples to make a dict
L_map = dict(L_tuples) #use the dict to dedupe
L_uniq = L_map.keys() #get back deduped values
L_sorted = sorted(L_uniq) #sort them ascending
second_largest = L_sorted[-2] #second from last is second largest
#or, rolling all that up...
second_largest = sorted(dict(zip(L, range(len(L)))).keys())[-2]
>>> import heapq
>>> values = [2, 6, 9, 9, 5]
>>> heapq.heapify(values)
>>> heapq._heapify_max(values)
>>> value = top = heapq.heappop()
>>> while value == top:
... value = heapq.heappop()
>>> print value
Here is another way to calculate the second maximum in a list. The code also considers the scenario that includes duplicate elements in the list.
number_of_elements=int(input('The number of elements in list\n'))
a=[]
for i in range(number_of_elements):
a.append(int(input('enter the list elements')))
#Use built-in function to calculate the maximum
max_list=max(a)
print("The maximum element is ",max_list)
#Calculate the number of times the number occur
count_of_max_num=a.count(max_list)
b=a
if (count_of_max_num == 1):
b.remove(max_list)
second_max=max(b)
print("The second largest number is", second_max, "The new list is" ,b)
else:
for i in range(count_of_max_num):
b.remove(max_list)
print ("The second largest is" , max(b))

How to limit for loop to print first few element from list in terms of their value in python?

I would like to limit for loop to print first few element from list in terms of their value. For example, if i < 6 :
list = [1,2,3,4,5,6,7,8,9,10]
for i < 6 in list:
print(i)
Thanks in advance !
In [9]: L = [1,2,3,4,5,6,7,8,9,10]
In [10]: for i in L:
....: if i<6:
....: print(i)
....:
1
2
3
4
5
based on I would like to limit for loop to print first few element from list in terms of their value it seems the list is in order so you can use itertools.takewhile :
from itertools import takewhile
lst = [1,2,3,4,5,6,7,8,9,10] # don't use list
tke = takewhile(lambda x: x< 6, lst)
for t in tke:
print(t)
1
2
3
4
5
If you want a list use list(...).
print(list(takewhile(lambda x: x< 6, lst))) # good reason why we should not use list as a variable name
[1, 2, 3, 4, 5]

Categories

Resources