How do I split a string into a list of words?
Given a string sentence
, this stores each word in a list called words
:
words = sentence.split()
How to split strings inside a list by whitespace characters
You can use simple list comprehension, like:
newlist = [word for line in mylist for word in line.split()]
This generates:
>>> [word for line in mylist for word in line.split()]
['this', 'is', 'a', 'string', 'of', 'text', 'this', 'is', 'a', 'different', 'string', 'of', 'text', 'and', 'for', 'good', 'measure', 'here', 'is', 'another', 'one']
How to split a list by string
You're looking for list operations, not for string operations. We just need to find the position where the separator string appears, and take a slice starting from the next element, like this:
lst = ['This is a list', 'of strings', 'blahblahblah', 'split_here', 'something else', 'we like cake', 'aardvarks']
new_list = lst[lst.index('split_here')+1:]
The above assumes that the separator string is present in the list, otherwise we'll get a ValueError
. The result is as expected:
new_list
=> ['something else', 'we like cake', 'aardvarks']
How to split a string into a list in Python?
The str.split()
function will help here.
nums = '128,120,119,118,119,118,120,116,116,120,128,121'
num_list = nums.split(',')
The argument is how what character you want to split by.
PS. If you want to split by multiple characters, say in a string where the elements are separated by a comma and a space, like so nums = '128, 120, 119, 118, 119, 118, 120, 116, 116, 120, 128, 121'
, you can use nums.split(', ')
.
Splitting a string into a List of string array in C#
If you really want a List of string-Arrays, just omit the "ToDictionary" line.
var result = str.Split(',')
.Select(line => line.Split('='))
.ToList();
will give you a List of Arrays, with each Array [0] holding the "Key", and [1] holding the "Value"
ad PS: To get the first "OU" value, you can use a regular expression:
var firstOuRegex = new Regex("[oO][uU]=([^,]+)", RegexOptions.Compiled);
var testString = "CN=Test,OU=ABC,OU=Company,DC=CFLA,DC=domain";
var result = firstOuRegex.Match(testString).Groups[1];
Split string into a list, with items of equal length in python 3
split_string_list = [string[x:x+4] for x in range(0,len(string),4)]
Try that
Basically what it is a list generated such that it starts with elements 0-4, then 4-8, etc. which is exactly what you want, typecasted into a string
Split string into a list on whitespace, excluding single spaces when the next character is not a dash
A less haphazard approach would be to interpret the headers on the first line as column indicators, and split on those widths.
import sys
import re
def col_widths(s):
# Shamelessly adapted from https://stackoverflow.com/a/33090071/874188
cols = re.findall(r'\S+\s+', s)
return [len(col) for col in cols]
widths = col_widths(next(sys.stdin))
for line in sys.stdin:
line = line.rstrip('\n')
fields = []
for col_max in widths[:-1]:
fields.append(line[0:col_max].strip())
line = line[col_max:]
fields.append(line)
print(fields)
Demo: https://ideone.com/ASANjn
This seems to provide a better interpretation of e,g. the LDate
column, where the dates are sometimes padded with more than one space. The penultimate column preserves the final dash as part of the column value; this seems more consistent with the apparent intent of the author of the original table, though perhaps separately split that off from that specific column if that's not to your liking.
If you don't want to read sys.stdin
, just wrap this in with open(filename) as handle:
and replace sys.stdin
with handle
everywhere.
Related Topics
Convert a String Representation of a Dictionary to a Dictionary
How to Read/Process Command Line Arguments
Pip' Is Not Recognized as an Internal or External Command
Use Different Python Version With Virtualenv
How to Flush the Output of the Print Function
How to Force Division to Be Floating Point? Division Keeps Rounding Down to 0
Why Isn't the 'Global' Keyword Needed to Access a Global Variable
Does Python Have a String 'Contains' Substring Method
A Non-Blocking Read on a Subprocess.Pipe in Python
Local Variables in Nested Functions
How to Type Hint a Method With the Type of the Enclosing Class
Error "Microsoft Visual C++ 14.0 Is Required (Unable to Find Vcvarsall.Bat)"
Best Way to Structure a Tkinter Application
How to Rotate an Image Around Its Center Using Pygame
Redirect Stdout to a File in Python