How to delete an empty list in a list? (Python) - python

I wrote a function which takes a .txt file.
The first thing it does is split the file at ',' and add them to a list, which creates a list of lists.I used:
lst = s.split(',')
I do get a list of lists, except every second line has an empty list ['\n']. I need to find a way get rid of these empty lists as they muck up the rest of the code.
Is there any simple way of doing this? Or is it just that I doing something wrong?
Any help would be greatly appreciated.
Thank You!
Sample Data:
1,2,3,4
,3,4,
Expected Output:
['1','2','3','4\n']
['','3','4','\n']
Current Output:
['1','2','3','4\n']
['\n']
['','3','4','\n']
Output after using sshashank124's suggestion:
['1','2','3','4\n']
[]
['','3','4','\n']
Output after using Alex Thornton's suggestion:
['1','2','3','4\n']
[]
['','3','4','\n']

Use strip() (or rstrip()) to get rid of new-line characters:
lst = s.strip().split(',')
See also: How can I remove (chomp) a newline in Python?

You can simply do it as:
lst = [i for i in s.split(',') if i != '\n']
Example
>>> s = 'hello,\n,world,\n,bye,\n,world'
>>> lst = [i for i in s.split(',') if i != '\n']
>>> print lst
['hello', 'world', 'bye', 'world']

lst = filter(lambda a: len(a)==1 and a[0]!='\n' or len(a)>1, lst)
This will clear the list of empty list [ ] or list containing only \n.
But it's probably better to clear the text of unwanted strings BEFORE splitting it to a list.
Edit
note at the end of code you have to replace the lst with your list containing the unwanted characters.
also you could try removing the \n from the original text file with this code.
text_file = text_file.replace('\n', '')

