Prepend a string in Python - python

I'm trying to prepend zeros to each number in a list if it isn't a the necessary number of digits.
lst = ['1234','2345']
for x in lst:
while len(x) < 5:
x = '0' + x
print(lst)
Ideally this would print ['012345', '02345']

You can use zfill:
Pad a numeric string s on the left with zero digits until the given
width is reached
lst = ['1234','2345']
[s.zfill(5) for s in lst]
# ['01234', '02345']
Or use format method with padding and alignment:
["{:0>5}".format(s) for s in lst]
# ['01234', '02345']

You code doesn't do the job because strings in python are immutable, see this for more info Why doesn't calling a Python string method do anything unless you assign its output?
You could you enumerate in this case like this:
lst = ['1234','2345', "23456"]
for i, l in enumerate(lst):
if len(l) < 5:
lst[i] = '0' + l
print(lst)
['01234', '02345', '23456']

You could use a list comprehension like this:
>>> ['0' * (5-len(x)) + x for x in lst]
['01234', '02345']
Or a list + map try:
>>> list(map(lambda x: '0' * (5-len(x)) + x, lst))
['01234', '02345']

Ultimately, it was a combination of the answers that did the job.
lst = ['1234','2345']
newlst = []
for i in lst:
i = i.zfill(5)
newlst.append(i)
print(newlst)
I apologize if my example wasn't clear. Thank you to all who offered answers!

You can do this:
>>> lst = ['1234','2345']
>>> lst = ['0' * (5 - len(i)) + i for i in lst]
>>> print(lst)
['01234', '02345']

Related

lambda if with list of numbers in python

