trafficLightsCount =12
bufferDist = '5 mi. '
intersectionCount = 20
print 'Found', trafficLightCount, light in
the intersectionCount, buffer and, 'buffereDist 'intersections.'
a='something'
b=12
c='Another thing'
print 'Found : ' + '{0},{1},{2}'.format(a,b,c)
You will get the output : Found : something,12,Another thing
Related
I have some string like this
' 12 2 89 29 11 92 92 10'
(all the numbers are positive integers so no - and no .), and I want to extract all numbers from it, edit some of the numbers, and then put them all together with the same whitespaces. For example, if I change the number 11 to 22, I want the final string as
' 12 2 89 29 22 92 92 10'
I did some search and most questions disregard the whitespaces and only care about the numbers. I tried
match = re.match((\s*(\d+)){8}, str)
but match.group(0) gives me the whole string,, match.group(1) gives me the first match \ 12 (I added the \ otherwise the website won't show the leading whitespaces), and match.group(2) gives me 12. But it won't give me any numbers after that, any index higher than 2 gives me an error. I don't think my approach is the correct one, what is the right way to do this?
I just tried re.split('(\d+)', str) and that seems to be what I need.
I'd recommend using a regular expression with non-capturing groups, to get a list of 'space' parts and 'number' parts:
In [15]: text = ' 12 2 89 29 11 92 92 10'
In [16]: parts = re.findall('((?: +)|(?:[0-9]+))', text)
In [17]: parts
Out[17]: [' ', '12', ' ', '2', ' ', '89', ' ', '29', ' ',
'11', ' ', '92', ' ', '92', ' ', '10']
Then you can do:
for index, part in enumerate(parts):
if part == '11':
parts[index] = '22'
replaced = ''.join(parts)
(or whatever match and replacement you want to do).
Match all numbers with spaces, change desired number and join array.
import re
newNum = '125'
text = ' 12 2 89 29 11 92 92 10'
^^
marray = re.findall(r'\s+\d+', text)
marray[6] = re.sub(r'\d+', newNum, marray[6])
print(marray)
[' 12', ' 2', ' 89', ' 29', ' 11', ' 92', ' 125', ' 10']
I have a line of code that is saying that there is invalid syntax in my print statement. Does anyone know about how to fix this? I've tried deleting the parentheses and have tried changing the +'s to ,'s.
print(stockName[random] + ' - $' + Names[0, amod] + ' : You have ' + x + ' of this stock')
If you use ' x ' with + operator it should be a string else you should use coma.
I'm having a slight problem with this function. I'm trying to generate a list of lists. For each item in those lists I need to remove the space to the left and to the right of each item. For some reason it's only working for the first item in each list.
def filereadlistoflists():
master_list=[]
input_file = open('movies.txt','r')
for line in input_file:
add_line = line.strip().split(",")
master_list.append(add_line)
return master_list
Here's the result:
[['Brad Pitt', ' Sleepers', ' Troy', ' Meet Joe Black', ' Oceans Eleven', ' Seven', ' Mr & Mrs Smith'], ['Tom Hanks', ' You have got mail', ' Apollo 13', ' Sleepless in Seattle', ' Catch Me If You Can']
I am a little confused on how regular expressions and sub works in Python.
I have this example:
nw = " textttt "
nw = re.sub(r'\s+(textttt)\s+', r'\1 ', nw)
The value in nw will be nw = "textttt ".
However if I have:
nw = " textttt "
nw = re.sub(r'\s(textttt)\s', r'\1 ', nw)
The value of nw will be nw = " textttt ".
Can someone please explain how the first and second results are generated and why they are different?
For clarity, let's replace spaces with digits:
import re
nw = "01textttt2345"
xx = re.sub(r'\d+(textttt)\d+', r'\1 ', nw)
print '[%s]' % xx # [textttt ]
xx = re.sub(r'\d(textttt)\d', r'\1 ', nw)
print '[%s]' % xx # [0textttt 345]
The first expression finds 01textttt2345 and replaces this with the value of the group(=textttt) plus a space. The second one finds only 1textttt2 and replaces that with textttt, leaving the rest of the string untouched.
\\s - works for single whitespace character
\\s+ - works for sequence of one or more whitespace characters.
I have a cell array 3x1 like this:
name1 = text1
name2 = text2
name3 = text3
and I want to parse it into separate cells 1x2, for example name1 , text1. In future I want to treat text1 as a string to compare with other strings. How can I do it? I am trying with regexp and tokens, but I cannot write a proper formula for that, if someone can help me with it please, I will be grateful!
This code
input = {'name1 = text1';
'name2 = text2';
'name3 = text3'};
result = cell(size(input, 1), 2);
for row = 1 : size(input, 1)
tokens = regexp(input{row}, '(.*)=(.*)', 'tokens');
if ~isempty(tokens)
result(row, :) = tokens{1};
end
end
produces the outcome
result =
'name1 ' ' text1'
'name2 ' ' text2'
'name3 ' ' text3'
Note that the whitespace around the equal sign is preserved. You can modify this behaviour by adjusting the regular expression, e.g. also try '([^\s]+) *= *([^\s]+)' giving
result =
'name1' 'text1'
'name2' 'text2'
'name3' 'text3'
Edit: Based on the comments by user1578163.
Matlab also supports less-greedy quantifiers. For example, the regexp '(.*?) *= *(.*)' (note the question mark after the asterisk) works, if the text contains spaces. It will transform
input = {'my name1 = any text1';
'your name2 = more text2';
'her name3 = another text3'};
into
result =
'my name1' 'any text1'
'your name2' 'more text2'
'her name3' 'another text3'