python splitting string twice - python

I'm trying to split a string twice with the goal of converting a>b where b contains numbers and is split into multiple x/y pairs
a = '{1;5}{2;7}{3;9}{4;8}'
b = [[1,5],[2,7],[3,9],[4,8]]
my code is currently this...
b = a.split('}{')
for item in b:
item.replace('{','')
item.replace('}','')
item.split(';')
the first split works correctly and returns this
b = ['{1;5','2;7','3;9','4;8}']
but manipulating the 'items in b' does not appear to work

You can use a list comprehension to do both splits at once and return a list of lists:
>>> a = '{1;5}{2;7}{3;9}{4;8}'
>>> [item.split(';') for item in a[1:-1].split('}{')]
[['1', '5'], ['2', '7'], ['3', '9'], ['4', '8']]

You have to actually modify the list b by interacting with its item. You should do something like this:
for i, s in enumerate(b):
b[i] = s.replace('{','')
b[i] = s.replace('}','')
b[i] = s.split(';')
In [9]: b
Out[9]: [['{1', '5'], ['2', '7'], ['3', '9'], ['4', '8}']]
I dont know if that's your expected output
Here are two examples where you are not affecting the list b
for item in b:
item = item.replace('{','')
item = item.replace('}','')
item = item.split(';')
In [21]: b = ['{1;5','2;7','3;9','4;8}']
In [22]: for item in b:
item = item.replace('{','')
item = item.replace('}','')
item = item.split(';')
In [23]: b
Out[23]: ['{1;5', '2;7', '3;9', '4;8}']
This one wouldnt do anything to the list b neither.
for item in b:
item.replace('{','')
item.replace('}','')
item.split(';')

This can also be done using regular expressions.
The items you are looking for inside the input string consist of
two numbers \d+
separated by a semicolon ;
enclosed in curly braces \{, \}.
The complete pattern looks like this:
pattern = r'\{(\d+);(\d+)\}'
The additional parentheses () define groups which allow extracting the numbers, for example with re.findall:
>>> for item in re.findall(pattern, a):
>>> print item
('1', '5')
('2', '7')
('3', '9')
('4', '8')
Then it is a simple matter of mapping int over the items to get the desired result:
>>> [map(int, item) for item in re.findall(pattern, a)]
[[1, 5], [2, 7], [3, 9], [4, 8]]
Some prefer list comprehensions over map:
>>> [[int(x) for x in item] for item in re.findall(pattern, a)]
[[1, 5], [2, 7], [3, 9], [4, 8]]

The function call
item.replace('{','')
does not do anything to item, since it returns a new string after the replacement. Instead, try:
item = item.replace('{','')
Similar changes need to be made for the other lines in that block.

Related

How to convert list elements to string using Python?

How should I convert list elements to string using Python?
For example I have a list that looks like this:
[1, 2, 3, 4, 5]
And I want it to look like this:
['1', '2', '3', '4', '5']
Use a one-liner to iterate over all elements it an Iterable and use str(element) to cast the element as a string
new_list = [str(i) for i in old_list]
def listToString(a):
li = []
for i in a:
li.append(str(i))
return li
We can input the list in the function.

Changing a list into a string

I have a list like this:
li = [1, 2, 3, 4, 5]
I want to change it into a string, get rid of the quotes, and get rid of the commas so that it looks like this:
1 2 3 4 5
I tried the following:
new_list = []
new_list.append(li)
new_string = " ".join(new_list)
print new_string
however I get the below error:
TypeError: sequence item 0: expected str instance, int found
Why does this happen and how can I fix this so that I get the output I want?
The items in the list need to be of the str type in order to join them with the given delimeter. Try this:
' '.join(map(str, your_list)) # join the resulting iterable of strings, after casting ints
This is happening because join is expecting an iterable sequence of strings, and yours contains int.
You need to convert this list to string either by using list comprehension:
>>> li
[1, 2, 3, 4, 5]
>>> new_li = [str(val) for val in li]
>>> new_li
['1', '2', '3', '4', '5']
or a regular for loop:
>>> for x in range(len(li)):
... li[x] = str(li[x])
...
>>> li
['1', '2', '3', '4', '5']
then your expression will work.
>>> result = ' '.join(li)
>>> result
'1 2 3 4 5'
The error is from attempting to join integers into a string, you could do this to turn every value into a string, then join them.
new_list = [str(x) for x in li]
new_string = " ".join(new_list)
As a one-liner:
new_string = " ".join([str(x) for x in li])

Python: How can I take a list of lists, convert every element into strings, and return the list of lists?

For example:
list = [[11,2,3,5],[5,3,74,1,90]]
returns the same thing, only everything is a str instead of an int.
I want to be able to use .join on them. Thanks!
If you only ever go 2 lists deep:
>>> l = [[11, 2, 3, 5], [5, 3, 74, 1, 90]]
>>> [[str(j) for j in i] for i in l]
[['11', '2', '3', '5'], ['5', '3', '74', '1', '90']]
I'd use a list-comp and map for this one:
[ map(str,x) for x in lst ]
But I suppose py3.x would need an addition list in there (yuck).
[ list(map(str,x)) for x in lst ]
As a side note, you can't use join on this list we return anyway. I'm guessing you want to do something like this:
for x in lst:
print ("".join(x))
If that's the case, you can forgo the conversion all together and just do it when you're joining:
for x in lst:
print ("".join(str(item) for item in x))

Add entry to beginning of list and remove the last one

