0

this is kind of a quick question, but just a bit complex:

say i want it to find n in range(5), i would do this:

for n in range(5):
      print n

and it would return 0,1,2,3,4.... but what if instead of returning i want it to put it in a list, say list x, such that if i want i can later say

>>>x
[0,1,2,3,4]

is that possible?

2
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by max.yevs
0

my apologies, once again i've found the answer before anyone could reply

x = []
for n in range(5):
    x.append(n)
0

another quick list question then:

say you have list a = [2,4,6,8,10]
and list b = [2,6,10,12]

is there any way to make a list c show all the ones they have in common? so list c = [2,6,10] ?

i would think its as easy as a & b (as it shows in lesson 5 tutorial), but apparently the minus, plus, &, etc. symbols don't support lists as operands

0

i've kind of found answer, to convert them to sets (which actually works well because i need to remove duplicates) but if i do that, the numbers go out of order, is it possible to put them back in order from smallest to largest?

ah...nevermind i've figured out how to do it...

0

my apologies, once again i've found the answer before anyone could reply

x = []
for n in range(5):
    x.append(n)

You're taking the long way here. Look:

>>> x = range(5)
>>> x
[0, 1, 2, 3, 4]
>>>
1

say you have list a = [2,4,6,8,10]
and list b = [2,6,10,12]

is there any way to make a list c show all the ones they have in common? so list c = [2,6,10] ?

Use sets and then convert back to list and sort() to get it ordered.

>>> setA = set([2,4,6,8,10])
>>> setB = set([2,6,10,12])
>>> rtn = list(setA.intersection(setB))
>>> rtn.sort()
>>> rtn
[2, 6, 10]
>>>

HTH

0

yeah, that's basically how i did using sets... really i apologize all the information i needed was in the turorial, it just took me some time to get it right...

a = set(a)
b = set(b)
c = b&a

yeah, i know for the [0,1,2,3,4] situation making a list is the long way to take, in reality after the "n in range" there was some other stuff before appending to x....

basically i was making a very inefficient prime number finder:

print ("Prime Number Finder")
x = input("What's the upper range?: ")
if x >= 2:
    print ("2")
a = []
b = []
for n in range(x+1):
    for m in range(n):
        if n is not 0:
            if m is not 0:
                 if m is not 1:
                    if n % m is 0:
                        a.append(n)                            
                    else:
                        b.append(n)
a = set(a)
b = set(b)
c = b-a
for h in sorted(set(c)):
    print h

i know there's probably much better ways to do it, but it works well and the method behind how it works is fairly interesting...
actually i suppose instead of making a new list "b" i could have just used the list "range n"...as you showed:

b = range(x)

that would actually be better because 2 would not fall out as an exception, and i wouldn't have to say print(2) as i did above

0

as a matter of fact, i did change it to b = range(x+1) .

works great, except it then shows 0 and 1 as prime numbers also, so i had to add an if h >= 2 statement

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.