I am coping with the formatting of numbers. I assumed that the .format allows to use multiple arguments:
a = 1.11111111111
b = 0.9
s = '({0:.2f}, {0:.2f})'.format(a, b)
print(s)
Returns:
'(1.11, 1.11)'
Instead of:
'(1.11, 0.90)'
On the other hand, this works fine:
'({}, {})'.format(a, b)
Returns:
'(1.11111111111111, 0.9)'
Any idea where is the problem?
You used for both of the the parameters the value a (0), you should call in the second param to value b (1).
the value before the : is for giving placeholders an explicit positional index.
This allows for re-arranging the order of display without changing the arguments.
Change
s = '({0:.2f}, {0:.2f})'.format(a, b)
To:
s = '({0:.2f}, {1:.2f})'.format(a, b)
Values before the : character specify either a field name or a conversion; by using 0 in both cases you're essentially telling .format, via the element index, to use a in both cases.
In your second case, '({}, {})'.format(a, b), by not specifying a position, .format replaces each empty pair of {} with the elements supplied in increasing order of position.
A simple replacement, as suggested, is to use 1 to indicate that you want to use b instead of a. Alternatively, simply omit them:
s = '({:.2f}, {:.2f})'.format(a, b)
to get a similar effect.
You should skim through the Syntax for the format strings to get an idea of the rules used when formatting.
Related
x=["zubin","viral","rushil"]
param=["x[0]","x[1]","x[2]"]
for paramValue in param:
p=paramValue # here i want to read "x[0]" and want to obtain value "zubin"
print(p) # This should print "zubin"
I want to read param and get the value of X list
Your's Thankful
If you need the index and the value you can use enumerate in pyhon
for index, paramValue in enumerate(param):
p=paramValue
i = index
You should find a better way to reference your list, for example via integer indexing. But here's one way using eval. Note there are security risks involved, so be sure your inputs are safe.
x = ["zubin", "viral", "rushil"]
param = ["x[0]", "x[1]", "x[2]"]
for p in param:
print(eval(p))
zubin
viral
rushil
I have a problem that asks me to combine three numeric variables, for example, a = 1 , b = 2 , c = 3. We're supposed to make d = ' 1|2|3 '. I've done some scripting in BASH where "|" refers to piping. How would you accomplish this in Python, and what does "|" mean?
The vertical bar is merely a character. Treat this the same way you would a comma or a dash. Convert each integer to string, and concatenate them with the bar in between. This is merely an exercise in string manipulation; nothing particularly deep or tricky.
Not sure what exactly you are expected to do or mean by "combining". In Python you can combine variables into tuples so that you can have a "compound" or combined variable that you can use for lookup tables, etc.
a = 1
b = 2
c = 3
combined = (a,b,c)
now you can do things like x = {combined: "value"}
The solution hspandher is on the right track, but would work primarily for one set of three numeric variables. It might be more useful to create a function like this:
def numberCombiner(a, b, c):
return str(a)+"|"+str(b)+"|"+str(c)
numberCombiner(1,2,3)
Which would take in three numeric values and output the string you want. Then you can call/invoke the numberCombiner function on different values, like:
numberCombiner(2,3,4)
which would return
>>> '2|3|4'
Using format:
>>> a=1
>>> b=2
>>> c=3
>>> "{}|{}|{}".format(a,b,c)
'1|2|3'
Just create a list of all the variables, and join them using the pipe symbol.
"|".join(map(str, [a, b, c]))
Use a generator expression to convert your integer variables into strings, and join them with the vertical line symbol.
"|".join(str(v) for v in [a, b, c])
More about generator expression.
More about string joining.
Thanks to juanpa.arrivillaga for pointing out the generator expression syntax where the generator is used right away by an enclosing function. See comments for more information.
I'm new to Python and I'm messing around with this and I don't really know why when I change the brackets to parenthesis I get an error, or why I can't just use len(text - 1).
I'm looking at this code:
def reverse(text):
result = ""
length = len(text)
for i in text:
result += text[length - 1]
length -= 1
return result
Any help with understanding this is greatly appreciated!
Thanks!
You can't use text - 1 because that doesn't make sense; you cannot take 1 from a string.
Now, as for len(...) this is a function so has () for the call rather than []. You pass it the thing you want the length of.
text[length - 1] is indexing into the string, at a position, and follows the list syntax for indexing or sub-scripting.
When you use len(text - 1) you try to subtract int (1) from str (text). It is insupported operation in python (and moreover, impossible). To get a part of string you need you must use text[length - 1].
Python Parentheses
Parentheses play many different roles in Python these are some of the main roles they play:
The mathematical role:
Python parentheses act like parentheses in math as they are at the top of the Python Priority Precedence
This means that this:
>>> 3 + 4 * 2
returns:
12
Whereas with parentheses:
>>> (3 + 4) * 2
returns:
14
But that's not all, their priority also expands to Boolean expressions:
for example:
False and False or True and True
evaluates to True as and is executed before or. However, if you add some parentheses:
False and (False or True) and True
It evaluates to False as the or is executed before the and
Tuple
In python, when you put something in a tuple you use () notation.
Functions
When you declare or call a function you always need to add the parentheses after the function name. Think of them as a basket to put the arguments in. If you forget them the Python interpreter will think that you are calling a variable for example:
list
This is a variable called list and does nothing special
list() #Empty basket
This, however, is a call to a function as there is a "basket"
Square Brackets
Square Brackets also have quite a few roles:
Lists
In python, you use square brackets if you want to declare a list instead of a tuple.
List comprehension
List comprehension is actually pretty complicated so read this for more information but just know that it uses square brackets
Looking up
The main use of square brackets is to look up a value inside a list, tuple, or dictionary. Think of it like the google search bar: you write what you want and it tells you what it has. For example:
tuple = (2, 4)
if you want to get 4 you will need to look up the 2nd value of the tuple:
tuple[1] #The first value is 0
Slicing
Slicing is simply the idea of taking only certain parts of a list (or tuple, dictionary or even string). Here is an explanation by Greg Hewgill (https://stackoverflow.com/a/509295/7541446):
There is also the step value, which can be used with any of the above:
a[start:end:step] # start through not past end, by step
The key point to remember is that the :end value represents the first
value that is not in the selected slice. So, the difference beween end
and start is the number of elements selected (if step is 1, the
default).
The other feature is that start or end may be a negative number, which
means it counts from the end of the array instead of the beginning.
So:
a[-1] # last item in the array a[-2:] # last two items in the
array a[:-2] # everything except the last two items
Python is kind to the programmer if there are fewer items than you ask
for. For example, if you ask for a[:-2] and a only contains one
element, you get an empty list instead of an error. Sometimes you
would prefer the error, so you have to be aware that this may happen.
I hope this provided useful insight to explaining the difference between parentheses and square brackets.
This means that in your question len() is a function where you are putting text inside the basket. However, when you call text[length-1] you are looking up the value at position length-1
The python builtin function len() returns the length in numbers of the object in argument e.g
temp = [1, 2, 3, 4]
length = len(temp)
then the len() will return 4 in this case. but if someone write
length = len(temp-1)
then
temp-1
is not a valid object, therefor you cannot use it.
The reason you can't do len(text-1) is because text is a string type you are trying to reverse, and being a string you cannot combine it with numbers(unless they are a string, but thats a different story) without getting an error. Therefore you do len(text)-1 because len(text) would equal the length of whatever the text is(lets say four), and then you can subtract 1 from 4 because 4 is an integer.
The reason you need brackets and not parentheses when you are doing text[length-1] is because in python trying to get a single value out of a string requires the use of string[] and putting a position in the string inside the []. You use partakes to call functions like print(string) or len(text), so putting text(length-1) would create an error that basically says the program doesn't have a function named "text" and it doesn't know what to do.
Hope this helps. Tell me if you have any more questions.
I am struggling with a Pyspark assignment. I am required to get a sum of all the viewing numbers per channels. I have 2 sets of files: 1 showing the show and views per show the other showing the shows and what channel they are shown on (can be multiple).
I have performed a join operation on the 2 files and the result looks like ..
[(u'Surreal_News', (u'BAT', u'11')),
(u'Hourly_Sports', (u'CNO', u'79')),
(u'Hourly_Sports', (u'CNO', u'3')),
I now need to extract the channel as the key and then I think do a reduceByKey to get the sum of views for the channels.
I have written this function to extract the chan as key with the views alongside, which I could then use a reduceByKey function to sum the results. However when I try to display results of below function with collect() I get an "AttributeError: 'tuple' object has no attribute 'split'" error
def extract_chan_views(show_chan_views):
key_value = show_chan_views.split(",")
chan_views = key_value[1].split(",")
chan = chan_views[0]
views = int(chan_views[1])
return (chan,views)
Since this is an assignment, I'll try to explain what's going on rather than just doing the answer. Hopefully that will be more helpful!
This actually isn't anything to do with pySpark; it's just a plain Python issue. Like the error is saying, you're trying to split a tuple, when split is a string operation. Instead access them by index. The object you're passing in:
[(u'Surreal_News', (u'BAT', u'11')),
(u'Hourly_Sports', (u'CNO', u'79')),
(u'Hourly_Sports', (u'CNO', u'3')),
is a list of tuples, where the first index is a unicode string and the second is another tuple. You can split them apart like this (I'll annotate each step with comments):
for item in your_list:
#item = (u'Surreal_News', (u'BAT', u'11')) on iteration one
first_index, second_index = item #this will unpack the two indices
#now:
#first_index = u'Surreal_News'
#second_index = (u'BAT', u'11')
first_sub_index, second_sub_index = second_index #unpack again
#now:
#first_sub_index = u'BAT'
#second_sub_index = u'11'
Note that you never had to split on commas anywhere. Also note that the u'11' is a string, not an integer in your data. It can be converted, as long as you're sure it's never malformed, with int(u'11'). Or if you prefer specifying indices to unpacking, you can do the same thing:
first_index, second_index = item
is equivalent to:
first_index = item[0]
second_index = item[1]
Also note that this gets more complicated if you are unsure what form the data will take - that is, if sometimes the objects have two items in them, other times three. In that case unpacking and indexing in a generalized way for a loop require a bit more thought.
I am not exactly resolving your code , but I faced same error when I applied join transformation on two datasets.
lets say , A and B are two RDDs.
c = A.join(B)
We may think that c is also Rdd , wrong. It is a tuple object where we cannot perform any split(",") kind of operations.One needs to make c into Rdd then proceed.
If we want tuple to be accessed, Lets say D is tuple.
E= D[1] // instead of E= D.split(",")[1]
I have a variable like this: ignore = val1,val2
But it's unclear for me how to use these values as separate ones.
Currently (with my knowledge) i need to hard code them like code below:
if (not Path.find("val1") > -1 ) and (not Path.find("val2") > -1 ):
etc
Now i want test added to it, and again i need to hard code it like this:
if (not Path.find("val1") > -1 ) and (not Path.find("val2") > -1 ) and (not Path.find("test") > -1 ):
Isn't there a better way of doing this?
If ignore is a tuple of value names:
if all(Path.find(v) <= -1 for v in ignore):
This has the advantage to stop as soon as the first condition is false. Just like your hard-coded example.
This is a tuple, one of the basic datatypes in Python.
You can access the different values using indexing notation, like ignore[0], ignore[1], etc.
However, if you're struggling with fundamental language features like this, I'd strongly recommend that you go read a Python tutorial before continuing.
ignore = var1, var2 essentially assigns ignore to a tuple, holding the values of var1 and var2. To access them, use ignore[0] or ignore[1] for the first and second elements respectively (in Python list/tuple indices start at 0, not 1).
Aside from this, you could also use collections.namedtuple. This allows you to treat the tuple like a class with attributes:
import collections
sometuple = collections.namedtuple('sometuple', 'var1 var2')
You can then access the element by name:
ignore = sometuple(var1, var2)
ignore.var1 # first element
ignore.var2 # second element
See the documentation for namedtuples here.
For general information on tuples, see the documentation.