how to remove particular element from unicode list in python - python

I want to remove specific value from a Unicode list i.e field
u'abv,( field),apn,army,elev,fema'
But when i try something like result.remove ('(field)') it stops working and gives an error ?

Convert it into list and use remove
s = u'abv,( field),apn,army,elev,fema'
res = s.split(",")
res.remove("army") # lets assume we need to remove army
['abv', '( field)', 'apn', 'elev', 'fema']
You can make your output list back to string as well, if you wish
output = ",".join(res)
'abv,( field),apn,elev,fema'

Related

How to separate a list with no commas into a list with commas

I have a list that's just one string with 5 words.
I want to be able to separate the string by using commas so it will end up with a list that has 5 strings. How am I able to do this?
tried a for loop to replace \ with " but for some reason it didn't work.
list=['order_id\tquantity\titem_name\tchoice_description\titem_price']
how do i separate it so that it becomes
list=['order_id','quantity','item_name','choice_description','item_price']
This should split your string into a list.
list = list[0].split("\t")
You can use split() on that original string in that list which you wish to split and store separate strings in a list.
eg.
"your string".split("\t")
First Edit:
Splitting your string with space will given you a tuple of 5 separate strings which your can now store in a list or keep appending to a new list
You can use the split function .
list = 'order_id\tquantity\titem_name\tchoice_description\titem_price'.split('\t')
Edit: You can acces the first element on the original list by doing list[0]. So your code should be:
l = ['order_id\tquantity\titem_name\tchoice_description\titem_price']
l = l[0].split('\t')

how to get second last and last value in a string after separator in python

In Python, how do you get the last and second last element in string ?
string "client_user_username_type_1234567"
expected output : "type_1234567"
Try this :
>>> s = "client_user_username_type_1234567"
>>> '_'.join(s.split('_')[-2:])
'type_1234567'
You can also use re.findall:
import re
s = "client_user_username_type_1234567"
result = re.findall('[a-zA-Z]+_\d+$', s)[0]
Output:
'type_1234567'
There's no set function that will do this for you, you have to use what Python gives you and for that I present:
split slice and join
"_".join("one_two_three".split("_")[-2:])
In steps:
Split the string by the common separator, "_"
s.split("_")
Slice the list so that you get the last two elements by using a negative index
s.split("_")[-2:]
Now you have a list composed of the last two elements, now you have to merge that list again so it's like the original string, with separator "_".
"_".join("one_two_three".split("_")[-2:])
That's pretty much it. Another way to investigate is through regex.

how to remove characters from python list

I have the follow python code which is a list. For some reason I get unwanted characters in the list how can we remove these characters from the beginning and end of list.
stuff = []
a = ('{"type": "push"}')
stuff.append(a)
print(stuff)
outputs
['{"type": "push"}']
How can we remove the [''] and output like
{"type": "push"}
By doing the following :
a = ('{"type": "push"}')
you are appending a string (type(a) returns str). What you might want instead is to append a dictionary
a = {"type": "push"} # ===> output = [{"type": "push"}]
which will give you the right output
I think you are assuming that a will be a tuple. However, that's not the case. In your current implementation it will be a string. That's why when you append(a) the whole string gets appended. Use any other data structures and there is no need of extra quotes that you don't require to be present.

Get rid of numbers from a string in python

I want to get names from a website in a list.
soup = bs4.BeautifulSoup(page.text, 'html.parser')
tbl = soup.find('ul', class_='static-top-names part1')
for link in tbl:
names = link.get_text()
print(names)
So i'm trying to get some names from a website and when i applied above code, i get names as a . When i try to iterate over it i get below output.
John
Mark
Steve and so on.
I want to get rid of the number in the text data and also just want to have the names in a list format.
All i want is to get these pure names and hopefully put them in a list form. Any help?
If the format is always #. name, then you can do the following:
name.split('. ', 1)[1]
Use regular expression for consistency.
import re
s = '1.TEST'
print(re.sub('\d+.','',s))
will give you TEST only. This will eliminate any size of number following with a dot. Basically, replace any number followed by a dot with emptiness.
Iterate over your original list and do the above at the same time using list comprehension
new_list = [re.sub('\d+.','',s) for s in original_list]
This should give you the new list as per your requirement.
You can simply split with '.' dot character or even a space if there is a space before name.
So name.split('' )[-1] name.split('.')[-1] would give just the name. Then you can append those names into a list.
Something like this.
names = [link.get_text().split(' ')[-1] for link in tbl]
This will you the list of just names, i used [-1] as the list index after since your text contains only two items after splitting with space. So if there are more items please use appropriate index.

How can I use list comprehension to replace a comma with an escaped comma in a list that contains strings and numbers

I have a tuple that contains elements that are both strings and numbers, and I am trying to replace a comma that exists in one of the elements of the tuple with an escaped comma, e.g. the input looks like the following
('', 'i_like_cats', 'I like, cookies', 10319708L, "* / Item ID='10319708'", 'i_wish_was_an_oscar_meyer_weiner',
0.101021321)
and what I want as output is
('', 'i_like_cats', 'I like\, cookies', 10319708L, "* / Item ID='10319708'", 'i_wish_was_an_oscar_meyer_weiner',
0.101021321)
What I want to do is replace the , after like with /, because I'm outputting the content to a csv file, so when the next step of the pipeline reads the file it splits the file at the comma between 'like' and 'cookies' even though I don't want it to. I am unable to modify the code of the downstream part of the pipeline so I can't switch to something like a semicolon or tab delimited file, and I can't change.
What I've tried to do is use list comprehension to solve this as follows
line = map(lambda x: str.replace(x, '"', '\"'), line)
but that generates a type error indicating that replace requires a string object but it received a long.
The only solution I can think of is to break the tuple down into it's individual elements, modify the element that contains the comma, and then build a new tuple and append each of the elements back on, but it seems like there has to be a more pythonic way to do that.
Thanks!
I think list comprehension works the best for apply a rule for every element; if you know which single string you would like to change, why can't you just change that single one?
If you want to change all comma into escaped comma, probably try this out:
strtuple = ('', 'i_like_cats', 'I like, cookies', 10319708, "* / Item ID='10319708'", 'i_wish_was_an_oscar_meyer_weiner',
0.101021321)
converted_strlist = [i.replace(',', '\\,') if isinstance(i, str) else i for i in strtuple]
You might want to verify an element is a str instance before calling replace on it.
Something like:
data = (
'',
'i_like_cats',
'I like, cookies',
10319708L,
"* / Item ID='10319708'",
'i_wish_was_an_oscar_meyer_weiner',
0.101021321,
)
line = [
x.replace(",", "\,") if isinstance(x, str) else x for x in data
]

Categories

Resources