I try to print from a list, only the organs that match the condition (divided by 2) with the help of a Lambda and something here does not work:
list = list(range(0, 50))
[(lambda x: print(x) if(x % 2 == 0)(x(l)) for l in list]
No need for a lambda for this task, you can just use a list comprehension to generate the sub-list of elements matching the condition:
l = list(range(0, 50))
print([x for x in l if x % 2 == 0])
check it
x = [i for i in range(50) if not i %2]
You don't need to use lambda for this. You can do this way:
list = list(range(50))
[i for i in list if i%2==0]

How to change the nth character of every string in a list in Python

Let's say I have the following list of strings:
list = ['ABC.010120.01', 'ABC.010220.02', 'ABC.010220.03']
I want to change the 11th character of each string, meaning the 2nd '.', to a '-'.
I've tried:
list = [n[:10] + '-' + n[11:] for n in list]
However this gives me the error:
TypeError: 'float' object is not subscriptable
The problem is that you have a float somewhere in your list. You could use a for loop using enumerate and str to solve it:
lst = ['ABC.010120.01', 'ABC.010220.02', 'ABC.010220.03']
for index, item in enumerate(lst):
item = str(item)
lst[index] = item[0:10] + "-" + item[11:]
Or, as a list comprehension:
new_lst = [item[0:10] + "-" + item[11:]
for x in lst
for item in [str(x)]]
Also, avoid calling your variables like builtin-objects (list, dict and the like).
>>> a = ['ABC.010120.01', 'ABC.010220.02', 'ABC.010220.03']
>>> a = [n[:10] + '-' + n[11:] for n in a]
>>> a
['ABC.010120-01', 'ABC.010220-02', 'ABC.010220-03']
Note that list, as str, set and some others are word reserved for python3.
What you're doing it's like change the base of an int, for example.
The issue is that there are nan float values in the list. I found that following this solution linked here worked to remove the nan's from my list as so:
lst = ['ABC.010120.01', 'ABC.010220.02', 'ABC.010220.03', nan]
lst = [x for x in lst if x == x]
Executing that beforehand allows this line to function without error:
lst = [n[:10] + '-' + n[11:] for n in last]

Python: Add Same Prefix to All Elements in List

Let's say I have the following Python List:
['7831-0', nan, '3165-0', '7831-0', '7831-1']
I want to add the same prefix ('ADD_' to each element in the above list. I also want to remove the nan from my list. My desired output list is as follows:
list = ['ADD_7831-0', 'ADD_3165-0', 'ADD_7831-0', 'ADD_7831-1']
I tried the following code:
prefix_ADD = 'ADD_'
new_list = [prefix_ADD + x for x in list]
But I get the following error:
TypeError: must be str, not float
[prefix_ADD + x for x in list if not str(x) == "nan"]
Is one way you could filter out nan
your_list = ['7831-0', float('nan'), '3165-0', '7831-0', '7831-1']
print(your_list) # ['7831-0', nan, '3165-0', '7831-0', '7831-1']
prefix_ADD = 'ADD_'
new_list = [prefix_ADD + x for x in your_list if isinstance(x, str)]
print(new_list) # ['ADD_7831-0', 'ADD_3165-0', 'ADD_7831-0', 'ADD_7831-1']
Maybe a less elegant solution but how about this:
new_list=[]
old_list = ['7831-0', nan, '3165-0', '7831-0', '7831-1']
prefix_ADD = 'ADD_'
for x in old_list:
if x != nan:
new_list.append(prefix_ADD + x)
print(new_list)
# ['ADD_7831-0', 'ADD_3165-0', 'ADD_7831-0', 'ADD_7831-1']

Replacing selected parts of an item in Python list

I have a list looking as follows
lst = ['.ab.cd.ef.gh.', '.ij.kl.mn.op.']
In each item, I want to replace item[0] with a * and item[-1] with a $.
I tried to use:
[item.eplace(item[0], '*') for item in lst]
but the result is that all . get replaced with * irrespective of position.
Appreciating your help!
This will work:
>>> lst = ['.ab.cd.ef.gh.', '.ij.kl.mn.op.']
>>> ['*' + item[1:-1] + '$' for item in lst]
['*ab.cd.ef.gh$', '*ij.kl.mn.op$']
>>>
item[1:-1], which uses Explain Python's slice notation, will get every character in item except for the first and the last:
>>> 'abcde'[1:-1]
'bcd'
>>> '*' + 'abcde'[1:-1] + '$' # Add the characters we want on each end
'*bcd$'
>>>
Use this code:
lst = ['.ab.cd.ef.gh.', '.ij.kl.mn.op.']
for k in range(0, len(lst)):
item = lst[k]
lst[k] = '*'+item[1:-1]+'$'
print lst
This loops over every item with a for loop and range(), and assigns lst[k] to item. Then it uses item[1:-1] to get the string excluding the first and last characters. Then we use string concatenation to add an asterisk to the beginning, and a dollar sign to the end. This runs as:
>>> lst = ['.ab.cd.ef.gh.', '.ij.kl.mn.op.']
>>> for k in range(0, len(lst)):
... item = lst[k]
... lst[k] = '*'+item[1:-1]+'$'
...
>>> print lst
['*ab.cd.ef.gh$', '*ij.kl.mn.op$']
>>>

How to split elements of a list?

I have a list:
my_list = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
How can I delete the \t and everything after to get this result:
['element1', 'element2', 'element3']
Something like:
>>> l = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
>>> [i.split('\t', 1)[0] for i in l]
['element1', 'element2', 'element3']
myList = [i.split('\t')[0] for i in myList]
Try iterating through each element of the list, then splitting it at the tab character and adding it to a new list.
for i in list:
newList.append(i.split('\t')[0])
Do not use list as variable name.
You can take a look at the following code too:
clist = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847', 'element5']
clist = [x[:x.index('\t')] if '\t' in x else x for x in clist]
Or in-place editing:
for i,x in enumerate(clist):
if '\t' in x:
clist[i] = x[:x.index('\t')]
Solution with map and lambda expression:
my_list = list(map(lambda x: x.split('\t')[0], my_list))
I had to split a list for feature extraction in two parts lt,lc:
ltexts = ((df4.ix[0:,[3,7]]).values).tolist()
random.shuffle(ltexts)
featsets = [(act_features((lt)),lc)
for lc, lt in ltexts]
def act_features(atext):
features = {}
for word in nltk.word_tokenize(atext):
features['cont({})'.format(word.lower())]=True
return features

Categories