Mathematica, convert string to number - wolfram-mathematica

I imported a table in mathematica using Import["..file","table"] now I cant use the numbers in the table as they are in string formta, can someone please explain how can i convert them from string to number again?

Another approach is to use Read. This is useful if you have numbers in "E" form scientific notation:
Read[StringToStream[#], Number] &/#{ "1" ,"1.5" , "1E-20" , "2.E10" }
{1, 1.5, 1.5*10^-20, 2.*10^10}
Note ToExpression gets these wrong:
ToExpression /# {"1", "1.5", "1.5E-20", "2.E10"}
{1, 1.5, -15.9226, 2. E10}
"1.5E-20" is evaluated as 1.5 * 2.71828-20
in the last case the "E10" is taken as a new symbol..
ToExpression is however faster if you can use it..

The intrinsic function ToExpression will convert its argument to an expression; if the argument is the string representation of a number the function will return the number.
These days (early 2023) there are other ways to convert a number string to a number. For example
Interpreter["Number"]["4.1234"]
ought to return the number 4.1234, and ought to fail when the string can't be parsed as a number. This approach makes it more difficult for arbitrary code to execute.

Related

Float with 'unnecessary' digits

I need to make a number out of the string. to do that i use well known maneuver looking something like this:
Float(string_var) rescue nil
This works nicely, however I do have a tiny, little problem. If a string is "2.50", variable I get is 2.5. Is it even possible to create Float object with 'unnecessary' 0 digit at the end? can I literally translate "2.50" into 2.50 ?
In short, the answer is no, given the question, as any Float, when examined, will use Float's to_s function, eliciting an answer without trailing zeroes.
Float will always give you a numeric value that can be interpreted any way you wish, though. In your example, you will get a float value (given a string that is a parsable float). What you are asking then, is how to display that value with trailing zeroes. To do that, you will be turning the float value back into a string.
Easiest way to accomplish that is to use the format given by one of your respondents, namely
string_var = "2.50"
float_value = Float(string_var) rescue nil # 2.5
with_trailing_zeroes = "%0.2f" % float_value # '2.50'

Max function not returning the max value in an array

I have unsorted arrays as some_array. When I use some_array.max to get the max number, I get the following output. For:
some_array = ["1.10.0", "1.11.0", "1.12.0", "1.13.0", "1.14.0", "1.15.0", "1.16.0",
"1.16.1", "1.17.0", "1.18.0", "1.7.0", "1.8.0"]
I get 1.8.0 instead of 1.18.0. For:
some_array = ["1.11.0", "1.12.0", "1.13.0", "1.14.0", "1.14.1", "1.15.0", "1.16.0", "1.17.0", "1.18.0", "1.19.0", "1.5.0", "1.8.0", "1.9.0"]
I get 1.9.0 instead of 1.19.0.
To me, it looks like max is picking up the last value from the array. Shouldn't max print the maximum value in the array? Do I have to sort the array before using max? Is there any other way to get max value out of array?
The existing answers already point out the problem in your code. This is how you may write it:
some_array.max_by { |version| version.split(".").map(&:to_i) }
#=> "1.18.0"
The problem is that you are thinking that they are integers, but they're not - they're strings. So they get sorted lexicographically. That is to say, treat them like an alphabet. "1.8.0" is greater than "1.18.0", because the "8" is greater than the "1" character (in the 3rd position).
Look at the following random strings. They are in order:
"abcdef"
"abdghi"
"adaaaa"
Why are they in order? Because you look at the first character, and compare them. Then look at the next character, and compare them, etc. Now look at your example:
"1.18.0"
"1.8.0"
Look at the first character of each, it's a "1", it's equal. Look at the next character, both are ".", they're equal. Look at the next characters, "1" and "8". "1" comes before "8". Therefore, "1.8.0" must come lexicographically after "1.18.0".
If you want to treat them like integers, there are a few things you can do. 1) You can write your own sort method in a block, or 2) wrap the strings in some hand-made Version object, and then write the comparator there.
If you need help with these specific ideas, let us know.
Shouldn't max() print the maximum value in the array?
Yes. It should. And it is doing so, as you can see in the results you got.
Do I have to sort the array before using max()?
No.
Is there any other way to get max value out of array?
Yes. But you already got the max value.
this method checks character by character.
So, comparing 1.9.0 and 1.19.0 , it will give 1.9.0 as bigger than 1.19.0 because the part "1." is equal in both strings, but the next character is 9 in the first one, and 1 in the second one, so the method will say 1.9.0 is bigger since 9 comes later than 1 in ASCII code.
When you need to sort version numbers the Versionomy Gem helps a lot. It also handles all the edge cases, for example beta and RC versions: 1.8beta < 1.8RC < 1.8.
Using that gem you must change your code to something like this:
require 'versionomy'
some_array.max_by { |version| Versionomy.parse(version) }

Lua table.sort issues

