I have a string like "['apple' 'bat' 'cat']".
The string need to convert into array like:['apple','bat','cat']
remove the first [ and last ] elements of your string
then split the remaining string into their elements
Iterate then each element and remove the opening and closing quote (first and last element)
Merge everything in a list comprehension
my_string = "['apple' 'bat' 'cat']"
result = [i[1:-1] for i in my_string[1:-1].split(' ')]
print(result)
['apple', 'bat', 'cat']
Related
I have a list of strings. I would like to extract the second word in each string and save it to a new list. Some of the elements only have a single word.
l = ["OPTY PLN EXTRCT","MRKT COMMUNITY TABLE", "COMM", "EXT OPTY EMP"]
Desired Output:
output = ['PLN', 'COMMUNITY', 'OPTY']
A List Comprehension works to extract the first word.
[i.split()[0] for i in l]
The code below throws an index error because there are elements in the list with only one word.
[i.split()[1] for i in l]
If the element only has a single word, I would like the iteration to skip over it and continue on.
I am playing with a Try and except but have not successfully gotten it to work.
You could just add if condition to your comprehension like ... if len(i.split()) > 1] however that would require you to split your words twice, here we're doing it once for every element of l by using map().
l = ["OPTY PLN EXTRCT","MRKT COMMUNITY TABLE", "COMM", "EXT OPTY EMP"]
output = [words[1] for words in map(str.split, l) if len(words) > 1]
Outputs:
['PLN', 'COMMUNITY', 'OPTY']
Alternatively if you're using python3.8 we could use new := notation.
output = [words[1] for i in l if len(words := i.split()) > 1]
words = [w.split(None, 3)[1] for w in l if any(x.isspace() for x in w.strip())]
This will exclude a word from the list if it is an empty string, consists only of whitespaces and if it does contain characters but has no whitespaces in between. Therefore you should have at least two words for it to split and extract. Note that my splitting solution stops the splitting after the second word, so you may have a lot of them in the string, but split() will not lose time by processing them all.
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]
I have a list in python that contains strings like['abc','defg',xyz','lmnopqrst']. I need to iterate through the list and extract elements in the 2nd and 3rd position and create another list with just those letters
ex : O/P should be a list which has ['bc','ef','yz','mn']
You can do like this:
new_list = [ s[1:3] for s in your_list ]
Let's use list comprehensions and slice notation:
output_list = [s[1:3] for s in input_list]
I need to split a string. I am using this:
def ParseStringFile(string):
p = re.compile('\W+')
result = p.split(string)
But I have an error: my result has two empty strings (''), one before 'Лев'. How do I get rid of them?
As nhahtdh pointed out, the empty string is expected since there's a \n at the start and end of the string, but if they bother you, you can filter them very quickly and efficiently.
>>> filter(None, ['', 'text', 'more text', ''])
['text', 'more text']
You could remove all newlines from the string before matching it:
p.split(string.strip('\n'))
Alternatively, split the string and then remove the first and last element:
result = p.split(string)[1:-1]
The [1:-1] takes a copy of the result and includes all indexes starting at 1 (i.e. removing the first element), and ending at -2 (i.e. the second to last element. The second index is exclusive)
A longer and less elegant alternative would be to modify the list in-place:
result = p.split(string)
del result[-1] # remove last element
del result[0] # remove first element
Note that in these two solutions the first and last element must be the empty string. If sometimes the input doesn't contain these empty strings at the beginning or end, then they will misbehave. However they are also the fastest solutions.
If you want to remove all empty strings in the result, even if they happen inside the list of results you can use a list-comprehension:
[word for word in p.split(string) if word]
I have a problem, in a 2D list:
t = [['\n'], ['1', '1', '1', '1\n']]
I want to remove the "\n" from the nested lists.
You can strip all strings in the nested lists:
t = [[s.strip() for s in nested] for nested in t]
This would remove all whitespace (spaces, tabs, newlines, etc.) from the start and end of each string.
Use str.rstrip('\n') if you need to be more precise:
t = [[s.rstrip('\n') for s in nested] for nested in t]
If you need to remove empty values too, you may have to filter twice:
t = [[s.rstrip('\n') for s in nested if not s.isspace()] for nested in t]
t = [nested for nested in t if nested]
where the first line only includes a stripped string if it contains more than just whitespace, and the second loop removes entirely empty lists. In Python 2, you could also use:
t = filter(None, nested)
for the latter line.