Tags down


Python - how to split a string list into two?

By : Jader Diaz
Date : October 18 2020, 01:08 AM
I wish this helpful for you There is a list like: , You can try this:
code :
list = ['AB', 'CD', 'EF', 'GH']

first, second = zip(*list)
('A', 'C', 'E', 'G')
('B', 'D', 'F', 'H')

Share : facebook icon twitter icon

When splitting an empty string in Python, why does split() return an empty list while split('\n') returns ['']?

By : Thamza
Date : March 29 2020, 07:55 AM
I wish this helpful for you
Question: I am using split('\n') to get lines in one string, and found that ''.split() returns empty list [], while ''.split('\n') returns [''].
code :
>>> data = '''\
Shasta      California     14,200
McKinley    Alaska         20,300
Fuji        Japan          12,400
>>> for line in data.splitlines():
        print line.split()

['Shasta', 'California', '14,200']
['McKinley', 'Alaska', '20,300']
['Fuji', 'Japan', '12,400']
>>> data = '''\
>>> for line in data.splitlines():
        print line.split(',')

['Guido', 'BDFL', '', 'Amsterdam']
['Barry', 'FLUFL', '', 'USA']
['Tim', '', '', 'USA']
>>> ''.split(',')       # No cuts
>>> ','.split(',')      # One cut
['', '']
>>> ',,'.split(',')     # Two cuts
['', '', '']
>>> data = '''\
Line 1
Line 2
Line 3
Line 4'''

>>> data.count('\n')                               # Inaccurate
>>> len(data.splitlines())                         # Accurate, but slow
>>> data.count('\n') + (not data.endswith('\n'))   # Accurate and fast
ps_aux_header  = "USER               PID  %CPU %MEM      VSZ"
patient_header = "name,age,height,weight"

Convert string to list. Python [string.split() acting weird]

By : Nahuel Foss
Date : March 29 2020, 07:55 AM
this will help Use ast.literal_eval():
code :
>>> from ast import literal_eval
>>> temp = "['a','b','c']"
>>> l = literal_eval(temp)
>>> l
['a', 'b', 'c']
>>> type(l)
<type 'list'>

Trying to split a long String into List ( which each var in the list represent a word) using Python

By : Serkan Akbaba
Date : March 29 2020, 07:55 AM
Does that help You're very close. You can just call text.split without any arguments and it should work.
code :
text = "Hello world            Hello    Stack"
the_list = text.split()

python split string list into lists of strings using text in the list

