I need to give an output like this:
/Fotos/Azoren-2018/buffet
/Fotos/Azoren-2018/restaurant/dc
/Fotos/Azoren-2018/restaurant/dc-41.jpg
/Fotos/Azoren-2018/restaurant/dc-42.jpg
/Fotos/Azoren-2018/restaurant/dc-43.jpg
/Fotos/Mallorca-2017/dc-10.jpg
/Fotos/Mallorca-2017/dc-11.jpg
/Fotos/Mallorca-2017/dc-19.jpg
But I think my output is a set:
#{"/Fotos/Mallorca-2017/dc-10.jpg" "/Fotos/Azoren-2018/buffet" "/Fotos/Azoren-2018/restaurant/dc-43.jpg" "/Fotos/Azoren-2018/restaurant/dc-42.jpg" "/Fotos/Mallorca-2017/dc-19.jpg" "/Fotos/Azoren-2018/restaurant/dc-41.jpg" "/Fotos/Mallorca-2017/dc-11.jpg" "/Fotos/Azoren-2018/restaurant/dc"}
Any ideas how can I convert that set into this output?
Use sort with clojure.string/join:
(->> #{"/Fotos/Mallorca-2017/dc-10.jpg" "/Fotos/Azoren-2018/buffet" "/Fotos/Azoren-2018/restaurant/dc-43.jpg" "/Fotos/Azoren-2018/restaurant/dc-42.jpg" "/Fotos/Mallorca-2017/dc-19.jpg" "/Fotos/Azoren-2018/restaurant/dc-41.jpg" "/Fotos/Mallorca-2017/dc-11.jpg" "/Fotos/Azoren-2018/restaurant/dc"}
sort
(clojure.string/join "\n")
println)
Output:
/Fotos/Azoren-2018/buffet
/Fotos/Azoren-2018/restaurant/dc
/Fotos/Azoren-2018/restaurant/dc-41.jpg
/Fotos/Azoren-2018/restaurant/dc-42.jpg
/Fotos/Azoren-2018/restaurant/dc-43.jpg
/Fotos/Mallorca-2017/dc-10.jpg
/Fotos/Mallorca-2017/dc-11.jpg
/Fotos/Mallorca-2017/dc-19.jpg
=> nil
Related
People,
I started studying clojure a few time ago and i have one question.
In this function:
(defn print-coords [coords]
(let [lat (first coords)
lon (last coords)]
(println (str "Latitude: " lat " - " "Longitude: " lon))))
=> #'dev-projects.core/print-coords
I'm defining a global function with one parameter.
But, if i invoke these arguments:
(print-coords [54 32])
Latitude: 54 - Longitude: 32
=> nil
I will get a sucessful answer.
Why does it work? The original function shouldn't only work with one argument?
I think you confuse (print-coords [53 32]) to be (print-coords 54 32).
In the first case, coords is the vector [53 32] whose first is 54 -> lat and whose second indeed is lon 32. Return value is correctly nil, since println returns nil.
However:
user=> (print-coords 54 32)
Execution error (ArityException) at user/eval141 (REPL:1).
Wrong number of args (2) passed to: user/print-coords
The question is how [coords] in the defn is compatible with [53 32] in the call.
A defn's parameters are surrounded by square brackets, but a function call's arguments are not wrapped in any punctuation. Thus [53 32] is literally a vector passed as the 1 argument to the function call. [53 32] corresponds to coords.
By the way, print-coords could be defined more idiomatically like this:
(defn print-coords [[lat lon]]
(println (str "Latitude: " lat " - " "Longitude: " lon)))
It is still a function of 1 argument. The only difference is that the 1 argument is immediately destructured and names are bound to the first two members. Now it is pleasantly clear how [53 32] corresponds [lat lon].
I have a list of values contain both scientific and floating numbers,need to convert the non scientific values to scientific values for list operations.
set Y_value [list 1.215647671415354e-7 1.1284486163276597e-6 4.538622670224868e-5 4.4706815970130265e-5 8.492852430208586e-6 6.077577836549608e-6 3.1041158763400745e-6
0.00015045881445985287 4.1016753016265284e-7 1.165599314845167e-6 1.8736355968940188e-6 2.9444883693940938e-5 2.5420340534765273e-5 2.0819682049477706e-6 9.529731869406532e-6
8.549810104341304e-7 1.558014082547743e-5 8.079621693468653e-6 4.868444739258848e-5 0.0001646481396164745]
This is the current list, I want to convert the floating values of the list to scientific values, and store them in new list using TCL.
Expected output
1.2156476714e-04 1.1284486163e-03 4.5386226702e-02 4.4706815970e-02 8.4928524302e-03 6.0775778365e-03
3.1041158763e-03 1.5045881446e-01 4.1016753016e-04 1.1655993148e-03 1.8736355969e-03 2.9444883694e-02
2.5420340535e-02 2.0819682049e-03 9.5297318694e-03 8.5498101043e-04 1.5580140825e-02 8.0796216935e-03
4.8684447393e-02 1.6464813962e-01
For creating a new list based on an existing one, lmap is the way to go.
Something like:
set result [lmap y $Y_value { format %.10e [expr {$y * 1000}] }]
Note: your example output values are 1000x those of your input values. For example, input 1.215647671415354e-7 becomes output 1.2156476714e-04. I've added this in the code as factor.
You can use tcl's format command to do this, as follows:
set Y_value [list 1.215647671415354e-7 1.1284486163276597e-6 4.538622670224868e-5 4.4706815970130265e-5 8.492852430208586e-6 6.077577836549608e-6 3.1041158763400745e-6 \
0.00015045881445985287 4.1016753016265284e-7 1.165599314845167e-6 1.8736355968940188e-6 2.9444883693940938e-5 2.5420340534765273e-5 2.0819682049477706e-6 9.529731869406532e-6 \
8.549810104341304e-7 1.558014082547743e-5 8.079621693468653e-6 4.868444739258848e-5 0.0001646481396164745]
set factor 1000
set Y_value_scientific [list]
foreach v $Y_value {
lappend Y_value_scientific [format %1.10e [expr {$v*$factor}]]
}
puts $Y_value_scientific
Note: if you are using Tcl 8.6 or newer, I recommend the one-liner answer by #Shawn using lmap.
I have a function
(defn f-test [c-width] (format "%-(c-width)s" "boogey"))
This is what happens when I try to evaluate it
(f-test 10)
FormatFlagsConversionMismatchException Conversion = c, Flags = ( java.util.Formatter$FormatSpecifier.failMismatch (Formatter.java:4041)
But this is what I am trying to do
(format "%-10s" "boogey")
"boogey "
how can I substitute in c-width inside the format call?
Try this one:
(defn f-test
[c-width]
(format (str "%-" c-width "s") "boogey"))
i have a list....
lst = [[45], [78], [264], [74], [67], [4756], [455], [465], [4567], [4566]]
and i want to add a zero at the beginning so it looks like this....
lst = [[0], [45], [78], [264], [74], [67], [4756], [455], [465], [4567], [4566]]
This doesn't work...
lst[0] = [0]
or This...
lst.append(0,[0])
what will actually work?
cheers
As the name goes, append always adds at the end. You need to do this:
lst.insert(0, [0])
in stead of lst.append(0,[0])
I have a dictionary, say d1 that looks like this:
d = {'file1': 4098, 'file2': 4139, 'file3': 4098, 'file4': 1353, 'file5': 4139}
Now, I've figured out how to get it to tell me if there are any dublicates or not. But what I'd like to get it to do is tell me if there are any, and what 2 (or more) values (and corresponding keys) are dublicates.
The output for the above would tell me that file1 and file3 are identical and that file2 and file5 are identical
I've been trying to wrap my head around it for a few hours, and haven't found the right solution yet.
try this to get the duplicates:
[item for item in d.items() if [val for val in d.values()].count(item[1]) > 1]
that outputs:
[('file3', 4098), ('file2', 4139), ('file1', 4098), ('file5', 4139)]
next sort the list by the second item in the tuple:
list = sorted(list, key=operator.itemgetter(1))
finally use itertools.groupby() to group by the second item:
list = [list(group) for key, group in itertools.groupby(list, operator.itemgetter(1))]
final output:
[[('file3', 4098), ('file1', 4098)], [('file2', 4139), ('file5', 4139)]]