convert string to list [duplicate] - python

This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 6 years ago.
I have a string like this:
st = "The most basic data structure in Python is the sequence * Each element of a sequence is assigned a number * its position or index * The first index is zero * the second index is one * and so forth *"
and I want to split into the list like this:
ls =["The most basic data structure in Python is the sequence","Each element of a sequence is assigned a number","its position or index",.....]
I'm just started python, please help me

You can split a string by a character:
yourString = "Hello * my name * is * Alex"
yourStringSplitted = yourString.split('*')

you can use the function str.split to split a string into a array on a specific character :
>>> str.split(st,"*")
['The most basic data structure in Python is the sequence ',
' Each element of a sequence is assigned a number ',
' its position or index ',
' The first index is zero ',
' the second index is one ',
' and so forth ',
'']

Related

Strip a single quotation ( ' ) from a list from CSV [duplicate]

This question already has answers here:
How do I escape backslash and single quote or double quote in Python? [duplicate]
(5 answers)
Closed 2 years ago.
I'm trying to print out a list and strip the " ' " (single quotes) character from a list I generated from a CSV File.
Here's the code I have
import numpy as np
import pandas as pd
export = pd.read_csv('File') #open file
skuList = export.values.T[0].tolist() #transpose DF + convert to list
#print(skuList)
skuList = [value.strip(" ' ") for value in skuList] #strip ' ' '
print(skuList)
The output from this program is
'GL2i-RS-36', '523-30', 'RK623-30', ....
The output that I would want would be:
GL2i-RS-36, 523-30, RK623-30, ....
Is there a way to print out a list without the single quotes?
Maybe try this (without the spaces around the '):
skuList = [value.strip("'") for value in skuList] #strip ' ' '

Remove a specified character while i using print in python [duplicate]

This question already has answers here:
Print a list of space-separated elements
(4 answers)
Closed 2 years ago.
Hi: I am new to python and programing.
I have a silly question that I couldn't solve.
a=[1,2,3,4,5]
for i in a:
print(i,end=' ')
will get a out put:
1 2 3 4 5
There are space between each numbers in the system out print: ( s means space)
1s2s3s4s5s
How can I remove the last space? The correct out put will be :
1s2s3s4s5
a=[1,2,3,4,5]
print(' '.join([str(x) for x in a])
This will first convert each element of 'a' to string and then join all element using join(). It must be noted that ' ' can be replaced with any other symbol as well
There are various ways, but a simple way is join:
print(' '.join(a), end='')
You can also directly use print:
print(*a, sep=' ', end='')

How do I move the second word BEFORE the first? [duplicate]

This question already has answers here:
Reverse the ordering of words in a string
(48 answers)
Closed 4 years ago.
If my variable is:
string = "first second"
How can I change that to print:
second first
annoyingly simple but I can't find a solution for it!
I'd split the string on ' ' and then join it back:
s = 'first second'
tmp = s.split(' ')
result = ' '.join((tmp[1], tmp[0]))

Using regular expression to count the number of spaces at the beginning of a string [duplicate]

This question already has answers here:
Check string indentation?
(4 answers)
Closed 4 years ago.
How can I use regex to count the number of spaces beginning of the string. For example:
string = ' area border router'
count_space variable would return me a value of 1 since there is 1 whitespace at the beginning of the string. If my string is:
string = ' router ospf 1'
count_space variable would return me a value of 2 since there is 2 whitespace at the beginning of the string. And so on....
I thing the expression would be something like RE = '^\s' ? But not sure how to formulate it.
You don't need regex, you can just do this:
s = ' area border router'
print(len(s)-len(s.lstrip()))
Output:
1

Python - How to ignore white spaces in double quotes while splitting a string? [duplicate]

This question already has answers here:
Split a string by spaces -- preserving quoted substrings -- in Python
(16 answers)
Closed 9 years ago.
I have my data as below
string = ' streptococcus 7120 "File being analysed" rd873 '
I tried to split the line using n=string.split() which gives the below result:
[streptococcus,7120,File,being,analysed,rd873]
I would like to split the string ignoring white spaces in " "
# output expected :
[streptococcus,7120,File being analysed,rd873]
Use re.findall with a suitable regex. I'm not sure what your error cases look like (what if there are an odd number of quotes?), but:
filter(None, it.chain(*re.findall(r'"([^"]*?)"|(\S+)', ' streptococcus 7120 "File being analysed" rd873 "hello!" hi')))
> ['streptococcus',
'7120',
'File being analysed',
'rd873',
'hello!',
'hi']
looks right.
You want shlex.split, which gives you the behavior you want with the quotes.
import shlex
string = ' streptococcus 7120 "File being analysed" rd873 '
items = shlex.split(string)
This won't strip extra spaces embedded in the strings, but you can do that with a list comprehension:
items = [" ".join(x.split()) for x in shlex.split(string)]
Look, ma, no regex!

Categories