By : user3098460
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Join the list as a string with custom delimiter, say | for example, use re.split to split on every occurrence of S-1 and then split each element of the resulting list back to a list based on delimiter |
code :
>>> res = [s.strip('|').split('|') for s in re.split(r'(?=S-1)', '|'.join(exemptions)) if s]
>>> pprint(res)
[['S-1', '20090820', '\t\t\t\tDOLLAR GENERAL CORP', '\t\t0000029534'],
  '\t\t\t\tDT INC',
  '\t\t\t\tBFF INC',
  '\t\t\t\tCONSTAR INC',
 ['S-1', '20020523', '\t\t\t\tCONSTAR INTERNATIONAL INC', '\t\t0000029806'],
 ['S-1', '20051123', '\t\t\t\tEXCO RESOURCES INC', '\t\t0000316300'],
 ['S-1', '20061221', '\t\t\t\tEXCO RESOURCES INC', '\t\t0000316300'],
 ['S-1/A', '20140327', '\t\t\t\tAlly Financial Inc.', '\t\t0000040729'],
 ['S-1', '20110331', '\t\t\t\tAlly Financial Inc.', '\t\t0000040729'],
 ['S-1', '20040319', '\t\t\t\tDIGIRAD CORP', '\t\t0000707388'],
 ['S-1', '20040408', '\t\t\t\tBUCYRUS INTERNATIONAL INC', '\t\t0000740761'],
 ['S-1', '20041027', '\t\t\t\tBUCYRUS INTERNATIONAL INC', '\t\t0000740761'],
 ['S-1', '20050630', '\t\t\t\tSEALY CORP', '\t\t0000748015'],

How to split a string in a list into multiple string based on whitespaces in python?

By : pandhandj
Date : March 29 2020, 07:55 AM
will be helpful for those in need Here, 'list' is my list of strings, i want to split 'a b' into 'a','b' and merge it back into the list with other strings , Try this code
code :
lis = ['abc', 'a b', 'a b c', '1234']
lis1 = []
for i in lis:
    b = i.split()
print lis1
['abc', 'a', 'b', 'a', 'b', 'c', '1234']
Related Posts Related Posts :
  • Add missing rows to data frame equally distribueted
  • Converting text data into Json format
  • TensorFlow: Adding a small noise to pre-trained weights
  • Sudoku solver Python algorithm clarification needed
  • Cant call on list object when generating from a list
  • Accesing elasticsearch on Heroku Bonsai from my computer
  • Oddity calculating runtime with timeit in Python?
  • Create binary array of matching rows in an array using numpy?
  • Pyspark sc.textFile() doesn't load file completely
  • Get/display the author of a post from another app for notifications system
  • Prevent setup.py test / pytest from installing extra dependencies
  • Parse an xml file with python
  • I have a csv which breaks due to extra commas, I require only one column from the dataset but it occurs after the column
  • Pandas query using filter and sort, leading to unresolved errors
  • Execute Highlighted Code in Jupyter notebook Cell?
  • How do I multiply a column in predefined increments?
  • Printing an error message if a user does not input a certain word
  • changing format of a df and removing undesired char
  • Organize data in CSV into multiple lists in Python 3
  • Converting a row of a pandas dataframe into a dataframe itself (instead of a series)?
  • Merge two pandas dataframes with timeseries index
  • How do I achieve this in Folium?
  • Is it possible to change some product's IP address using python scripts? If possible then how ? Includes Printer and oth
  • How to remove margins from Matplotlib bar chart?
  • Procfile Heroku
  • Alternating between upper and lower cases
  • How to remove extra row after set_index() without losing index name?
  • Why is kdeplot scaled off the y-axis when including in Seaborn PairGrid?
  • Converting CSV data to string
  • Failling to build a Django project in VS 2015 - django\contrib\admin\widgets.py
  • How to put lists into one larger list in a certain order for certain conditions? - Python 3.x
  • Sublime uses Python 3, SublimeREPL uses Python 2.7. Huh?
  • Difference between dates in Pandas dataframe
  • python 3: import module
  • Add 2d array to make 3d in python
  • Pandas Merge columns of a pivot table
  • Split dataframe into testing_df and validation_df
  • Python - Splitting words in txt
  • replacing float 0 with NaN
  • What to Use Instead of QWebEngineView for PyQt 5.6 with Anaconda 3
  • Replacement of element in a list of python
  • How to supress 'star imports' warnings from Spyder IDE?
  • What I am doing wrong with S3 PUT request using AWS Signature Version 4
  • How to print the level of every node in binary-tree?
  • What would be equivalent to this list comprehension?
  • Pandas get count of group above group median
  • How to view initialized weights (i.e. before training)?
  • Appending a tuple to a np array returns a list of scalars instead of tuples
  • How to only allow digits, letters, and certain characters in a string in Python?
  • difference between 2 scipy sparse csr matrices
  • How can I use lists in an equation?
  • Save tf.summary.image with Estimator API
  • Plotting multiple stacked bar graph given a pandas dataframe in Python
  • How to print only a certain part of a list?
  • How to resolve ValueError: Input contains NaN, infinity or a value too large for dtype('float64')
  • Missing data in Pandas Merge
  • Python - Get html table element with lxml.html regex
  • Function that would create a copy of lists
  • python 2.7: Debugging an if-else statement (syntax error)
  • 404 fail with pytest when 'manage.py shell' and browser don't
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com