Remove amp; in the list [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to remove the amp; from all the data inside the list
['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11', '?daypartId=1&catId=10', '?daypartId=1&catId=6', '?daypartId=1&catId=4', '?daypartId=1&catId=14', '?daypartId=1&catId=5', '?daypartId=1&catId=3', '?daypartId=1&catId=8']
desired output without amp;
['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11',...]

Using re.sub():
import re
data = ['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11', '?daypartId=1&catId=10', '?daypartId=1&catId=6', '?daypartId=1&catId=4', '?daypartId=1&catId=14', '?daypartId=1&catId=5', '?daypartId=1&catId=3', '?daypartId=1&catId=8']
data = [re.sub(r'amp;', '', item) for item in data]

A simple list comprehension is best using replace
my_list=['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11', '?daypartId=1&catId=10', '?daypartId=1&catId=6', '?daypartId=1&catId=4', '?daypartId=1&catId=14', '?daypartId=1&catId=5', '?daypartId=1&catId=3', '?daypartId=1&catId=8']
[i.replace('&','') for i in my_list]
using regex
import re
[re.sub('&','',i) for i in my_list]
Output
['?daypartId=1;catId=12',
'?daypartId=1;catId=1',
'?daypartId=1;catId=2',
'?daypartId=1;catId=11',
'?daypartId=1;catId=10',
'?daypartId=1;catId=6',
'?daypartId=1;catId=4',
'?daypartId=1;catId=14',
'?daypartId=1;catId=5',
'?daypartId=1;catId=3',
'?daypartId=1;catId=8']

Related

How can I find an element or his index in a list by a few characters of it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How can I find an element or his index in a list by a few characters of it?
For Example:
x = input()
list = ['alberto', 'kari', 'mino']
#now I want to find the element, which starts or contain x (for exampke x = 'albe')
#the next similir element should be found
#and then remove it
for item in list:
If 'alb' in item:
Item.pop()

Convert list of list into dictionary [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a list of list as follows
my_list = [['val1','12'],['val2', 'disabled'],['apple', 10],['banana', 20]]
i would like to convert my_list into dictionary such that my_dict looks like
my_dict = {'val1':'12','val2':'disabled','apple':10, 'banana:20}
Just issue dict(my_list). The dict constructor accepts any iterable of two-element iterables.
You could write your own code like:
my_dict = {}
for i in my_list:
my_dict[i[0]] = i[1]
print(my_dict)

Coverting a python list into string with comma separated in efficient way [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a list of acc nos:
list1 = ['1234','3456','2345','5543','1344','5679','6433','3243','0089']
Output I need is a string:
print(output): '1234','3456','2345','5543','1344','5679','6433','3243','0089'
You can join all the values with ',' and then adding a ' before and after the string like this:
"'{0}'".format("','".join(list1))
>>> list1 = ['1234','3456','2345','5543','1344','5679','6433','3243','0089']
>>> print(','.join(["'{0}'".format(s) for s in list1]))
'1234','3456','2345','5543','1344','5679','6433','3243','0089'

Efficient way of parsing string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How would you turn a string that looks like this
7.11,8,9:00,9:15,14:30,15:00
into this dictionary entry
{7.11 : [8, (9:00, 9:15), (14:30, 15:00)]}?
Suppose that the number of time pairs (such as 9:00,9:15 and 14:30,15:00 is unknown and you want to have them all as tuple pairs.
First split the string at the commas, then zip cluster starting from the 3rd element and put it into a dictionary:
s = "7.11,8,9:00,9:15,14:30,15:00"
ss = s.split(',')
d = {ss[0]: [ss[1]] + list(zip(*[iter(ss[2:])]*2))}
Output:
{'7.11': ['8', ('9:00', '9:15'), ('14:30', '15:00')]}
If you need to convert it from string to appropiate data types (you'll have to adapt it according to your needs), then after getting the ss list:
time_list = [datetime.datetime.strptime(t,'%H:%M').time() for t in ss[2:]]
d = {float(ss[0]): [int(ss[1])] + list(zip(*[iter(time_list)]*2))}

select the characters in after "):" and print it in between? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a file like this
(E:0.13228,((D:0.08440,A:0.14071):0.29270,(H:0.30329,(B:0.06928,
(F:0.00236,G:0.00010):0.00010):0.44531):0.06201):0.57269,C:0.19183);
I need to generate it as follows:
(E:0.13228,((D:0.08440,A:0.14071)0.29270:0.29270,(H:0.30329,(B:0.06928,
(F:0.00236,G:0.00010)0.00010:0.00010)0.44531:0.44531)0.06201:0.06201)0.57269:0.57269,C:0.19183);
What you should use in this cases are the regular expressions. Check Python re
match_pattern = r'\):(\d(\.\d+)?)'
output_pattern = r')\1:\1'
input_str = """(E:0.13228,((D:0.08440,A:0.14071):0.29270,(H:0.30329,(B:0.06928,
(F:0.00236,G:0.00010):0.00010):0.44531):0.06201):0.57269,C:0.19183);"""
output_str = re.sub(match_pattern, output_pattern, input_str)
print(output_str)
And the result is:
(E:0.13228,((D:0.08440,A:0.14071)0.29270:0.29270,(H:0.30329,(B:0.06928,
(F:0.00236,G:0.00010)0.00010:0.00010)0.44531:0.44531)0.06201:0.06201)0.57269:0.57269,C:0.19183);
I guess you could go with some string manipulation like so if you're not using regular expressions
data = "(E:0.13228,((D:0.08440,A:0.14071):0.29270,(H:0.30329,(B:0.06928,
(F:0.00236,G:0.00010):0.00010):0.44531):0.06201):0.57269,C:0.19183);"
dataParts = data.split("):")
correctedData = dataParts[0]
for dataPart in dataParts[1:]:
number = dataPart[:7]
correctedData = "){}:".format(number).join([correctedData, dataPart])
but that's not clean...

Categories