So, having some issues getting this table to sort correctly.
Basically, table.sort thinks 10 == 1, 20 == 2, and so on. I'll post my sort code below, but I'm not sure if that has anything to do with it. Is this just an inherent issue with the table.sort algorithm in Lua?
if svKey == "q" and metalMatch == true then
table.sort(vSort.metals, function(oneMQ, twoMQ)
return oneMQ.metalQ > twoMQ.metalQ
end)
end
Values stored in vSort.metals.metalQ are strings anywhere from 1 to 3 digits long. Is there a way to make table.sort differentiate between single-, double-, and triple-digit values?
The order operators work as follows. If both arguments are numbers, then they are compared as such. Otherwise, if both arguments are strings, then their values are compared according to the current locale. You can set the locale. Strings are compared lexicographically, which is generally character by character with shorter strings before longer strings.
If you want a numeric sort, then use, well, a numeric type. This might work:
function(oneMQ, twoMQ)
return tonumber(oneMQ.metalQ) > tonumber(twoMQ.metalQ)
end
It assumes that all the metalQ values are numeric. If not, coerce to a default or provide a fallback sort order in your sort expression for non-numeric values.

understanding custom partitioner in hadoop

i am learning partitioner concept now.can any one explain me the below piece of code.it is hard for me to understand
public class TaggedJoiningPartitioner extends Partitioner<TaggedKey,Text> {
#Override
public int getPartition(TaggedKey taggedKey, Text text, int numPartitions) {
return taggedKey.getJoinKey().hashCode() % numPartitions;
}
}
how this taggedKey.getJoinKey().hashCode() % numPartitions determine which reducer to be executed for a key?
can any one explain me this?
It's not as complex as you think once you break things down a little bit.
taggedKey.getJoinKey().hashCode() will simply return an integer. Every object will have a hashCode() function that simply returns a number that will hopefully be unique to that object itself. You could look into the source code of TaggedKey to see how it works if you'd like, but all you need to know is that it returns an integer based on the contents of the object.
The % operator performs modulus division, which is where you return the remainder after performing division. (8 % 3 = 2, 15 % 7 = 1, etc.).
So let's say you have 3 partitioners (numPartitions = 3). Every time you do modulus division with 3, you'll get either 0, 1, or 2, no matter what number is passed. This is used to determine which of the 3 partitioners will get the data.
The whole idea of partitioners is that you can use them to group data to be sorted. If you wanted to sort by month, you could pass every piece of data with the string "January" to the first partition, "December" to the 12th partitioner, etc. But in your case it on the outside looks a bit confusing. But really they just want to spread the data out (hopefully) evenly, so they're using a simple hash/modulus function to choose the partition at random.

Sorting strings containing numbers in a user friendly way

Being used to the standard way of sorting strings, I was surprised when I noticed that Windows sorts files by their names in a kind of advanced way. Let me give you an example:
Track1.mp3
Track2.mp3
Track10.mp3
Track20.mp3
I think that those names are compared (during sorting) based on letters and by numbers separately.
On the other hand, the following is the same list sorted in a standard way:
Track1.mp3
Track10.mp3
Track2.mp3
Track20.mp3
I would like to create a comparing alogorithm in Delphi that would let me sort strings in the same way. At first I thought it would be enough to compare consecutive characters of two strings while they are letters. When a digit would be found at some position of both the strings, I would read all digits following them to form a number and then compare the numbers.
To give you an example, I'll compare "Track10" and "Track2" strings this way:
1) read characters while they are equal and while they are letters: "Track", "Track"
2) if a digit is found, read all following digits: "10", "2"
2a) if they are equal, go to 1 or else finish
Ten is greater than two, so "Track10" is greater than "Track2"
It had seemed that everything would be all right until I noticed, during my tests, that Windows considered "Track010" lower than "Track10", while I thought the first one was greater as it was longer (not mentioning that according to my algorithm both the strings would be equal, which is wrong).
Could you provide me with the idea how exactly Windows sorts files by names or maybe you have a ready-to-use algorithm (in any programming language) that I could base on?
Thanks a lot!
Mariusz
Jeff wrote up an article about this on Coding Horror. This is called natural sorting, where you effectively treat a group of digits as a single "character". There are implementations out there in every language under the sun, but strangely it's not usually built-in to most languages' standard libraries.
The mother of all sorts:
ls '*.mp3' | sort --version-sort
The absolute easiest way, I found, was isolate the string you want, so in the OP's case, Path.GetFileNameWithoutExtension(), remove the non-digits, convert to int, and sort. Using LINQ and some extension methods, it's a one-liner. In my case, I was going on directories:
Directory.GetDirectories(#"a:\b\c").OrderBy(x => x.RemoveNonDigits().ToIntOrZero())
Where RemoveNonDigits and ToIntOrZero are extensions methods:
public static string RemoveNonDigits(this string value) {
return Regex.Replace(value, "[^0-9]", string.Empty);
}
public static int ToIntOrZero(this string toConvert) {
try {
if (toConvert == null || toConvert.Trim() == string.Empty) return 0;
return int.Parse(toConvert);
} catch (Exception) {
return 0;
}
}
The extension methods are common tools I use everywhere. YMMV.
Here's a Python approach:
import re
def tryint(s):
"""
Return an int if possible, or `s` unchanged.
"""
try:
return int(s)
except ValueError:
return s
def alphanum_key(s):
"""
Turn a string into a list of string and number chunks.
>>> alphanum_key("z23a")
["z", 23, "a"]
"""
return [ tryint(c) for c in re.split('([0-9]+)', s) ]
def human_sort(l):
"""
Sort a list in the way that humans expect.
"""
l.sort(key=alphanum_key)
And a blog post with more detail: https://nedbatchelder.com/blog/200712/human_sorting.html

Resources