I have a list of string and I want to take the last "word" of it, explanation :
Here's my code :
myList = ["code 53 value 281", "code 53 value 25", ....]
And I want to take only the number at the end :
myList = ["281", "25", ....]
Thank you.
Let's break down your problem.
So first off, you've got a list of strings. You know that each string will end with some kind of numeric value, you want to pull that out and store it in the list. Basically, you want to get rid of everything except for that last numeric value.
To write it in code terms, we need to iterate on that list, split each string by a space character ' ', then grab the last word from that collection, and store it in the list.
There are quite a few ways you could do this, but the simplest would be list comprehension.
myList = ["Hey 123", "Hello 456", "Bye 789"] # we want 123, 456, 789
myNumericList = [x.split(' ')[-1] for x in myList]
# for x in myList is pretty obvious, looks like a normal for loop
# x.split(' ') will split the string by the space, as an example, "Hey 123" would become ["Hey", "123"]
# [-1] gets the last element from the collection
print(myNumericList) # "123", "456", "789"
I don't know why you would want to check if there are integers in your text, extract them and then convert them back to a string and add to a list. Anyhow, you can use .split() to split the text on spaces and then try to interpret the splitted strings as integers, like so:
myList = ["code 53 value 281", "code 53 value 25"]
list = []
for var in myList:
list.append(var.split()[-1])
print(list)
Loop through the list and for a particular value at i-th index in the list simply pick the last value.
See code section below:
ans=[]
for i in myList:
ans.append(i.split(" ")[-1])
print(ans)
Related
list1 = ['192,3.2', '123,54.2']
yx = ([float(i) for i in list1])
print(list1)
This is the code I have and I am trying to learn for future reference on how to remove , within a list of string. I tried various things like mapping but the mapping would not work due to the comma within the num.
If you want to remove commas from a string use :
list1 = string.split(",")
the string variable contains your string input, you get your output in the form a list, join the list if you want the original string without the commas.
string_joined = "".join(list1)
string_joined will contain your string without the commas.
If you want your string to just remove the comma and retain the empty space at that position, your syntax :
string = string.replace(","," ")
Also, the fist two syntax I explained, can be shortened to a single syntax :
string = string.replace(",","")
Now if you want to iterate in your list of strings, consider each element(string) in your list one at a time :
for string in list1 :
<your codes go here>
Hope this answers what you are looking for.
we can do regex to remove the non-digits to get rid of other characters
import regex as re
print([float(re.sub("[^0-9|.]", "", s)) for s in list1])
without regex:
[float(s.replace(',','')) for s in list1 ]
output:
[1923.2, 12354.2]
So I have a list that has a strings in the form of a sentence as each element, like this
a = ["This is a sentence with some words.", "And this is a sentence as well.", "Also this right here is a sentence."]
What I want to do with this list is to only keep the third and fourth word of each string, so in the end I want a list like
b = ["a sentence", "is a", "right here"]
The first thing to do I presume is to split the list after spaces, so something like
for x in a:
x.split()
However I'm a bit confused on how to continue. The above loop should produce basically one list per sentence where every word is an own element. I thought about doing this
e = []
for x in a:
x.split()
a = x[0:2]
a = x[2:]
e.append(a)
but instead of removing words it removes characters and I get the following output
['is is a sentence with some words.', 'd this is a sentence as well.', 'so this right here is a sentence.']
I'm not sure why it produces this behavior. I have been sitting at this for a while now and probably missed something really stupid, so I would really appreciate some help.
Nothing can modify a string, they are immutable. You can only derive data from it. As others have said, you need to store the value of .split().
Lists are mutable but slicing them also does not modify them in place, it creates a new sublist which you need to store somewhere. Overall this can be done like so:
e = [' '.join(x.split()[2:4]) for x in a]
The whole thing is a list comprehension in case you're not familiar. .join() converts the sublist back into a string.
When you do x.split(), the output does not take effect on x itself, it results in a list of strings, since strings are not mutable:
lst = s.split(),
Then just join your desired items:
e.append(' '.join(lst[2:4]))
Strings are immutable. x.split() returns a list of strings, but does not modify x. However you do not capture that return value, so it is lost.
I've got a small problem with finding part of string in a list with python.
I load the string from a file and the value is one of the following: (none, 1 from list, 2 from list, 3 from list or more...)
I need to perform different actions depending on whether the String equals "", the String equals 1 element from list, or if the String is for 2 or more elements. For Example:
List = [ 'Aaron', 'Albert', 'Arcady', 'Leo', 'John' ... ]
String = "" #this is just example
String = "Aaron" #this is just example
String = "AaronAlbert" #this is just example
String = "LeoJohnAaron" #this is just example
I created something like this:
if String == "": #this works well on Strings with 0 values
print "something"
elif String in List: #this works well on Strings with 1 value
print "something else"
elif ... #dont know what now
The best way would be to split this String with a pattern from a list. I was trying:
String.Find(x) #failed.
I tried to find similar posts but couldn't.
if String == "": #this works well on Strings with 0 values
print "something"
elif String in List: #this works well on Strings with 1 value
print "something else"
elif len([1 for x in List if x in String]) == 2
...
This is called a list comprehension, it will go through the list and find all of the list elements that have a substring in common with the string at hand, then return the length of that.
Note that there may be some issues if you have a name like "Ann" and "Anna", the name "Anna" in the string will get counted twice. If you need a solution that accounts for that, I would suggest splitting on capital letters to explicitly separate the list into separate names by splitting on capital letters (If you want I can update this solution to show how to do that with regex)
I think the most straightforward approach would be to loop over the list of names and for each of them check if its in your string.
for name in List:
if name in String:
print("do something here")
So, you want to find whether some string contains any members of the given list.
Iterate over the list and check whether the string contains the current item:
for data in List:
if data in String:
print("Found it!")
I have a huge list of lines, each of which looks as follows
1 01 01 some random text
The 1 01 01 part is a reference number that changes from line to line. I want to remove the two whitespaces between the three reference numbers, so that the lines look as follows.
10101 some random text
Obviously, this calls for a for loop. The question is what I should write inside the loop I can't use strip,
for i in my_list:
i.strip()
because that, if anything, would remove all whitespaces, giving me
10101somerandomtext
which I don't want. But if I write
for i in my_list:
i.remove(4)
i.remove(1)
I get an error message 'str' object has no attribute 'remove'. What is the proper solution in this case.
Thanks in advance.
If the number is always at the beginning, you can use the fact that str.replace function takes an optional argument count:
for l in mylist:
print l.replace(' ', '', 2)
Note that I'm doing print here for a reason: you can't change the strings in-place, because strings are immutable (this is also why they don't have a remove method, and replace returns a modified string, but leaves the initial string intact). So if you need them in a list, it's cleaner to create another list like this:
newlist = [l.replace(' ', '', 2) for l in mylist]
It's also safe to overwrite the list like this:
mylist = [l.replace(' ', '', 2) for l in mylist]
Use the count argument for replace, to replace the first 2 spaces.
a = "1 01 01 some random text"
a.replace(" " , "", 2)
>>> '10101 some random text'
split takes a second argument - the number of splits to make
for i in my_list:
components = i.strip(" ", 3)
refnum = ''.join(components[:3])
text = components[3]
Or in python 3:
for i in my_list:
*components, text = i.strip(" ", 3)
refnum = ''.join(components)
I am looking for a specific string in a list; this string is part of a longer string.
Basically i loop trough a text file and add each string in a different element of a list. Now my objective is to scan the whole list to find out if any of the elements string contain a specific string.
example of the source file:
asfasdasdasd
asdasdasdasdasd mystring asdasdasdasd
asdasdasdasdasdasdadasdasdasdas
Now imagine that each of the 3 string is in an element of the list; and you want to know if the list has the string "my string" in any of it's elements (i don't need to know where is it, or how many occurrence of the string are in the list). I tried to get it with this, but it seems to not find any occurrence
work_list=["asfasdasdasd", "asdasdasdasd my string asdasdasdasd", "asdadadasdasdasdas"]
has_string=False
for item in work_list:
if "mystring" in work_list:
has_string=True
print "***Has string TRUE*****"
print " \n".join(work_list)
The output will be just the list, and the bool has_string stays False
Am I missing something or am using the in statement in the wrong way?
You want it to be:
if "mystring" in item:
A concise (and usually faster) way to do this:
if any("my string" in item for item in work_list):
has_string = True
print "found mystring"
But really what you've done is implement grep.
Method 1
[s for s in stringList if ("my string" in s)]
# --> ["blah my string blah", "my string", ...]
This will yield a list of all the strings which contain "my string".
Method 2
If you just want to check if it exists somewhere, you can be faster by doing:
any(("my string" in s) for s in stringList)
# --> True|False
This has the benefit of terminating the search on the first occurrence of "my string".
Method 3
You will want to put this in a function, preferably a lazy generator:
def search(stringList, query):
for s in stringList:
if query in s:
yield s
list( search(["an apple", "a banana", "a cat"], "a ") )
# --> ["a banana", "a cat"]