This question already has answers here:
Split string in Lua?
(18 answers)
Closed 9 years ago.
I have one string, that is = s = "Pedro Martinez, Defense"
I want to split the string before the comma and after the comma, store those cuts on 2 variables, for example:
I think that I need to use the string.gmatch function or string.sub
How can I do that?
The accepted answer at How to convert GPS coordinates to decimal in Lua? shows how to use string.match and patterns. Applying the same techniques as mentioned there, you could use
local name, expertise = string.match(s, "(.*),%s*(%a*)")
which would lead to name being "Pedro Martinez" and expertise being "Defense".
Related
This question already has answers here:
how to detect and get url on string javascript [duplicate]
(2 answers)
Closed 1 year ago.
I have been looking for a way to parse URL from a string.
My current goal is to parse
https://example.com/foo.png
from a string like
abcxyz https://example.com/foo.png gibberfish text.
Anyone got a solution or a package that can help me to do the job?
Thanks in advance.
text = "abcxyz https://example.com/foo.png gibberfish text."
console.log(text.split(" ")[1])
.split() splits the string at spaces into an array of words.
You can refer: https://www.w3schools.com/jsref/jsref_split.asp
I recommend this solution. It turns all words into an array and picks out the ones starting with https://
text.split(" ").filter(i => i.startsWith("https://")).toString();
This question already has answers here:
Regular expression to stop at first match
(9 answers)
Python non-greedy regexes
(7 answers)
Closed 2 years ago.
Consider following code:
import re
mystring = "\enquote {aaa} a \enquote {bbb}"
text = re.sub(r"\\enquote \{(.+)\}", r"\1", mystring)
print(text)
is outputing:
"aaa} a \enquote {bbb"
but I am acrually trying to achieve output of:
"aaa a bbb"
What have I misunderstood?
Background: I am trying to do some simple conversion from LaTeX format to general text, for which I need to replace LaTeX commands, but retain raw text itself (and sometimes do also some actual replacing). So how can I do group capture in python?
This question already has answers here:
Python convert tuple to string
(5 answers)
Closed 2 years ago.
I would like to know how to convert an unknown number of arguments to a string. what I need to do is redirect the output of a function that is supposed to go to print() to a Qt QPlainTextEdit. since it only accepts strings, I need a way to convert the given arguments to a string.
basically what I am looking for is print() but instead of outputting to the terminal, the output is to a string.
If you have an array of elements you can do:
def print_foo(arr,count):
for i in range(0,int(count)):
print(str(arr[i]))
Basically, converting to string is - newstr = str(varriable)
This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 3 years ago.
I have scoured the internet and I cannot find any reference to this type of for loop:
variable = [(item["attribute1"], item["attribute2]") for item in piece_of_json_data]
I am using this to update wtform's SelecField choices:
form.SelectField.choices = variable
but I can only get it to work if I replace one of the attributes in parenthesis with a static number:
variable = [(1, item["attribute2"]) for item in piece_of_json_data]
but that sets the value of the option field to "1", when I need the option values to be one of the attributes as a string.
Does this create a dict? a tuple? is there some kind of terminology for this that I can use to find documentation?
Thanks to the comments, I now understand that I am using list comprehension to create a tuple. The tuple works fine with both string and integer values. My issue has to do with .choices not accepting a string.
I found that my only problem is that I had coerce set to int on my selectfield, so naturally it wanted an integer.
This question already has answers here:
What is the purpose of the single underscore "_" variable in Python?
(5 answers)
Trying to understand Python loop using underscore and input
(4 answers)
Closed 4 years ago.
I was check a solution on hacker rank where i was solving a question asking to print the name of the person with the second highest score from an input which has to be converted to a nested list first .
I understood all the logic in the code and most part of the code but why the Underscore ( _ ) in the for loop .Please explain me the code if there is a different concept .
marksheet = []
for _ in range(0,int(input())):
marksheet.append([input(), float(input())])
second_highest = sorted(list(set([marks for name, marks in marksheet])))[1]
print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest]))
It's a Pythonic convention to use underscore as a variable name when the returning value from a function, a generator or a tuple is meant to be discarded.
In your example, the code inside the for loop does not make any use of the values generated by range(0,int(input())), so using an underscore makes sense as it makes it obvious that the loop does not intend to make use of it.