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)?

Recommended Answers

All 8 Replies

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']

Thanks a lot!

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?

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
>>>

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?

I thing there is another way to do it:

>>> String = "Hello World!!"
>>> print String[2:6]
llo

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']

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]))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.