Lets say I have string: s = '{1} goes to {0}'
And I want to format this string with list: l = ['Hollywood', 'Frankie']
I cannot modify string and list both. Is there way to write simple piece of code to handle this case?
PS. I know about question "Python Format String with List", but it is not what Im asking.
Use the unpack operator * when passing the list to the format method.
s = '{1} goes to {0}'
l = ['Hollywood', 'Frankie']
print(s.format(*l))
This outputs:
Frankie goes to Hollywood
Related
I have the following string below, and I want to get all the values before the equal sign and in a list.
asaa=tcp:192.168.40.1:1119 dsae=tcp:192.168.40.2:1115 dem=tcp:192.168.40.3:1117 ape=tcp:192.168.40.4:1116
Result should be:
asaa
dsae
dem
ape
Any help would be appreciated. been trying a couple different things but can get it into a list nor can i get the rest of the values.
s = 'asaa=tcp:192.168.40.1:1119 dsae=tcp:192.168.40.2:1115 dem=tcp:192.168.40.3:1117 ape=tcp:192.168.40.4:1116'
parts = s.split()
result = [part.split('=')[0] for part in parts]
print(result)
# ['asaa', 'dsae', 'dem', 'ape']
I ran a survey on Mechanical Turk, and the results were returned to me in a string formatted like this:
[{"Q1_option1":{"Option 1":true},"Q1_option2":{"Option 2":false},"Q2_option1":{"Option 1":true},"Q2_option2":{"Option 2":false}}]
I'm not the sharpest programmer out there, and I'm struggling with how to extract the boolean values from the string. I only need the "true" and "false" values in the order they appear.
I would really appreciate any help!
You can use the regular expression module re to extract the values
import re
string = '[{"Q1_option1":{"Option 1":true},"Q1_option2":{"Option 2":false},"Q2_option1":{"Option 1":true},"Q2_option2":{"Option 2":false}}]'
bool_vals = re.findall("true|false", string)
print(bool_vals)
bool_vals is a list that contains the values in the order they appeared in your input string.
I am brand new to Scala and having a tough time figuring this out.
I have a string like this:
a = "The dog crossed the street"
I want to create a list that looks like below:
a = List("The","dog","crossed","the","street")
I tried doing this using .split(" ") and then returning that, but it seems to do nothing and returns the same string. Could anyone help me out here?
It's safer to split() on one-or-more whitespace characters, just in case there are any tabs or adjacent spaces in the mix.
split() returns an Array so if you want a List you'll need to convert it.
"The dog\tcrossed\nthe street".split("\\s+").toList
//res0: List[String] = List(The, dog, crossed, the, street)
I want to find a specific string within a string.
For example, let's say I have the string
string = "username:quantopia;password:blabla
How can I then find quantopia?
I am using python 3.
Update: I am sorry I did not mention what I try before..
string.split('username:',1)[1].split(';',1)[0]
But this look very bad and not efficient, I was hoping for something better.
Just use regex as such:
import re
username = re.search("username:(.*);password", "username:quantopia;password:blabla").group(1)
print("username:", username)
This will output quantopia.
In this expression "username:(.*);password" you are saying "give me everything from username: to ;password" So this is why you're getting quantopia. This might as well be ":(.*);" as it will output the same thing in this case.
The simple solution is:
string = "username:quantopia;password:blabla"
username = "username"
if username in string:
# do work.
You might be better to just use split to create a dictionary so you dont need to use multiple regex to extract different parts of data sets. The below will split stirng into key value pairs then split key value pairs then pass the list of lists to dict to create a dictionary.
string = "username:quantopia;password:blabla"
data = dict([pairs.split(':') for pairs in string.split(';')])
print(f'username is "{data["username"]}" and password is "{data["password"]}"')
OUTPUT
username is "quantopia" and password is "blabla"
So let's say I have a string called Hi there.
I am currently using
m:match("^(%S+)")
to get just Hi from the string, now all I need to do is just get "there" from the string but I have no idea how.
Checkout this page: http://lua-users.org/wiki/SplitJoin
There are numerous ways to split words in a string on whitespace.
This one seems like it might be a good fit for your problem:
function justWords(str)
local t = {} -- create an empty table
-- create a function to insert a word into the table
local function helper(word) table.insert(t, word) return "" end
-- grab each word in the string and pass it to `helper`
if not str:gsub("%w+", helper):find"%S" then return t end
end
table = justWords(example)
table[1] -- hi
table[2] -- there
table[3] -- nil