Finding Multiples of a Defined List in Python - python-3.x

I'm trying to write a program that takes a list as input and prints only the multiples of three...can't figure it out, hoping to find some help. The list I've defined is [3, 1, 6, 2, 3, 9, 7, 9, 5, 4, 5, 12, 13, 15].

def multiples_of_three(input_list):
return [y for y in input_list if y % 3 == 0]
x = [3, 1, 6, 2, 3, 9, 7, 9, 5, 4, 5, 12, 13, 15]
print(multiples_of_three(x))
% is the modulo operator in python, y is a multiple of 3 if and only if y % 3 == 0

Related

Apply if statement on multiple lists with multiple conditions

I would like to append ids to a list which meet a specific condition.
output = []
areac = [4, 4, 4, 4, 1, 6, 7,8,9,6, 10, 11]
arean = [1, 1, 1, 4, 5, 6, 7,8,9,10, 10, 10]
id = [1, 2, 3, 4, 5, 6, 7,8,9,10, 11, 12]
dist = [2, 2, 2, 4, 5, 6, 7.2,5,5,5, 8.5, 9.1]
for a,b,c,d in zip(areac,arean,id,dist):
if a >= 5 and b==b and d >= 3:
output.append(c)
print(comp)
else:
pass
The condition is the following:
- areacount has to be >= 5
- At least 3 ids with a distance of >= 3 with the same area_number
So the id output should be [10,11,12].I already tried a different attempt with Counter that didn't work out. Thanks for your help!
Here you go:
I changed the list names to something more descriptive.
output = []
area_counts = [4, 4, 4, 4, 1, 6, 7, 8, 9, 6, 10, 11]
area_numbers = [1, 1, 1, 4, 5, 6, 7, 8, 9, 10, 10, 10]
ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
distances = [2, 2, 2, 4, 5, 6, 7.2, 5, 5, 5, 8.5, 9.1]
temp_numbers, temp_ids = [], []
for count, number, id, distance in zip(counts, numbers, ids, distances):
if count >= 5 and distance >= 3:
temp_numbers.append(number)
temp_ids.append(id)
for (number, id) in zip(temp_numbers, temp_ids):
if temp_numbers.count(number) == 3:
output.append(id)
output will be:
[10, 11, 12]

When i use set( list_a + list_b ) it returns a dictionary. Do sets naturally return dictionaries?

I'm doing some beginner python exercises and one of them is to remove duplicates from a list. I've successfully done it, but the strange thing is that it is returning a dictionary instead of a list.
This is my code.
import random
a = []
b = []
for i in range(0,20):
n = random.randint(0,10)
a.append(n)
for i in range(0,20):
n = random.randint(0,10)
b.append(n)
print(sorted(a))
print(sorted(b))
c = set(list(a+b))
print(c)
and this is what it's spitting out
[0, 0, 1, 1, 1, 1, 2, 3, 4, 4, 6, 6, 7, 7, 7, 8, 9, 9, 10, 10]
[0, 1, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 7, 8, 9, 9, 10, 10, 10]
{0, 1, 2, 3, 4, 6, 7, 8, 9, 10}
thanks in advance!
{0, 1, 2, 3, 4, 6, 7, 8, 9, 10} is a set, not a dictionary, a dictionary would be printed as {key:value, key:value, ...}
Try print(type(c)) and you'll see it prints <class 'set'> rather than <class 'dict'>
Also try the following
s = {1,2,3}
print(type(s))
d = {'a':1,'b':2,'c':3}
print(type(d))
You'll see the type is different

Python code to print elements from list that meet certain criteria [duplicate]

