How to convert comma-separated String to List?
Convert comma separated String to List
List<String> items = Arrays.asList(str.split("\\s*,\\s*"));
The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace
which will place the words into the list and collapse any whitespace between the words and commas.
Please note that this returns simply a wrapper on an array: you CANNOT for example .remove()
from the resulting List
. For an actual ArrayList
you must further use new ArrayList<String>
.
How to convert comma-delimited string to list in Python?
You can use the str.split method.
>>> my_string = 'A,B,C,D,E'
>>> my_list = my_string.split(",")
>>> print my_list
['A', 'B', 'C', 'D', 'E']
If you want to convert it to a tuple, just
>>> print tuple(my_list)
('A', 'B', 'C', 'D', 'E')
If you are looking to append to a list, try this:
>>> my_list.append('F')
>>> print my_list
['A', 'B', 'C', 'D', 'E', 'F']
How to convert a comma separated String to ArrayList in Java
The ArrayList
returned by Arrays.asList
is not java.util.ArrayList
. It's java.util.Arrays.ArrayList
. So you can't cast it to java.util.ArrayList
.
You need to pass the list to the constructor of java.util.ArrayList
class:
List<String> items = new ArrayList<String>(Arrays.asList(CommaSeparated.split("\\s*,\\s*")));
or, you can simply assign the result:
List<String> items = Arrays.asList(CommaSeparated.split("\\s*,\\s*"));
but mind you, Arrays.asList
returns a fixed size list. You cannot add or remove anything into it. If you want to add or remove something, you should use the 1st version.
P.S: You should use List
as reference type instead of ArrayList
.
How can I convert a comma delimited string to a list of integers?
Something like this
var results = yearsString.Split(',').Select(x => int.Parse(x.Trim()));
Note : There is no fault tolerance in this approach, if they cant be converted to int
this will blow up with an exception
And just for fun here is a an approach using pointers unsafe
and fixed
fixed (char* pInput = Input)
{
var len = pInput + Input.Length;
var current = 0;
var results = new List<int>();
for (var p = pInput; p < len; p++)
{
if (*p >= 48 & *p <= 58)
current = current * 10 + *p - 48;
else if (*p == ',')
{
results.Add(current);
current = 0;
}
}
results.Add(current);
return results;
}
Benchmarks
Just because i was bored
----------------------------------------------------------------------------
Mode : Release (64Bit)
Test Framework : .NET Framework 4.7.1 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------
Operating System : Microsoft Windows 10 Pro
Version : 10.0.17134
----------------------------------------------------------------------------
CPU Name : Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
Description : Intel64 Family 6 Model 42 Stepping 7
Cores (Threads) : 4 (8) : Architecture : x64
Clock Speed : 3401 MHz : Bus Speed : 100 MHz
L2Cache : 1 MB : L3Cache : 8 MB
----------------------------------------------------------------------------
Test 1
--- Standard input --------------------------------------------------------
| Value | Average | Fastest | Cycles | Garbage | Test | Gain |
--- Scale 100 ---------------------------------------------- Time 0.003 ---
| Unsafe | 2.304 µs | 2.101 µs | 9.298 K | 0.000 B | Pass | 90.28 % |
| Linq | 23.711 µs | 22.214 µs | 82.700 K | 0.000 B | Base | 0.00 % |
--- Scale 1,000 -------------------------------------------- Time 0.046 ---
| Unsafe | 21.697 µs | 21.013 µs | 75.490 K | 0.000 B | Pass | 90.41 % |
| Linq | 226.218 µs | 205.332 µs | 768.447 K | 0.000 B | Base | 0.00 % |
--- Scale 10,000 ------------------------------------------- Time 0.250 ---
| Unsafe | 214.526 µs | 200.829 µs | 733.557 K | 0.000 B | Pass | 89.93 % |
| Linq | 2.130 ms | 1.996 ms | 7.257 M | 0.000 B | Base | 0.00 % |
--- Scale 100,000 ------------------------------------------ Time 2.906 ---
| Unsafe | 2.303 ms | 2.063 ms | 7.680 M | 0.000 B | Pass | 90.99 % |
| Linq | 25.571 ms | 22.624 ms | 84.808 M | 0.000 B | Base | 0.00 % |
--- Scale 1,000,000 --------------------------------------- Time 36.594 ---
| Unsafe | 23.061 ms | 21.910 ms | 78.356 M | 0.000 B | Pass | 93.07 % |
| Linq | 332.639 ms | 274.595 ms | 1.055 B | 0.000 B | Base | 0.00 % |
---------------------------------------------------------------------------
Convert comma delimited string to list
I'm pretty sure you just need to split the matched string by ,
using String.Split()
.
$Credentials = 'Credentials: myacct1,myacct2'
$runtimes = $Credentials -match "Credentials: (?<content>.*)"
$runtimeList = $matches['content'].Split(",")
Foreach ($runtime in $runtimeList)
{
Write-Host $runtime
}
# Output:
# myacct1
# myacct2
How can I convert comma separated string into a List int
Here is one way of doing it:
List<int> TagIds = tags.Split(',').Select(int.Parse).ToList();
Convert comma separated string into list
Like this:
$string = "word1,word2,word3,word4";
$string = explode(",",$string);
foreach ($string as $str) {
echo "<li>".$str."</li>";
}
You can explode()
the string into an array, loop through it, and output the results into a list option.
Dataframe: Cell Level: Convert Comma Separated String to List
- Use
pandas.Series.str.split
to split the string into alist
.
# use str split on the column
df.mgrs_grids = df.mgrs_grids.str.split(',')
# display(df)
driver_code journey_code mgrs_grids
0 7211863 7211863-140 [18TWL927129, 18TWL888113, 18TWL888113, 18TWL887113, 18TWL888113, 18TWL887113, 18TWL887113, 18TWL887113, 18TWL903128]
1 7211863 7211863-105 [18TWL927129, 18TWL939112, 18TWL939112, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL960111, 18TWL960112]
2 7211863 7211863-50 [18TWL927129, 18TWL889085, 18TWL889085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL890085]
3 7211863 7211863-109 [18TWL927129, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952105, 18TWL951103]
print(type(df.loc[0, 'mgrs_grids']))
[out]:
list
separate row per value
- After creating a column of lists.
- Use
pandas.DataFrame.explode
to create separate rows for each value in the list.
# get a separate row for each value
df = df.explode('mgrs_grids').reset_index(drop=True)
# display(df.hea())
driver_code journey_code mgrs_grids
0 7211863 7211863-140 18TWL927129
1 7211863 7211863-140 18TWL888113
2 7211863 7211863-140 18TWL888113
3 7211863 7211863-140 18TWL887113
4 7211863 7211863-140 18TWL888113
Update
- Here is another option, which combines the
'journey_code'
to the front of'mgrs_grids'
, and then splits the string into a list.- This list is assigned back to
'mgrs_grids'
, but can also be assigned to a new column.
- This list is assigned back to
# add the journey code to mgrs_grids and then split
df.mgrs_grids = (df.journey_code + ',' + df.mgrs_grids).str.split(',')
# display(df.head())
driver_code journey_code mgrs_grids
0 7211863 7211863-140 [7211863-140, 18TWL927129, 18TWL888113, 18TWL888113, 18TWL887113, 18TWL888113, 18TWL887113, 18TWL887113, 18TWL887113, 18TWL903128]
1 7211863 7211863-105 [7211863-105, 18TWL927129, 18TWL939112, 18TWL939112, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL960111, 18TWL960112]
2 7211863 7211863-50 [7211863-50, 18TWL927129, 18TWL889085, 18TWL889085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL890085]
3 7211863 7211863-109 [7211863-109, 18TWL927129, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952105, 18TWL951103]
# output to nested list
df.mgrs_grids.tolist()
[out]:
[['7211863-140', '18TWL927129', '18TWL888113', '18TWL888113', '18TWL887113', '18TWL888113', '18TWL887113', '18TWL887113', '18TWL887113', '18TWL903128'],
['7211863-105', '18TWL927129', '18TWL939112', '18TWL939112', '18TWL939113', '18TWL939113', '18TWL939113', '18TWL939113', '18TWL939113', '18TWL939113', '18TWL960111', '18TWL960112'],
['7211863-50', '18TWL927129', '18TWL889085', '18TWL889085', '18TWL888085', '18TWL888085', '18TWL888085', '18TWL888085', '18TWL888085', '18TWL890085'],
['7211863-109', '18TWL927129', '18TWL952106', '18TWL952106', '18TWL952106', '18TWL952106', '18TWL952106', '18TWL952106', '18TWL952106', '18TWL952105', '18TWL951103']]
Related Topics
Eclipse Java Debugging: Source Not Found
Differences in Boolean Operators: & VS && and | VS ||
Moving Decimal Places Over in a Double
Webdriver - Wait for Element Using Java
File Path to Resource in Our War/Web-Inf Folder
What Is the Correct Way to Structure This Kind of Data in Firestore
How to Have an Autocomplete Using Jtextfield and a Jlist
Should a Retrieval Method Return 'Null' or Throw an Exception When It Can't Produce the Return Value
How to Catch Multiple Java Exceptions in the Same Catch Clause
How to Determine an Object's Class
What Does the Arrow Operator, '->', Do in Java
Output in a Table Format in Java's System.Out