If your sample data doesn't have a blank line between the two populated lines, then it seems to me you are having a line-end issue upon reading the text file in the first place. If you post how you're reading in the text file, you could get answers that fix your problem before it ever gets to the point where you have any empty lists to remove.
That said, if lst really does just have [\n] as every other element, you can simply skip them as follows:
lst = s.strip().split(',')[::2]
(Note I've already incorporated the strip mentioned in previous answers to remove the newline characters.)

Related

How to remove string quotes from list in Python

I know there are plenty of titles that sound the same believe me I looked however this has to do with the quotes which make it a string which I have seen very little of and no solutions which worked for me.
I am trying to create a list which i can then put into a database there should normally be a couple hundred items to my list however the problem is the outside quotes turn the entire list into one big element and have had little luck trying to fix it
list with problem
list1 = ['("a"),("b"),("c"),("d"),("e")']
what I need
list1 = [("a"),("b"),("c"),("d"),("e")]
I have tried multiple things so far I have tried...
list1 = ['("a"),("b"),("c"),("d"),("e")']
listFormated = [x.replace("'", '') for x in list1]
instead of removing the outside quote from the list it took all quote marks from inside the string element and removed those instead.
for example
list1= ['('a'),('b')('c')("let's")']
now becomes
list1= ['(a),(b),(c),("lets")']
Also Ive tried ast.literal_eval and it almost worked but take a look at the results
from ast import literal_eval
listFormated = [literal_eval(i) for i in list1]
but this returns
[('a','b','c',"let's")]
I would really appreciate any help someone could give.
EDIT:
I have not communicated correctly that I am trying to put bible text into a list in which each line is in a tuple so I can place it into an sqlite db so there wont just be single letters but multiple letters with apostrophes Collins and such and one of the answers were close but it splits each letter into a tuplized list and I need a way to keep the original formatting. Here is my first few lines to give a better example
['(" In the beginning God created the Heauen, and the Earth. "), (" And the ear th was without forme, and voyd, and darkenesse was vpon the face of the deepe: and the Spirit of God mooued vpon the face of the waters. "), (" And God said, Let there be light: and there was light. "), (" And God saw the light, that it was good: and God diuided the light from the darkenesse. ")']
UPDATED
Is it good enough?
list1 = ['("a"),("b"),("c"),("d"),("e")']
list2 = list1[0].split(',')
list2
>> ['("a")', '("b")', '("c")', '("d")', '("e")']
If you actually need a list of single itemed tuples:
[tuple([x[2]],) for x in list1[0].split(',')]
>> [('a',), ('b',), ('c',), ('d',), ('e',)]
Note: As far as I understand, this is how Python shows tuples with single item in it
From your example, it looks like you have a list with one value, which is a string. Is that really what you want?
Otherwise, if you have the string '("a"),("b"),("c"),...', and want to convert it to a list, you could use the split method:
stringified_list: str = '("a"),("b"),("c")'
list_of_strings: List[str] = stringified_list.split(",")
print(list_of_strings) # Will print ['("a")', '("b")', '("c")']
If you want to return only the strings, you could use ast.literal_eval:
import ast
stringified_list: str = '("a"),("b"),("c")'
list_of_strings: List[str] = stringified_list.split(",")
list_of_letters: List[str] = [ast.literal_eval(letter) for letter in list_of_strings]
print(list_of_letters) # Will print ['a', 'b', 'c']
Or, if you would like to return tuples of the strings, you could cast the literal_eval(letter) to tuple
list_of_tuples: List[Tuple[str]] = [tuple(ast.literal_eval(letter)) for letter in list_of_strings]
print(list_of_letters) # Will print [('a',), ('b',), ('c',)]
You can read more about ast.literal_eval here
Quick note: If from the begging you had the squared brackets inside your string, you could have used ast.literal_eval on the input:
import ast
stringified_list: str = '[("a"),("b"),("c")]'
list_of_strings: List[str] = ast.literal_eval(stringified_list)
print(list_of_strings) # Will print ['a', 'b', 'c']
Also, here is why you shouldn't use eval directly.
After edit
There are two methods of solving it.
The first, you could loop over to list, and each time you find an opening parentheses you can start aggregating the following strings into some variable. Later, when you reach the closing parentheses, you can say that this sentence is finished and repeat the process with the next one.
txt = "('Fizz, Buzz!'), ('fizz buzz')"
sentances = []
last_sentance = ''
in_sentance = False
for t in txt:
if t == ')':
in_sentance = False
sentances.append(last_sentance)
last_sentance = ''
continue
if t == '(':
in_sentance = True
continue
if in_sentance:
last_sentance += t
print(sentences) # This will print ['Fizz, Buzz!', 'fizz buzz']
This isn't the best piece of code I wrote, but you get the idea.
Another method is to split using python regex:
import re
txt = "('Fizz, Buzz!'), ('fizz buzz')"
splitted = re.split("\(+(.*?)\)", txt,)
print(splitted) # This will print ['Fizz, Buzz!', ', ', 'fizz buzz']
You can than remove the commas using
filtered = list(filter((', ').__ne__, x))
print(filtered) # This will print ['Fizz, Buzz!', 'fizz buzz']
After that you can do the manipulations you want on this list, e.g. converting each item into a tuple etc.
This should do what you're trying to do.
list1 = ['("a"),("b"),("c"),("d"),("e")']
list1 = [eval(list1[0])] # since the string is valid python, it can just be evalled
>>> list1
["a", "b", "c", "d", "e"]
Also, why does it contain a string in the first place? It seems like you'd rather have other data types, so why use strings?

How to execute it correctly?

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]

Is there a reverse \n?

I am making a dictionary application using argparse in Python 3. I'm using difflib to find the closest matches to a given word. Though it's a list, and it has newline characters at the end, like:
['hello\n', 'hallo\n', 'hell\n']
And when I put a word in, it gives a output of this:
hellllok could be spelled as hello
hellos
hillock
Question:
I'm wondering if there is a reverse or inverse \n so I can counteract these \n's.
Any help is appreciated.
There's no "reverse newline" in the standard character set but, even if there was, you would have to apply it to each string in turn.
And, if you can do that, you can equally modify the strings to remove the newline. In other words, create a new list using the current one, with newlines removed. That would be something like:
>>> oldlist = ['hello\n', 'hallo\n', 'hell\n']
>>> oldlist
['hello\n', 'hallo\n', 'hell\n']
>>> newlist = [s.replace('\n','') for s in oldlist]
>>> newlist
['hello', 'hallo', 'hell']
That will remove all newlines from each of the strings. If you want to ensure you only replace a single newline at the end of the strings, you can instead use:
newlist = [re.sub('\n$','',s) for s in oldlist]

How can I merge these two lines of code into one (Python 3.X)?

text = ''.join(sorted([x for x in input()]))
text = text.replace('+', '', text.count('+'))
I just it love it when you can do all sort of things in one line with Python.
text = ''.join(sorted(input())).replace('+', '')
OR
text = ''.join(sorted(input().replace('+', '')))
You don't need to use list comprehension. Just pass the input() to sorted(); sorted support any iterable.
the 3rd argument to str.replace() is redundant. Because the code is replacing all occurences of +.
Ok, this is not exactly the same code, but in this case result is similar:
text = ''.join(sorted([x for x in input() if x != '+']))
Instead of creating whole string and then replacing one character, you can simply remove it in first list comperhesion.

How to split a list of a giant single string into seperate substrings

So I have a list of 3 giant strings, such as:
lst=['Mary,had,"3",little-lambs','Mary,sold-her,"3",lambs,away','Mary,was,sad']
that list has giant strings, but I want to split those strings into seperate little strings in a list (so for the first one I want:
ls=['Mary','had','3','little','lambs']
and so on. I tried
.split
but it wont work because its a list and that is a string method. I need a completely non-pythonic way please. (Also if anyone can help with the next step, I'm tring to put the last value (in this case lambs,away,sad into a dictionary as keys to mary.
For example like:
dictionary={"lambs": "Mary","away":"Mary","sad":"Mary"}
because later I need to indicate to Mary(the values), and all the keys associated with Mary should pop up. If anyone can help it would be greatly appreciated, I'm really stuck, and any help should be completely non-pythonic please.
Edit: I have used a for loop and split each thing and appended it to a new list, but the result creates a list of the list of strings
lst1=[]
for item in lst:
item=item.split(",")
lst1.append(item)
print(content)
print(lst1)
lst1=[['Mary','had','3','little','lambs']]
I'm trying to avoid creating a list inside another list because I dont know how to index each part of it to create the dictionary I mentioned earlier
Just use re.split() in the above answer.
import re
last_keys = dict()
for item in lst:
tokens = re.split('-|,',item)
last_keys[tokens[-1]] = "Mary"
last_keys
{'lambs': 'Mary', 'away': 'Mary', 'sad': 'Mary'}
EDIT: Changed the split to be a re.split including regex specifically for the "," and "-" characters. Should be modified to match necessary delimiters.
This will give you the dictionary you need. You can append tokens to a separate list to get a list of the individual words per string.
import re
last_keys = dict()
for item in lst:
tokens = re.split(r',|\-', item)
last_keys[tokens[-1]] = "Mary"

Categories