I have a list of about 40 entries. And I frequently want to append an item to the start of the list (with id 0) and want to delete the last entry (with id 40) of the list.
How do I do this the best?
Example with 5 entries:
[0] = "herp"
[1] = "derp"
[2] = "blah"
[3] = "what"
[4] = "da..."
after adding "wuggah" and deleting last it should be like:
[0] = "wuggah"
[1] = "herp"
[2] = "derp"
[3] = "blah"
[4] = "what"
And I don't want to end up manually moving them one after another all of the entries to the next id.
Use collections.deque:
>>> import collections
>>> q = collections.deque(["herp", "derp", "blah", "what", "da.."])
>>> q.appendleft('wuggah')
>>> q.pop()
'da..'
>>> q
deque(['wuggah', 'herp', 'derp', 'blah', 'what'])
Use insert() to place an item at the beginning of the list:
myList.insert(0, "wuggah")
Use pop() to remove and return an item in the list. Pop with no arguments pops the last item in the list
myList.pop() #removes and returns "da..."
Use collections.deque
In [21]: from collections import deque
In [22]: d = deque([], 3)
In [24]: for c in '12345678':
....: d.appendleft(c)
....: print d
....:
deque(['1'], maxlen=3)
deque(['2', '1'], maxlen=3)
deque(['3', '2', '1'], maxlen=3)
deque(['4', '3', '2'], maxlen=3)
deque(['5', '4', '3'], maxlen=3)
deque(['6', '5', '4'], maxlen=3)
deque(['7', '6', '5'], maxlen=3)
deque(['8', '7', '6'], maxlen=3)
Here's a one-liner, but it probably isn't as efficient as some of the others ...
myList=["wuggah"] + myList[:-1]
Also note that it creates a new list, which may not be what you want ...
Another approach
L = ["herp", "derp", "blah", "what", "da..."]
L[:0]= ["wuggah"]
L.pop()

How to convert list of intable strings to int

In Python, I want to convert a list of strings:
l = ['sam','1','dad','21']
and convert the integers to integer types like this:
t = ['sam',1,'dad',21]
I tried:
t = [map(int, x) for x in l]
but is showing an error.
How could I convert all intable strings in a list to int, leaving other elements as strings?
My list might be multi-dimensional. A method which works for a generic list would be preferable:
l=[['aa','2'],['bb','3']]
I'd use a custom function:
def try_int(x):
try:
return int(x)
except ValueError:
return x
Example:
>>> [try_int(x) for x in ['sam', '1', 'dad', '21']]
['sam', 1, 'dad', 21]
Edit: If you need to apply the above to a list of lists, why didn't you converted those strings to int while building the nested list?
Anyway, if you need to, it's just a matter of choice on how to iterate over such nested list and apply the method above.
One way for doing that, might be:
>>> list_of_lists = [['aa', '2'], ['bb', '3']]
>>> [[try_int(x) for x in lst] for lst in list_of_lists]
[['aa', 2], ['bb', 3]]
You can obviusly reassign that to list_of_lists:
>>> list_of_lists = [[try_int(x) for x in lst] for lst in list_of_lists]
How about using map and lambda
>>> map(lambda x:int(x) if x.isdigit() else x,['sam','1','dad','21'])
['sam', 1, 'dad', 21]
or with List comprehension
>>> [int(x) if x.isdigit() else x for x in ['sam','1','dad','21']]
['sam', 1, 'dad', 21]
>>>
As mentioned in the comment, as isdigit may not capture negative numbers, here is a refined condition to handle it notable a string is a number if its alphanumeric and not a alphabet :-)
>>> [int(x) if x.isalnum() and not x.isalpha() else x for x in ['sam','1','dad','21']]
['sam', 1, 'dad', 21]
I would create a generator to do it:
def intify(lst):
for i in lst:
try:
i = int(i)
except ValueError:
pass
yield i
lst = ['sam','1','dad','21']
intified_list = list(intify(lst))
# or if you want to modify an existing list
# lst[:] = intify(lst)
If you want this to work on a list of lists, just:
new_list_of_lists = map(list, map(intify, list_of_lists))
For multidimenson lists, use recursive technique may help.
from collections import Iterable
def intify(maybeLst):
try:
return int(maybeLst)
except:
if isinstance(maybeLst, Iterable) and not isinstance(lst, str):
return [intify(i) for i in maybeLst] # here we call intify itself!
else:
return maybeLst
maybeLst = [[['sam', 2],'1'],['dad','21']]
print intify(maybeLst)
Use isdigit() to check each character in the string to see if it is a digit.
Example:
mylist = ['foo', '3', 'bar', '9']
t = [ int(item) if item.isdigit() else item for item in mylist ]
print(t)
Use a list comprehension to validate the numeracy of each list item.
str.isnumeric won't pass a negative sign
Use str.lstrip to remove the -, check .isnumeric, and convert to int if it is.
Alternatively, use str.isdigit in place of .isnumeric.
Keep all values in the list
l = ['sam', '1', 'dad', '21', '-10']
t = [int(v) if v.lstrip('-').isnumeric() else v for v in l]
print(t)
>>> ['sam', 1, 'dad', 21, -10]
Remove non-numeric values
l = ['sam', '1', 'dad', '21', '-10']
t = [int(v) for v in t if v.lstrip('-').isnumeric()]
print(t)
>>> [1, 21, -10]
Nested list
l = [['aa', '2'], ['bb', '3'], ['sam', '1', 'dad', '21', '-10']]
t = [[int(v) if v.lstrip('-').isnumeric() else v for v in x] for x in l]
print(t)
>>> [['aa', 2], ['bb', 3], ['sam', 1, 'dad', 21, -10]]

Categories