Questions tagged [one-liner]

One Liners are an abridged one-line version of a multi-line script.

Originally, a one-liner program was textual input to the command-line of an operating system shell that performs some function in just one line of input. The one-liner can be

  • an expression written in the language of the shell;
  • the invocation of an interpreter together with program source for the interpreter to run;
  • the invocation of a compiler together with source to compile and instructions for executing the compiled program.

Certain dynamic scripting languages such as AWK, sed, and Perl have traditionally been adept at expressing one-liners. Shell interpreters such as Unix shells or Windows PowerShell allow for the construction of powerful one-liners.

The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line.

Find more information at:

153 questions
3
votes
2 answers

Java Matcher class to implement toFindResult()

According to this question, there is a big difference between find and matches(), still both provide results in some form. As a kind of Utility the toMatchResult function returns with the current results of the matches() operation. I hope my…
Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46
3
votes
1 answer

python for if one liner

I have an issue. I want to make my code simple and more comprehensible. I'm trying to get the next date's value from data x. Here is my code. Is there a way to make it shorter using lambda or map? def nextDay(date,x,time=1): res, c = None, 0 …
Juyun Lee
  • 51
  • 3
3
votes
5 answers

How to join Array to String (Java) in one line?

Let's assume there is an array: String[] myArray = new String[]{"slim cat", "fat cat", "extremely fat cat"}; Now I want to transform this array into a String joined with "&". So that the output becomes: slim cat&fat cat&extremely fat cat I am…
Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
2
votes
1 answer

How do I pipe files into whisper.cpp?

whisper.cpp only supports wav-files. I have files in other formats I want to transcribe. It would be nice if I could make the conversion and transcription in one step/using a one-liner. I have tried these two, and some variant, but they…
d-b
  • 695
  • 3
  • 14
  • 43
2
votes
1 answer

Inverse multi-line grep in perl one-liner

I have a text file with inconsistent formatting, but the relevant sections look like: CDS complement(99074..99808) /note="important in cell to cell spread of the virus, a tegument protein" …
2
votes
1 answer

PowerShell - How to achieve this logic as in bash command line { A ; { B && C ;} ;}

I am trying to achieve a specific logic in PowerShell 1-liner that is similar to the following bash command: { $another_command_group ;} && { A ; { B && C ;} ;} && { $another_command_group ;} { A ; { B && C ;} ;} The logic of this command is as…
wut
  • 35
  • 5
2
votes
3 answers

Can a list comprehension be divided in two lists?

I think I've caught the idea of one-line for loop, but now I have a problem. I know I can define a dataframe column using this like: df = pd.DataFrame(columns=["columnA"]) list = [0, 1, 2, 3, 4] df["columnA"] = [i for i in list] Now my question…
2
votes
4 answers

One liner to revert dictionary with non-unique values with O(n) complexity

Is it possible to produce one liner (i.e. comprehension) with better time complexity than O(n²) as below? my_map = {'A': 'x', 'B': 'y', 'C': 'x', 'D': 'z'} rev_map = {b: [a2 for a2 in my_map.keys() if my_map[a2] == b] …
aeiou
  • 337
  • 1
  • 7
2
votes
2 answers

f-string with curly brace and function

I searched for existing answers. I found answers for printing curly braces, but they do not include executing a function inside the f-string as well. The one marked as duplicate did not solve my problem. Part of the string that I'm trying to print…
sirEgghead
  • 236
  • 1
  • 12
2
votes
6 answers

How could one reduce the usage of helper functions in lambda expressions?

In this example I'm taking letters from a set and append them to a dictionary where the letter becomes the key and the literal 1 becomes the value to each pair. def base_dict_from_set(s): return reduce(lambda d,e : addvalue(1, e, d), s,…
2
votes
1 answer

one liner to create a dictionnary with values of list an an other one. Is it possible?

I have the following code : rx_dctvals = {} for key, val in pos_table.items(): rx_dctvals[re.compile("|".join(sorted([to_regex(v) for v in val], key=len, reverse=True)))] = key Is it possible to make a one liner…
Etienne Armangau
  • 255
  • 2
  • 10
2
votes
1 answer

Python one-liner for extracting all URLs

I need a python one-liner that will return all URLs found in a string and put it into a bash array. Something like: URLs=($(echo 'foo bar baz http://blackfridaygift.info/BUu4nmkRR baz foo bar…
Clayton Dukes
  • 1,297
  • 2
  • 11
  • 30
2
votes
1 answer

How do i validate and assign my output in a simple way

Is it possible to do something like this in python: I have a single 'span' that I want to scrape. I just want to simplify how I get the data, I will get an error if I convert the bs4 instance to text if it is None. and in general it would be nice if…
mama
  • 2,046
  • 1
  • 7
  • 24
2
votes
3 answers

Print only if not null in Python - one-line method?

I have a class object, Task, with four properties, t, date, priority and checked. Only t must contain a value, the other three properties are optional. I've written a print method which will print the strings if they're not null: class Task: def…
Lou
  • 2,200
  • 2
  • 33
  • 66
1
vote
1 answer

iterator yielding n-tuples from an iterator as oneliner expression

What I'm looking for is a oneliner-variant of the function batched(iterable, n) described in the code section of Itertools Recipes that will batch data into tuples of a certain length. Assume the source to be an iterator of arbitrary length, e.g. an…
antiplex
  • 938
  • 2
  • 12
  • 17
1
2
3
10 11