How to format a flat string with integers in it in erlang? - string

In erlang, I want to format a string with integers in it and I want the result to be flattened. But I get this:
io_lib:format("sdfsdf ~B", [12312]).
[115,100,102,115,100,102,32,"12312"]
I can get the desired result by using the code below but it is really not elegant.
lists:flatten(io_lib:format("sdfsdf ~B", [12312])).
"sdfsdf 12312"
Is there a better formatting strings with integers in them, so that they are flat? Ideally, using only one function?

You flatten a list using lists:flatten/1 as you've done in your example.
If you can accept a binary, list_to_binary/1 is quite efficient:
1> list_to_binary(io_lib:format("sdfsdf ~B", [12312])).
<<"sdfsdf 12312">>
However, question why you need a flat list in the first place. If it is just cosmetics, you don't need it. io:format/1,2,3 and most other port functions (gen_tcp etc) accept so called deep IO lists (nested lists with characters and binaries):
2> io:format([115,100,102,115,100,102,32,"12312"]).
sdfsdf 12312ok

There is an efficiency reason that io_lib:format returns deep lists. Basically it saves a call to lists:flatten.
Ask yourself why you want the list flattened. If you are going to print the list or send it to a port or write it to a file, all those operations handle deep lists.
If you really need a flattened list for some reason, then just flatten it. Or you can create your own my_io_lib:format that returns flattened lists if you think it important.
(If you only want to flatten the list for debugging reasons then either print your strings with ~s, or create a flattener in an erlang module named user_default. Something like this:
-module(user_default).
-compile(export_all).
%% either this:
fl(String) ->
lists:flatten(String).
%% or this:
pp(String) ->
io:format("~s~n", [String]).
Then you can use fl/1 and print/1 in the Erlang shell (as long as user_default.beam is in your path of course).)

Related

Doing Apply(str) to int creates a bunch of \n's?

I have a dataframe like this labeled by year:
I would like to change these ints to strs because they are actually categorical variables. However, this is the result:
X_train['YrSold'] = X_train['YrSold'].apply(str)
I would prefer to get rid of all those \n's automatically in the .apply(str) process, as opposed to postprocessing each column via regex. Seems like the latter would have more room for error.
Solution was astype(str) instead of apply(str)

Python 3 - string to list (I know it has been asked but I can't get anything to work)

I have an assignment, similar to scrabble. I have to check if a subset is in the set. can only use a letter once. so if subset has 2t and the set has 1t it is false.
My problem is, I used 2 inputs to allow people to enter the subset and set, but that create a string no breaks between the letters which mean split or list won't create a LIST with individual letters. (at least I can't find any way.)
My plan was something like
wordset = word.lower().split()
subset = letters.lower()
for i in range(len(subset)):
if i in subset and in set:
set.remove(i)
I know that properly won't work but until I can get it into a list or someone gives me a hint how to do it with string I can't start testing it. Sorry for so much writing.
If you wish to get a list of characters in a given string you can use a list comprehension:
characters = [x for x in some_string]

modgrammar import lists to use as disjunct literals?

I'd like to be able to have files that contain lists of terms that I can read and use in a modgrammar grammar, but OR() doesn't work on a Python list as far as I can tell...
from modgrammar import *
with open(termfile) as f:
terms = [x.strip() for x in f.readlines()]
class SomeGrammar(Grammar):
grammar = (OR(terms))
Trying to parse strings that begin with anything but the first term in the list throws an exception. Is there a way to do this cleanly?
Modgrammar will interpret a list as a series of terms to match in order, so OR(terms) is interpreted as "match these in order OR (nothing else)", which isn't what you're looking for.
Fortunately, Python has a built-in syntax to take a list and pass it as multiple arguments for a function (like OR). You should be able to use OR(*terms) to do what you want instead.

Checking if values in List is part of String

I have a string like this:
val a = "some random test message"
I have a list like this:
val keys = List("hi","random","test")
Now, I want to check whether the string a contains any values from keys. How can we do this using the in built library functions of Scala ?
( I know the way of splitting a to List and then do a check with keys list and then find the solution. But I'm looking a way of solving it more simply using standard library functions.)
Something like this?
keys.exists(a.contains(_))
Or even more idiomatically
keys.exists(a.contains)
The simple case is to test substring containment (as remarked in rarry's answer), e.g.
keys.exists(a.contains(_))
You didn't say whether you actually want to find whole word matches instead. Since rarry's answer assumed you didn't, here's an alternative that assumes you do.
val a = "some random test message"
val words = a.split(" ")
val keys = Set("hi","random","test") // could be a List (see below)
words.exists(keys contains _)
Bear in mind that the list of keys is only efficient for small lists. With a list, the contains method typically scans the entire list linearly until it finds a match or reaches the end.
For larger numbers of items, a set is not only preferable, but also is a more true representation of the information. Sets are typically optimised via hashcodes etc and therefore need less linear searching - or none at all.

Set of strings efficient implementation

Is there an easy way to create a set of strings in Matlab?
I am going through a list of filepaths and want to get all names of folders at a specific level.
But since in some folders there are several files, I get these folders several times.
I know there would be the possibility to create a cell array and check every time if the current folder name is already in the array, and if not, add it.
Another option would be to use the java HashSet class.
But is there any easy inbuilt Matlab way to do something like that?
I can't use a Vector since it would create a vector of chars not strings.
Unfortunately there's nothing as efficient as Java Set implementations.
But you can use set operations. Either union when you add, or just call unique on your collection with duplicates.
You could use the rdir script... MATLAB file exchange to the rescue!
Use it like this:
listing = rdir(name);
The function returns a structure listing similar to the built-in dir command.
It should save you the headache of iterating through a directory tree yourself.
How about "unique":
x = {'dog', 'cat', 'cat', 'fish', 'horse', 'bird', 'rat', 'rat'};
x_set=unique(x)
x_set =
'bird' 'cat' 'dog' 'fish' 'horse' 'rat'

Resources