954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python lists in range

Hey, i need to make app that should take 10 chars from list. I tried it with range command but it's not working.

list = 'abcdefghijklmnopqrstuvxyzo'
for list in range(10):
    print list

And this code just prints numbers 0 to 10.
So, is there some other command that would do same as range(i mean printing 10 chars from the list)?

Toikkala
Newbie Poster
6 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Dont use list as a variable name,it`s a python keyword

>>> list
<type 'list'>
>>> a = 'abc'
>>> list(a)
['a', 'b', 'c']


Loop over the len() of my_sting,stop at 10 and use slice.

my_string = 'abcdefghijklmnopqrstuvxyzo'

for i in range(len(my_string)):
    if i == 10:
        print my_string[:i]
        #--> abcdefghij

If you want to save 10 chars in a list.

my_string = 'abcdefghijklmnopqrstuvxyzo'

my_list = []
for i in range(len(my_string)):
    if i == 10:
        my_list.append(my_string[:i])
        
print my_list
#--> ['abcdefghij']
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

Thanks a lot!

Toikkala
Newbie Poster
6 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Well it works but i tried to add more functionality by adding random.shuffle but it just prints "none"?Why?

import random
my_string = 'abcdefghijklmnopqrstuvxyzo'
 
my_list = []
for i in range(len(my_string)):
    if i == 10:
        my_list.append(my_string[:i])
 
print random.shuffle([ my_string])

Random.shuffle should just shuffle those string chars, but it just prints "none"? Why?

Toikkala
Newbie Poster
6 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

If you want to shuffle it`s need to more than 2 elements in list.
Look into spilt(),list(),to split upp upp a string or a list.

>>> a = 'abc'
>>> list(a)
['a', 'b', 'c']

>>> a = 'a b c'.split()
>>> a
['a', 'b', 'c']
>>> from random import shuffle
>>> help(shuffle)
Help on method shuffle in module random:

shuffle(self, x, random=None, int=<type 'int'>) method of random.Random instance
    x, random=random.random -> shuffle list x in place; return None.
    
    Optional arg random is a 0-argument function returning a random
    float in [0.0, 1.0); by default, the standard random.random.

>>> #This list has 3 element
>>> l = ['a', 'b', 'c']
>>> shuffle(l)
>>> l
['b', 'a', 'c']
>>> my_list
['abcdefghij']
>>> #my_list has only 1 element an can not be shuffled
>>> print shuffle(my_list)
None
>>>
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

Actually this is string so it wont work and i want that i would work with the range-thingy.

import random
my_string = 'abcdefghijklmnopqrstuvxyzo'
s = 'gfsfggf'.join(my_string)
 
my_list = []
for i in range(len(my_string)):
    if i == 10:
        my_list.append(my_string[:i])
 
print random.shuffle([ my_string])

And it's not working, so how should i do shuffel thing?

Toikkala
Newbie Poster
6 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

I thing there is another way to do it:

>>> String = "Hello World!!"
>>> print String[2:6]
llo
funfullson
Junior Poster in Training
77 posts since Jan 2009
Reputation Points: 10
Solved Threads: 2
 
And it's not working, so how should i do shuffel thing?
import random

my_string = 'abcdefghijklmnopqrstuvxyzo'

my_list = []
for i in range(len(my_string)):
    if i == 10:
        my_list.append(my_string[:i])


r =  list(''.join(my_list))
print r #now we can shuffle
random.shuffle(r)
shuffle_string = ''.join(r)
print shuffle_string         #string
#--> ebdihfjgca
print shuffle_string.split() #make list
#--> ['ebdihfjgca']
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

You might consider random facility for producing samples:

import random
import string
random_count = random.randint(3,len(string.ascii_lowercase))
print('Non-repeating sample of %i items is:\n%s' %
      (random_count, random.sample(string.ascii_lowercase, random_count)))
shuffled_letters = list(string.ascii_lowercase)
random.shuffle(shuffled_letters)
print('With shuffle and randint, same:\n%s' % (shuffled_letters[:random_count]))
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: