Given:
xs = ['1', '2', '3']
Use map then list to obtain a list of integers:
list(map(int, xs))
In Python 2, list was unnecessary since map returned a list:
map(int, xs)
Answer from cheeken on Stack OverflowGiven:
xs = ['1', '2', '3']
Use map then list to obtain a list of integers:
list(map(int, xs))
In Python 2, list was unnecessary since map returned a list:
map(int, xs)
Use a list comprehension on the list xs:
[int(x) for x in xs]
e.g.
>>> xs = ["1", "2", "3"]
>>> [int(x) for x in xs]
[1, 2, 3]
listofIDs.Select(int.Parse).ToList()
Here is a safe variant that filters out invalid ints:
List<int> ints = strings
.Select(s => Int32.TryParse(s, out int n) ? n : (int?)null)
.Where(n => n.HasValue)
.Select(n => n.Value)
.ToList();
It uses an out variable introduced with C#7.0.
This other variant returns a list of nullable ints where null entries are inserted for invalid ints (i.e. it preserves the original list count):
List<int?> nullableInts = strings
.Select(s => Int32.TryParse(s, out int n) ? n : (int?)null)
.ToList();
Convert List<string> to List<int> in C# - Stack Overflow
How to convert string list to int list?
String List to Int List!? - Scripting - Unity Discussions
Convert list of string elements to list of integers
There's a two-step process involved here. The first is to convert the strings to an integer, then convert the array to a list. If you can use LINQ, the easiest way is to use:
Use with AIstringArray.Select(x => Int32.Parse(x)).ToList();
Getting a hint from your code:
Use with AIvar listOfInts = s.Split(',').Select(Int32.Parse).ToList();
Instead of using LINQ you can use List<T>.ConvertAll<TOutput>(...)
List<int> intList = stringList.ConvertAll(int.Parse);
I would suggest using TryParse(), in case some of the values are not convertible into int.
For this I have created an Extension Method. Below is my demo LinqPad code.
void Main()
{
List<string> sourceList = new List<string> {"1", "2","3", "qwert","4", "5","6", "7","asdf", "9","100", "22"};
//Dump is a LinqPad only method. Please ignore
sourceList.ConvertToInt().Dump();
}
static public class HelperMethods
{
static public List<int> ConvertToInt(this List<string> stringList)
{
int x = 0;
var intList = stringList.Where(str => int.TryParse(str, out x))
.Select (str => x)
.ToList();
return intList;
}
}
In this case, only the numeric int values get parsed and the rest is gracefully ignored. You could built in some error handling / notification if you want to.
/Edit Based on Peter Kiss' suggestion here is a more generic approach based on the IEnumerable interface.
static public IEnumerable<int> ConvertToInt(this IEnumerable<string> source)
{
int x = 0;
var result = source.Where(str => int.TryParse(str, out x))
.Select (str => x);
return result;
}
With this you'd just have to call AsEnumerable() before calling ConvertToInt() The result is of course of type IEnumerable<Int32> and from here on, you can convert it easily into a List by using .ToList() or an array or whatever you need at that point.
numbers = input("Please type some numbers: ").split()for n in range(0, len(numbers)):numbers[n] = int(numbers[n])
Just tried converting a string list to an int list using a for loop and made this code but not sure what it means step by step, if someone could please explain the last 2 lines it would be much appreciated. Thanks!
Hi all.
I've recently started to dive into Python, and i find it refreshing, frustrating, fascinating and sometimes hopeless.
I've got an assignment that's due in 3 hours, and i can't for the life of me figure it out. Could anyone help?
I've got a long list of numbers i've imported from a csv file, but they dont appear to be integers. They come out as f.ex: '334', '234', '543' etc. Can anyone tell me how to convert the entire list to a list of integers?
You can use .ToList<T>() to convert .Select() result (which is IEnumerable<int[]>) to a list.
This way, you apply int.Parse to every item of item array, and convert the result to List.
Also, in order to provide code homogeneity, you can use LINQ Select and ToArray instead of Array.ConvertAll:
List<string[]> stringArrayList = ...;
List<int[]> intArrayList = stringArrayList
.Select(stringArray => stringArray.Select(int.Parse).ToArray())
.ToList();
One little correction: you do not cast string to an int, you convert / parse it.
You can try something like this:
List<int[]> Listintegers = yourList
.Select(y => y.Select(x => int.Parse(x)).ToArray())
.ToList();
Hi everyone I'm a beginner with C# and I was making a simple Console Application. What I want to do is that the user can insert a string (I use Console.ReadLine) and the program convert it into a list (which will be used later) For example if I write "1 3 -2 -5 1" I want that the program add to an empty list the values 1, 3, -2,...
Is it possible? And if it is, how should I do it? Thanks in advance and sorry if I made some mistakes (I'm not English)