This question already has answers here:
Specify length of Sequence or List with Python typing module
(4 answers)
How to limit function parameter as array of fixed-size?
(3 answers)
Closed last month.
I am writing a function that needs to take in an ordered pair coordinate (represented in a tuple). The problem is that I don't know a way to require it to have exactly two elements in the parameters of the function. Is there a better way to handle this kind of situation (like a better data type to use)? I can't think of anything.
def function(coordinate: tuple):
pass
A tuple needs to be of a given length for some non-specific function to be executed properly.
We can check the number of elements in the tuple element by using the len() function on it that comes with python directly. Wrapped in a short if condition we can only execute the said function when we have guaranteed that the given tuple has 2 elements in it. Otherwise we may print a short error message for the user.
if len(your_tuple) == 2:
function(your_tuple)
else:
print("Please enter only 2D coordinates")
Related
This question already has answers here:
Apply function to each element of a list
(4 answers)
How do I iterate through two lists in parallel?
(8 answers)
Closed 11 months ago.
Right, so this might be a rather confusing question. But in my Computer science class, we were given this optional challenge to calculate the distances a catapult has achieved after a user inputs a set of angles and speeds. But the challenge is that we have to split the problem into multiple smaller functions but each function is only allowed to have one statement per function.
I think I have a way to do it but I'm having trouble with one part.
[x = get_angles(), y = get_speeds(): 2*x[i]*y[i]/GRAVITY for i in range(len(x))]
This is part of my code for creating a list of the distances travelled. Now, this is effectively pseudo-code cause I have no clue how to make it work. Does python have anything that allows something like this to happen?
Any help would be great. Sorry for being so long-winded but thanks anyway :)
Trying to change the line of code you provided into something that works, I got this:
(lambda x, y: [2*x[i]*y[i]/GRAVITY for i in range(len(x))])(get_angles(), get_speeds())
This trick uses a lambda function to "store" the x, y values and use them in the same line.
I'm not sure this does exactly what you want, but this lambda function trick still applies.
This question already has answers here:
Is there any built-in way to get the length of an iterable in python?
(10 answers)
What's the shortest way to count the number of items in a generator/iterator?
(7 answers)
Closed 4 years ago.
Is there anyway to see the len() of an itertools.Combination or other object, really, without materializing it to a list?
I can get the cardinality of combs or permutations with the factorials,... but I want something that generalizes.
Thanks
For any iterable it, you can do:
length = sum(1 for ignore in it)
That doesn't create a list, so the memory footprint is small. But for many kinds of iterables, it also consumes it (for example, if it is a generator, it's consumed and can't be restarted; if it is a list, it's not consumed). There is no generally "non-destructive" way to determine the length of an arbitrary iterable.
Also note that the code above will run "forever" if it delivers an unbounded sequence of objects.
No need to create a list. You can count the number of items in an iterable without storing the entire set:
sum(1 for _ in myIterable)
Yes,
def count_iterable(i):
return sum(1 for e in i)
Taken from: Is there any built-in way to get the length of an iterable in python?
This question already has answers here:
Can a variable number of arguments be passed to a function?
(6 answers)
Closed 4 years ago.
I want to have an argument in a function that fills an array with multiple strings, so that i have
def Test(colors):
colorarray = [colors]
which i can fill with
Test(red,blue)
but i always get either the takes 2 positional arguments but 3 were given error or the single strings do not get accepted (e.g. TurtleColor(Color[i]) tells me "bad color string: "red","blue")
i know i can pass the strings as seperate arguments, but i kind of want to avoid having that many different arguments.
You need to read input arguments as a list
def Test(*colors):
colorarray = [*colors]
print(colorarray)
Test('red','blue')
This question already has answers here:
Python- Turning user input into a list
(3 answers)
Create a tuple from an input in Python
(5 answers)
Closed 10 months ago.
Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.
values = input("Input some comma separated numbers : ")
list = values.split(",")
tuple = tuple(list)
print('List : ',list)
print('Tuple : ',tuple)
This does work but is there any other easier way?
If you're looking for a more efficient way to do this, check out this question:
Most efficient way to split strings in Python
If you're looking for a clearer or more concise way, this is actually quite simple. I would avoid using "tuple" and "list" as variable names however, it is bad practice to name variables as their type.
Well, the code that you have written is pretty concise but you could remove few more line by using the below code:
values = input("Enter some numbers:\n").split(",")
print(values) #This is the list
print(tuple(values)) #This is the tuple
This question already has answers here:
Why does Python code use len() function instead of a length method?
(7 answers)
Closed 7 years ago.
I'm new to Python and I have a question about the string operations. Is there an over-arching reason that I should understand as to why the lower operation is written as 'variable.lower()' while another one, say length, would be written as 'len(variable)'?
lower is a string method, that is, a function built in to the string object itself. It only applies to string objects.
len is a built in function, that is, a function available in the top namespace. It can be called on many different objects (strings, lists, dicts) and isn't unique to strings.