I'm trying to isolate a substring of each string element of an array such that it is the string until the last period. For example I would want to have:
input = 'A.01.0'
output = 'A.01'
or
input = 'A.0'
output = 'A'
And I want to do this for all elements of an array.
Use some rsplit magic:
x=["123","456.678","abc.def.ghi"]
[y.rsplit(".",1)[0] for y in x]
This is one way to produce the wanted output format. You need to alter this to suit your needs.
output = input[:input.rindex('.')]
For the entire array:
arr = ['A.01.0', 'A.0']
arr = [x[:x.rindex('.')] for x in arr]
Hope that helps :-)
Something like this?
>>> i = ['A.01.0', 'A.0']
>>> [x[:x.rfind('.')] for x in i]
['A.01', 'A']
Related
Is that possible select two elements from one array. In my case I want to select like this a[1,2] and show only '12' in one string format.
Instant of a[0]+a[1], any other possible way to do it quick ?
python code
a = ['1','2','3','4','5']
a[0]
execution result
'1'
expected code like
a = ['1','2','3','4','5']
a[0,1]
expected result (one string)
'12'
You could custom a new class to achieve that:
class List_:
def __init__(self, data):
self.data = data
def __getitem__(self, pos):
return "".join(self.data[i] for i in pos)
a = List_(['1','2','3','4','5'])
a[0, 1]
And the result:
'12'
Python can concatenate the indices into one string so to get your desired output you can do:
a = ['1','2','3','4','5']
print(a[0]+a[1])
which returns:
'12'
Given an arbitrary list of indexes you can select those with a comprehension and join():
a = ['1','2','3','4','5']
indexes = [1, 3, 4]
''.join(a[n] for n in indexes)
# 245
Alternatively you can use the builtin itemgetter. This is handy if you want to get the same items from multiple lists:
from operator import itemgetter
a = ['1','2','3','4','5']
b = ['5','6','7','8','9']
gettr = itemgetter(1,3, 4)
"".join(gettr(a))
# 245
"".join(gettr(b))
# 689
You may try slicing the array to isolate the section you want, then just string join to form a single string. For example, for the first 3 elements you may try:
a = ['1','2','3','4','5']
print(''.join(a[0:3])) # prints '123'
As in the comments says, you can use print(a[0]+a[1]) or print("".join(a[0:2])) to get your desired output.
I have an array I want to iterate through. The array consists of strings consisting of numbers and signs.
like this: €110.5M
I want to loop over it and remove all Euro sign and also the M and return that array with the strings as ints.
How would I do this knowing that the array is a column in a table?
You could just strip the characters,
>>> x = '€110.5M'
>>> x.strip('€M')
'110.5'
def sanitize_string(ss):
ss = ss.replace('$', '').replace('€', '').lower()
if 'm' in ss:
res = float(ss.replace('m', '')) * 1000000
elif 'k' in ss:
res = float(ss.replace('k', '')) * 1000
return int(res)
This can be applied to a list as follows:
>>> ls = [sanitize_string(x) for x in ["€3.5M", "€15.7M" , "€167M"]]
>>> ls
[3500000, 15700000, 167000000]
If you want to apply it to the column of a table instead:
dataFrame = dataFrame.price.apply(sanitize_string) # Assuming you're using DataFrames and the column is called 'price'
You can use a string comprehension:
numbers = [float(p.replace('€','').replace('M','')) for p in a]
which gives:
[110.5, 210.5, 310.5]
You can use a list comprehension to construct one list from another:
foo = ["€13.5M", "€15M" , "€167M"]
foo_cleaned = [value.translate(None, "€M")]
str.translate replaces all occurrences of characters in the latter string with the first argument None.
Try this
arr = ["€110.5M","€110.5M","€110.5M","€110.5M","€110.5M","€110.5M","€110.5M"]
f = [x.replace("€","").replace("M","") for x in arr]
You can call .replace() on a string as often as you like. An initial solution could be something like this:
my_array = ['€110.5M', '€111.5M', '€112.5M']
my_cleaned_array = []
for elem in my_array:
my_cleaned_array.append(elem.replace('€', '').replace('M', ''))
At this point, you still have strings in your array. If you want to return them as ints, you can write int(elem.replace('€', '').replace('M', '')) instead. But be aware that you will then lose everything after the floating point, i.e. you will end up with [110, 111, 112].
You can use Regex to do that.
import re
str = "€110.5M"
x = re.findall("\-?\d+\.\d+", str )
print(x)
I didn't quite understand the second part of the question.
Here is my code:
test_list= [
["Romeo and Juliet","Shakespeare"],
["Othello","Play"],
["Macbeth","Tragedy"]
]
value = "Tragedy"
print(test_list.index(value))
As a result I get “ValueError: ‘Tragedy’ is not in list
I’d come to the conclusion that .index only works for 1D arrays? But then how do I do it die 2D arrays? This code works fine if I make the array 1D. Please help, but in simple terms as I am a beginner.
Apologies for formatting issues on mobile. The array is set out correctly for me.
Loop through your list and search each sublist for the string.
Testlist = [
["Romeo and Juliet","Shakespeare"],
["Othello","Play"],
["Macbeth","Tragedy"]
]
Value = "Tragedy"
for index, lst in enumerate(Testlist):
if Value in lst:
print( index, lst.index(Value) )
You can also use the map operator:
# Get a boolean array - true if sublist contained the lookup value
value_in_sublist = map(lambda x: value in x, test_list)
# Get the index of the first True
print(value_in_sublist.index(True))
You can also use numpy:
import numpy as np
test_list = np.array(test_list)
value = 'Tragedy'
print(np.where(test_list == value))
Output:
(array([2]), array([1]))
If you have multiple occurences of an element, then np.where will give you a list of indices for all the occurences.
numpy arrays may help in your specific case
import numpy
test_array = numpy.array(Testlist)
value = "Tragedy"
numpy.where(test_array==value)
# you will get (array([2]), array([1]))
Let say my string is as:
x = 'abcdefghi'
I want to reverse it in subsets of 3, so that my output is:
x = 'cbafedihg'
i.e. 0th index is swapped with 2nd index, 3rd index swapped with 5th, and so on.
Below is my code based on converting the string to list and swap the elements within the list:
string_list = list(x)
for i in range(len(string_list)/3):
string_list[i*3], string_list[i*3+2] = string_list[i*3+2], string_list[i*3]
''.join(string_list)
# Output: 'cbafedihg'
I want to know what will be the most efficient and most pythonic way to achieve it.
Note: len(x)%3 will always be 0.
The above code can be written using string slicing and list comprehension as:
# Here x[i*3:i*3+3][::-1] will reverse the substring of 3 chars
>>> ''.join([x[i*3:i*3+3][::-1] for i in range(len(x)/3)])
'cbafedihg'
Based on the comment by Delgan, it could be further simplified using step as 3 with range itself as:
>>> ''.join(x[i:i+3][::-1] for i in range(0, len(x), 3))
'cbafedihg'
Writing a function that is more readable and flexible?
def get_string(input_str, step=3):
output = ""
i = 0
for _ in list(input_str):
if i == len(input_str):
return output
elif i+step-1 >= len(input_str):
output += input[len(input_str)-1:i-1:-1]
return output
else:
output += input_str[i+step-1:i:-1] + input_str[i]
i += step
return output
And here comes the flexible part:
get_string("abcdefghi")
# Ouputs 'cbafedihg'
get_string("abcdefghi", 2)
# Outputs 'badcfehgi'
get_string("abcdefghi", 5)
# Outputs 'edcbaihgf'
Not to mention, if you want to add some more logic or change the logic, it is easier to change here.
Another alternative to achieve this is to type-cast your string to list, then simply swap the elements of list using list slicing with step as 3, and join back the list of strings as:
>>> string_list = list(x)
>>> string_list[::3], string_list[2::3] = string_list[2::3], string_list[::3]
>>> ''.join(string_list)
'cbafedihg'
I have two lists.
Example:
a=[10,20,30,40,50,60,70,80]
b=[2,4,6,8,10,12,14,16,18,20,22,24]
Say I want to replace every second element in list a with every third element from list b.
For my purposes Im using this formula to do this for the first 2 respective elements:
a[1]="{}\n".format(b[2])
But how do I do this for all elements in the lists?
Thanks in advance!
Use index slicing:
a = [10,20,30,40,50,60,70,80]
b = [2,4,6,8,10,12,14,16,18,20,22,24]
a[1::2] = b[2::3]
or if this formatting is also important:
a[1::2] = map('{}\n'.format, b[2::3])
Try this:
for x,y in zip(range(1, len(a), 2), range(2, len(b), 3)):
a[x] = '{}\n'.format(b[y])
Or, the short way, although this is a straight replacement and doesn't do your formatting:
a[1::2] = b[2::3]