We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

YourBuddy • 10 years ago

The ( ' This is some random text ' ) problem has some issues. I use ie10 and the first var is ( word), the second var is (words2) and the third is (words). The problem works with one var , (word). It will not work the way it is written.

ReconSniper • 7 years ago

Probably a typo...the author should correct the words to words2

Domen Muzga • 7 years ago

how can i split a continous string 'AGGCATGCATGCAT' into triplets --> 'AGG', 'CAT'.... etc, you get the point, ? please

Gael Lickindorf • 7 years ago

>>> s = 'AGGCATGCATGCAT'
>>> s[0:3]
'AGG'
>>> [s[i:i+3] for i in range(0,len(s),3)]
['AGG', 'CAT', 'GCA', 'TGC', 'AT']

Angry Nimbus • 7 years ago

Thank you very much! You're awesome! Yeah, I'm making proteins, too. :D

Master Roshi • 6 years ago

why protein make dna that will make proteins instead ......LOL
use bioperl instead ....Splice function

Sandeep Bolla • 7 years ago

simply awesome

Osama • 6 years ago

a = 'sdfgjrerdfsfgdfg'
q = []
for x in range(0, len(a), 3):
z = (a[x:x+3])
q = q + [z]
print(q)
>>>
['sdf', 'gjr', 'erd', 'fsf', 'gdf', 'g']

Mark du Preez • 7 years ago

Are you making proteins? :-D

Rahul Gupta • 6 years ago

correctness and clarity should be the main goal of a post. well nice info about pyrhon programming

Joshua Van Deren • 7 years ago

Did you mean to say:
words2 = words.split(" ")

instead of:
words2 = word.split(" ")

blueboy90780 • 6 years ago

That's such a minor error - I don't think anyone would've mind it being there xD

pythonforbeginners • 7 years ago

Thanks for the catch! We have corrected.

Ruckazoid • 10 years ago

What if there's an excess amount of white space between words? how do we use strip to work with split?

KodeRiter • 10 years ago

str.split() with no arguments strips away all the whitespace.

Rifky Satyana • 5 years ago

i have a question.. how do you get a data from a text file that is put this way (a txt file of a coordinate) :
6,8 2,6
2,6 3,0
6,4 7,8
6,4 4,3
3,0 4,3

and output it so that in an array it shows only one of each coordinates.. its really hard for me.

Rein Jakobson • 5 years ago

But how can I split from commas and newlines at the same time if I read from file: "Names.txt".
For Example:

Thomas, Julie
Rick
Emma, Sam
John, Anna

to: ['Thomas', 'Julie', 'Rick', 'Emma', 'Sam', 'John', 'Anna']

MadBroCowDisease • 6 years ago

So you don't have to add a separate 'print' line when calling the '.split()' command? Print is built in to the command?

General SUMON • 6 years ago

if I have a list like ..x = [1,2.5,'A', 'R', 23, 55.5] how can I get l_1 = [1,2,23], l_2 =[2.5,55], s_t = [A,R]

Thomas White • 6 years ago

use the power of Python!!!!
i_1 = [y for y in x if type(y) == int]
i_2 = [y for y in x if type(y) == float]
s_t = [y for y in x if type(y) == str]

yes, it is that simple. This is three lines with no "side effects".

x = [1,2.5,'A', 'R', 23, 55.5]
i=0
l_1=[]
l_2=[]
s_t=[]
y=[]

while i<len(x): if="" type(x[i])="=" int:="" l_1.append(x[i])="" elif="" type(x[i])="=" float:="" l_2.append(x[i])="" elif="" type(x[i])="=" str:="" s_t.append(x[i])="" else:="" print="" x[i],'not="" defined="" '="" y.append()="" i+="1">

Thomas White • 6 years ago

If I only had a penny for every time someone did not use a comprehension and should have... I would be a thousandaire. Python is a powerful and compact language. If you find your code overly long and complex, review it, I guarantee there is a better way to utilize Python's capabilities. I am no expert by any means, but I do believe in improving what needs improvement.

Firnas Jubran • 6 years ago

how can I split a sting into a list by every upper letter in the way for example str_list = [“This”, “is”, “not a Red”, “”, “wine, but a white One”] it will give us split_str_list = [“This”, “is”, “not a ”, ”Red”, “”, “wine, but a white “, ”One”]

Thomas White • 6 years ago

import sys
def BreakOnUpper(str_list):
newstr = []
for i,e in enumerate(str_list):
if any(c.isupper()for c in e): # is there an uppercase letter in e?
for p,c in enumerate(e): #at what position?
if c.isupper():
if i>1:
newstr.append(e[:p])
newstr.append(e[p:])
break
else: #just append anything that does not contain an uppercase letter
newstr.append(e)
return newstr

def main():
str_list = ["This", "is", "not a Red", "", "wine, but a white One"]
print(BreakOnUpper(str_list))

if __name__ == "__main__":
sys.exit(main())

Serge Arroyo • 6 years ago

isnt this also a typo?

>>> a,b,c = x.split(“,”)

isn't this supposed to be:

>>> (a,b,c) = x.split(“,”)

Matthew Hardie • 6 years ago

No it is not, a,b,c will also work

Jad Dabliz • 7 years ago

hello how can i split on non alphabetic characters i used split([^a-zA-Z]) and did not work

broketheinterweb • 7 years ago

Look up their unicode equivalent (UTC-8)

naman gautam • 8 years ago

I am very greatful to you guys for this explanation. thank you.

Joe Chagnon • 9 years ago

What an awesome resource.

Furkan • 10 years ago

Thanks. Helped me.