I have a numpy array that looks like below:
x = ['11BIT' '4FUNMEDIA' 'ABCDATA' 'ABPL' 'ACAUTOGAZ' 'ADIUVO']
The output should be like this:
x = ['11BIT', '4FUNMEDIA', 'ABCDATA', 'ABPL', 'ACAUTOGAZ', 'ADIUVO']
I tried to use x.tolist() but it didn't help. Basically I need a coma between values. Anyone could help ?
Thanks All
"A comma between values" as in a string?
",".join(x)
if you want a true list:
x = list(x)
or
x = [i for i in x]
should do the trick
Related
Would like my output which should not be string but my code returning string to me. Please look on my below code in which z is my output. I tried with regex, replace, strip, eval, ast.literal_eval but nothing worked for me as of now.
x = "'yyyymm'='202005','run_id'='51',drop_columns=run_id"
y = x.split(',')
print(y)
This will print:
["'yyyymm'='202005'","'run_id'='51'","drop_columns=run_id"]`
But I want:
['yyyymm'='202005','run_id'='51',drop_columns=run_id]
x is a string and if you split a string, you will get an array of strings. It is basically cutting it into pieces.
Your question is not really clear on what you want to achieve. If you want to have key-value-pairs, you'd need to split each token at the =. This would give you something like this:
[('yyyymm', '202005'), ('run_id', '51'), ('drop_columns', 'run_id')]
But the items in the tuples would still be strings. If you want to have integers, you would need to cast them which is only possible if the strings consist of digits. It would not be possible to cast 'run_id' to integer.
You can refer to this example. I'm not sure if that is 100% what you are looking for, but it should give you the correct idea.
x = "yyyymm=202005,run_id=51,drop_columns=run_id"
y = x.split(',')
tmp = []
for e in y:
tmp.append((e.split('=')[0], e.split('=')[1]))
out = []
for e in tmp:
if str.isnumeric(e[1]):
out.append((e[0], int(e[1])))
else:
out.append(e)
print(out)
This will give you:
[('yyyymm', 202005), ('run_id', 51), ('drop_columns', 'run_id')]
So i have numpy array like this
x = [1,2,3,4,5]
if i want to get this
x = [1,2,4]
how to produce this result?
I understand the i:j:k syntax but it only have one step but what i want to achieve is dynamic step
Is it possible to transform a string into a list, like this:
"man1\nman2\nwoman\nman4"
into
["man1", "man2", "man4"]
in python
Yes, you need to use string.split.
>>> x = "man1\nman2\nman3"
>>> x.split('\n')
['man1', 'man2', 'man3']
I got the answer.
in_str = "man1\nman2\nwoman\nman4"
lst = list(in_str.split("\n"))
out = [x for x in lst if re.search(man, x)]
print out
This prints ["man1", "man2", "man4"]
I have the following python list:
x = ["56843", "84631", "13831"]
And I want to get the following list:
x = [ ["5","6","8","4","3"] , ["8","4","6","3","1"] ]
How can I achieve this?
I have tried with split, but I need a separator that I don't know which is
Just this:
x = [list(y) for y in x]
list(y) which y is a string will convert the string into a list of characters.
x = [[ch for ch in s] for s in x]
x = ["56843","84631","13831"]
c = []
for i in x:
c.append(list(i))
print(c)
import os
a = ['docs-assets', 'ico', 'favicon.png']
for item in range(len(a)):
z = os.path.join("sample",a[item])
print(z)
Results:
sample\docs-assets
sample\ico
sample\favicon.png
Can you tell me how i can join each item in the "a" list using os.path.join() so that the result would be:
sample\docs-assets\ico\favicon.png
Thanks
Like so:
os.path.join('sample', *a)
You can do it as:
s = 'sample\\'+'\\'.join(a)
>>> print s
sample\docs-assets\ico\favicon.png
DEMO