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
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
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
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852