Split list python list of number into multiple lists - python-3.x

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)

Related

Python3: comparing dynamic list to create a regexp

I am currently writing a class to create a regexp.
As an input, we got 3 sentences in a list ("textContent") and the output regexp should match the 3 sentences.
For this, I use ZIP. The code below is 100% working.
from array import *
textContent = []
textContent.append("The sun is.shining")
textContent.append("the Sun is ShininG")
textContent.append("the_sun_is_shining")
s = ""
for x, y, z in zip(textContent[0], textContent[1], textContent[2]):
if x == y == z:
s+=str(x)
else:
s+="."
#answer is ".he..un.is..hinin."
print(s)
It's working but ONLY with 3 sentences in a List.
Now, I want the same comparison but with a dynamic list that could contain 2 or 256 sentences for example. And I'm stuck. I don't know how to adjust the code for that.
I noticed that the following throws no error:
zip(*textContent)
So, I'm stuck with the variables that I compare before: x, y, z
for x, y, z in zip(*textContent):
It could work only if textContent contains 3 values...
Any idea? May be another class than ZIP could make the job.
Thanks
This will solve your problem with zipping and comparing:
l = ['asd', 'agd', 'add', 'abd']
for letters in list(zip(*l)):
if all([letters[0] == letter for letter in letters]):
print('Yey')
else:
print('Ugh')
>>> Yey
>>> Ugh
>>> Yey
And for l = ['asd', 'agg', 'add', 'cbb'] it will print 3 'Ugh'.
Also you should check if l is longer than 0

Is it possible to transform a string into a list, like this: "man1\nman2\nwoman\nman4" into ["man1", "man2", "man4"] in PYTHON

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"]

Numpy array into list

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

Nested comprehension - inner loop inside an if loop

How do I exclude multiple parameters while inside a list comprehension?
I have a simple expression:
data = [x for x in blob if e not in x.thing]
However instead of one string 'e' I would like to test multiple strings
So a bit of pseudocode:
exclude = ['tom', 'dick', 'harry', ....]
data = [x for x in blob if <any of the values of exclude> not in x.thing]
I don't know the length or values of exclude so I can't do
[x for x in blob if e1 not in x.thing else x for x in blob if e2 not in x.thing else ... ]
Try this, just copy paste this line:
[x for x in blob if all(e not in x.thing for e in exclude)]
for example :
Input :
exclude = ['tom', 'dick', 'harry']
blob=['franceharry','germany','gerrytom']
[x for x in blob if all(e not in x for e in exclude)]
Output :
['germany']

String Manipulating and slicing from a list

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

Resources