I have a string myString:
myString = "alpha beta gamma"
I want to split myString into its three words:
myWords = myString.split()
Then, I can access each word individually:
firstWord = myWords[0]
secondWord = myWords[1]
thirdWord = myWords[2]
My question: How can I assign these three words in just one line, as an output from the split() function? For example, something like:
[firstWord secondWord thirdWord] = myString.split()
What's the syntax in Python 2.7?
Almost exactly what you tried works:
firstWord, secondWord, thirdWord = myString.split()
Demo:
>>> first, second, third = "alpha beta gamma".split()
>>> first
'alpha'
>>> second
'beta'
>>> third
'gamma'
To expand on Mr. Pieter's answer and the comment raised by TheSoundDefense,
Should str.split() return more values than you allow, it will break with ValueError: too many values to unpack. In Python 3 you can do
first, second, third, *extraWords = str.split()
And this will dump everything extra into a list called extraWords. However, for Python 2 it gets a little more complicated and less convenient as described in this question and answer.
Alternatively, you could change how you're storing the variables and put them in a dictionary using a comprehension.
>>> words = {i:word for i,word in enumerate(myString.split())}
>>> words
{0: 'alpha', 1: 'beta', 2: 'gamma'}
This has the advantage of avoiding the value unpacking all together (which I think is less than ideal in Python 2). However, this can obfuscate the variable names as they are now referred to as words[0] instead of a more specific name.
the above answer is correct also if you want to make a list do this:
my_list=[first, second, third] = "alpha beta gamma".split()
Alternatively, using a list comprehension ...
mystring = "alpha beta gamma"
myWords = [x for x in mystring.split()]
first = myWords[0]
second = myWords[1]
third = myWords[2]
print(first, second, third)
alpha beta gamma
Related
So I have a long list of column headers. All are strings, some are several words long. I've yet to find a way to write a function that extracts the first word from each value in the list and returns a list of just those singular words.
For example, this is what my list looks like:
['Customer ID', 'Email','Topwater -https:', 'Plastics - some uml']
And I want it to look like:
['Customer', 'Email', 'Topwater', 'Plastics']
I currently have this:
def first_word(cur_list):
my_list = []
for word in cur_list:
my_list.append(word.split(' ')[:1])
and it returns None when I run it on a list.
You can use list comprehension to return a list of the first index after splitting the strings by spaces.
my_list = [x.split()[0] for x in your_list]
To address "and it returns None when I run it on a list."
You didn't return my_list. Because it created a new list, didn't change the original list cur_list, the my_list is not returned.
To extract the first word from every value in a list
From #dfundako, you can simplify it to
my_list = [x.split()[0] for x in cur_list]
The final code would be
def first_word(cur_list):
my_list = [x.split()[0] for x in cur_list]
return my_list
Here is a demo. Please note that some punctuation may be left behind especially if it is right after the last letter of the name:
names = ["OMG FOO BAR", "A B C", "Python Strings", "Plastics: some uml"]
first_word(names) would be ['OMG', 'A', 'Python', 'Plastics:']
>>> l = ['Customer ID', 'Email','Topwater -https://karls.azureedge.net/media/catalog/product/cache/1/image/627x470/9df78eab33525d08d6e5fb8d27136e95/f/g/fgh55t502_web.jpg', 'Plastics - https://www.bass.co.za/1473-thickbox_default/berkley-powerbait-10-power-worm-black-blue-fleck.jpg']
>>> list(next(zip(*map(str.split, l))))
['Customer', 'Email', 'Topwater', 'Plastics']
[column.split(' ')[0] for column in my_list] should do the trick.
and if you want it in a function:
def first_word(my_list):
return [column.split(' ')[0] for column in my_list]
(?<=\d\d\d)\d* try using this in a loop to extract the words using regex
I would like to sort out the first row of a given list.
I've been already tried to use python "replace" to remove the second row.
But the problem is that the replace function seems not work at all.
Here is the regular expression I used: replace(r'^ //.*$','')
Here is the list:
//SA/... //short_message/Saint/...
//SS-SA/... //long_message/wonder-girl/...
here is the output I am expecting:
//SA/...
//SS-SA/...
l = ["1 12","3 12","2 12"] # space separated
n = [x.split()[0] for x in l]
print sorted(n)
I am trying to code a program which will ask for a person's full name (first, middle, last) and will print (last, middle, tsrif). Like print their name backwards, but the letters are backwards only on their first name. I can't figure out how to flip the order of the words without flipping their letters. Any help?
My code so far:
import sys
str = raw_input("first middle last")
str.split( );
>>> my_string = raw_input('?: ').split()
?: first middle last
>>> new_string = ' '.join(my_string[:0:-1] + [my_string[0][::-1]])
>>> new_string
'last middle tsrif'
>>> s = 'first middle last'
>>> L = s.split()
>>> L[0] = L[0][::-1]
>>> L
['tsrif', 'middle', 'last']
>>> print L[::-1]
['last', 'middle', 'tsrif']
Reverse the list at first. Then reverse the last string in list
my_list = string.split()
my_list = list(reversed(my_list))
my_list[2] = my_list[2][::-1]
print my_list
Don't use str as variable.
This should work
import sys
str = "first middle last";
allWords = str.split(" ");
length = len(allWords)
for index in range(length-1):
print(allWords[length-index-1],end=" ")
print((allWords[0])[::-1])
The str.split() function returns the list, it doesn't modify it in place. So you want :
split_up = str.split()
(obviously split_up is an arbitrary name, the point is that it's a list)
input_name = "first middle last" # whatever your input is: using as example here
split_name = input_name.split()
desired_output = [split_name [2], split_name [1], split_name [0][::-1]]
>>> desired_output
['last', 'middle', 'tsrif']
>>> ' '.join(desired_output)
'last middle tsrif'
>>> ', '.join(desired_output)
'last, middle, tsrif'
Let's assume you have a variable s which you've gotten from the user. If you want to split that up into individual words, you use .split() and save the result
s = "john henry smith"
parts = s.split() # ['john', 'henry', 'smith']
now, to reverse these words, you can use the built in function reversed and convert the result to a list
rev_parts = list(reversed(parts)) # ['smith', 'henry', 'john']
Finally you want to reverse just the last element of rev_parts, you can use a -1 index to indicate the last element in the list. What you want to do is overwrite the existing element. We can use reversed again here, but we'll also need to use a join to tell it to but all the characters back together after
rev_name = ''.join(reversed(rev_parts[-1])) # 'nhoj'
rev_parts[-1] = rev_name # overwrite 'john'
Now you have a list of what you want. If you want to put them all back together separated by spaces, use another join
result = ' '.join(rev_parts)
You can eliminate various intermediate variables and steps here as you feel comfortable.
An alternative to using reversed is to use the slice syntax [::-1] which says, from the end to the beginning, back one character at a time. In that case you'd have
rev_parts = parts[::-1]
rev_parts[-1] = rev_parts[-1][::-1]
so i'm trying to make a program in Python PyScripter 3.3 that takes input, and converts the input into an acronym. This is what i'm looking for.
your input: center of earth
programs output: C.O.E.
I don't really know how to go about doing this, I am looking for not just the right answer, but an explanation of why certain code is used, thanks..
What I have tried so far:
def first_letters(lst):
return [s[:1] for s in converted]
def main():
lst = input("What is the phrase you wish to convert into an acronym?")
converted = lst.split().upper()
Beyond here I am not really sure where to go, so far I know I need to captialize the input, split it into separate words, and then beyond that im not sure where to go...
I like Python 3.
>>> s = 'center of earth'
>>> print(*(word[0] for word in s.upper().split()), sep='.', end='.\n')
C.O.E.
s = 'center of earth' - Assign the string.
s.upper() - Make the string uppercase. This goes before split() because split() returns a list and upper() doesn't work on lists.
.split() - Split the uppercased string into a list.
for word in - Iterate through each element of the created list.
word[0] - The first letter of each word.
* - Unpack this generator and pass each element as an argument to the print function.
sep='.' - Specify a period to separate each printed argument.
end='.\n' - Specify a period and a newline to print after all the arguments.
print - Print it.
As an alternative:
>>> s = 'center of earth'
>>> '.'.join(filter(lambda x: x.isupper(), s.title())) + '.'
'C.O.E.'
s = 'center of earth' - Assign the string.
s.title() - Change the string to Title Case.
filter - Filter the string, retaining only those elements that are approved by a predicate (the lambda below).
lambda x: x.isupper() - Define an anonymous inline function that takes an argument x and returns whether x is uppercase.
'.'.join - Join all the filtered elements with a '.'.
+ '.' - Add a period to the end.
Note that this one returns a string instead of simply printing it to the console.
>>> import re
>>> s = "center of earth"
>>> re.sub('[a-z ]+', '.', s.title())
'C.O.E.'
>>> "".join(i[0].upper() + "." for i in s.split())
'C.O.E.'
Since you want an explanation and not just an answer:
>>> s = 'center of earth'
>>> s = s.split() # split it into words
>>> s
['center', 'of', 'earth']
>>> s = [i[0] for i in s] # get only the first letter or each word
>>> s
['c', 'o', 'e']
>>> s = [i.upper() for i in s] # convert the letters to uppercase
>>> s
['C', 'O', 'E']
>>> s = '.'.join(s) # join the letters into a string
>>> s
'C.O.E'
>>> s = s + '.' # add the dot at the end
>>> s
'C.O.E.'
fh=open('asd.txt')
data=fh.read()
fh.close()
name=data.split('\n')[0][1:]
seq=''.join(data.split('\n')[1:])
print name
print seq
In this code, the 3rd line means "take only first line with first character removed" while the 4th line means "leave the first line and join the next remaining lines".
I cannot get the logic of these two lines.
Can anyone explain me how these two slice operators ([0][1:]) are used together?
Thanx
Edited: renamed file variable (which is a keyword, too) to data.
Think of it like this: file.split('\n') gives you a list of strings. So the first indexing operation, [0], gives you the first string in the list. Now, that string itself is a "list" of characters, so you can then do [1:] to get every character after the first. It's just like starting with a two-dimensional list (a list of lists) and indexing it twice.
When confused by a complex expression, do it it steps.
>>> data.split('\n')[0][1:]
>>> data
>>> data.split('\n')
>>> data.split('\n')[0]
>>> data.split('\n')[0][1:]
That should help.
lets do it by steps, (I think I know what name and seq is):
>>> file = ">Protein kinase\nADVTDADTSCVIN\nASHRGDTYERPLK" <- that's what you get reading your (fasta) file
>>> lines = file.split('\n') <- make a list of lines
>>> line_0 = lines[0] <- take first line (line numbers start at 0)
>>> name = line_0[1:] <- give me line items [x:y] (from x to y)
>>> name
'Protein kinase'
>>>
>>> file = ">Protein kinase\nADVTDADTSCVIN\nASHRGDTYERPLK"
>>> lines = file.split('\n')
>>> seqs = lines[1:] <- gime lines [x:y] (from x to y)
>>> seq = ''.join(seqs)
>>> seq
'ADVTDADTSCVINASHRGDTYERPLK'
>>>
in slice [x:y], x is included, y is not included. When you want to arrive to the end of the list just do not indicate y -> [x:] (from item of index x to the end)
Each set of [] just operates on the list that split returns, and the resulting
list or string then used without assigning it to another variable first.
Break down the third line like this:
lines = file.split('\n')
first_line = lines[0]
name = first_line[1:]
Break down the fourth line like this:
lines = file.split('\n')
all_but_first_line = lines[1:]
seq = ''.join(all_but_first_line)
take this as an example
myl = [["hello","world","of","python"],["python","is","good"]]
so here myl is a list of list. So, myl[0] means first element of list which is equal to ['hello', 'world', 'of', 'python'] but when you use myl[0][1:] it means selecting first element from list which is represented by myl[0] and than from the resulting list(myl[0]) select every element except first one(myl[0][1:]). So output = ['world', 'of', 'python']