This question already has answers here:
Print an element in a list based on a condition
(3 answers)
Select value from list of tuples where condition
(4 answers)
How to return a subset of a list that matches a condition [duplicate]
(1 answer)
Closed 3 years ago.
I have a list with 22 integers (ranging from 1 through 9) and want to create/ print a new list containing only those integers that are above 5.
This is what I have tried so far - the result (obviously) is that 'the_list' gets printed multiple times - i.e. the number of times = the number of instances above 5.
the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
print(the_list)
k=5
tl2=[]
for i in the_list:
if i > k :
tl2.append(the_list)
Try this code:
>>> the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
>>> print(the_list)
[1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
>>> the_filtered_list = list(filter(lambda x: x > 5, the_list))
>>> print(the_filtered_list)
[7, 6, 6, 7, 6, 6, 7]
See
filter
lambda
EDIT:
Another option is to use a generator expression:
>>> the_filtered_list = list(i for i in the_list if i > 5)
>>> print(the_filtered_list)
[7, 6, 6, 7, 6, 6, 7]
See
generator expressions and list comprehensions
EDIT:
My initial answer was indeed slow and memory inefficient. Here is the comparison of several possibilities. Which one to choose depends on how big the list is and what it is used for later.
>>> import random
>>> import timeit
>>> import sys
>>>
>>> the_list = [random.randrange(1, 10) for _ in range(100)]
>>>
>>> timeit.timeit('filter(lambda x: x > 5, the_list)', setup=f'the_list = {the_list}')
0.15890196000000856
>>> timeit.timeit('[i for i in the_list if i > 5]', setup=f'the_list = {the_list}')
2.633208761999981
>>> timeit.timeit('(i for i in the_list if i > 5)', setup=f'the_list = {the_list}')
0.227755295999998
>>>
>>> timeit.timeit('list(filter(lambda x: x > 5, the_list))', setup=f'the_list = {the_list}')
7.5565902380000125
>>> timeit.timeit('list(i for i in the_list if i > 5)', setup=f'the_list = {the_list}')
3.599053368
>>>
>>> sys.getsizeof(filter(lambda x: x > 5, the_list))
64
>>> sys.getsizeof([i for i in the_list if i > 5])
440
>>> sys.getsizeof((i for i in the_list if i > 5))
128
>>>
>>> sys.getsizeof(list(filter(lambda x: x > 5, the_list)))
480
>>> sys.getsizeof(list(i for i in the_list if i > 5))
480
The problem is you are appending the list, not the number 'i'
the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
print(the_list)
k=5
tl2=[]
# the_list refers to the entire list
# i is an element in the list
for i in the_list:
if i > k :
# append the number 'i' if it is greater than k
tl2.append(i)
print (t12)
the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
print(the_list)
k=5
new_ls = [x for x in the_list if x >k]
print(new_ls)
try this solution

To find unit digit at a particular index of fibonacci series in O(1) time. (fibonacci series may be <=10^18)

This code give wrong output above 10^7 input. can any body help me to solve this problem?
from math import sqrt,floor,log
def fib(N):
var = (1 + sqrt(5)) / 2
return round(pow(var, N) / sqrt(5))
test = int(input())
a=floor(log(test,2))
b=2**a
a=b%60
print(fib(a-1)%10)
Fibonacci series has a cycle of 60 for its unit digit (without getting deep into map you can see that after 60 you get 1 and 1 again, so the sum would be 2 and so on).
Therefore, you can prepare a list of these Fibonacci unit digits:
fib_digit = [1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, 7, 7, 4, 1, 5, 6, 1, 7, 8, 5, 3, 8, 1, 9, 0, 9, 9, 8, 7, 5, 2, 7, 9, 6, 5, 1, 6, 7, 3, 0, 3, 3, 6, 9, 5, 4, 9, 3, 2, 5, 7, 2, 9, 1, 0]
and return fib_digit[N % 60] in O(1).

How to sum an output in Python

The program below will create a list of 100 numbers chosen randomly between 1-10. I need help to then sum the list, then average the list created.
I have no idea how to begin and since I'm watching videos online I have no person to turn to. I'm very fresh in this world so I may just be missing entire ideas. I would doubt that I don't actually know enough though because the videos I paid for are step by step know nothing to know something.
Edit: I was informed that what the program does is overwrite a variable, not make a list. So how do I sum my output like this example?
This is all I have to go on:
Code:
import random
x=0
while x < 100:
mylist = (random.randrange(1,10))
print(mylist)
x = x+1
I think the shortest and pythonic way to do this is:
import random
x = [random.randrange(1,10) for i in range(100)] #list comprehension
summed = sum(x) #Sum of all integers from x
avg = summed / len(x) #Average of the numbers from x
In this case this shouldn't have a big impact, but you should never use while and code manual counter when you know how many times you want to go; in other words, always use for when it's possible. It's more efficient and clearer to see what the code does.
def sum(list):
sm = 0
for i in list:
sm+=i
return sm
Just run sum(list) to get sum of all elements
Or you can use
import random
x=0
mylist = []
sm = 0
while x < 100:
mylist.append(random.randrange(1,10))
sm += mylist[x]
x += 1
Then sm will be sum of list
The code is not correct. It will not create a list but generate a number everytime. Use the below code to get your desired result.
import random
mylist = []
for x in range(100):
mylist.append(random.randrange(1,10))
print(mylist)
print(sum(mylist))
OR
import random
mylist = [random.randrange(1,10) for value in range(100)]
print(mylist)
print(sum(mylist))
Output:
[3, 9, 3, 1, 3, 5, 8, 8, 3, 3, 1, 2, 5, 1, 2, 1, 4, 8, 9, 1, 2, 2, 4,
6, 9, 7, 9, 5, 4, 5, 7, 7, 9, 2, 5, 8, 2, 4, 3, 8, 2, 1, 3, 4, 2, 2,
2, 1, 6, 8, 3, 2, 1, 9, 6, 5, 8, 7, 7, 9, 9, 9, 8, 5, 7, 9, 4, 9, 8,
7, 5, 9, 2, 6, 8, 8, 3, 4, 8, 4, 7, 9, 9, 4, 2, 9, 9, 6, 3, 4, 9, 5,
3, 8, 4, 1, 1, 3, 2, 6]
512

Resources