I'm having problems with a homework question.
"Write a function, to_str(a), that takes an array, a, converts each of
its elements to a string (using str(a[i])) and appends all these
strings together."
This is what I have
def to_str(a):
for i in a: a.append([i])
return str(a[i])
I have no idea how to use str(a[i]), I was wondering if someone can point me to the right direction
From the docs:
str(object) -> string
Return a nice string representation of the object. If the argument is
a string, the return value is the same object.
So str(a[i]) will return a string representation of a[i], i.e. convert a[i] to a string.
You will then need to concatenates the strings for all values of i.
As for your code, I have the following comments:
i is an element of a, not an index, as you might be thinking;
you are appending elements of a to a (endlessly, I'm afraid);
a[i] can cause an exception, because, like I said, i is an element, not an index;
you need to return a concatenation of strings, not a string from one element.
Also, if using str(a[i]) is not strictly mandatory, I'd suggest to skip it as unpythonic. You don't need indexes at all for this. Examples:
''.join(str(element) for element in a)
or
''.join(map(str, a))
will return what you need. In both cases str is applied to all elements of a.
The simplest-to-understand ("beginner") way without using indexes will be
s = ''
for element in a:
s += str(element)
return s
It's a bit less efficient, though it does effectively the same thing.
Converting each element into a string is easiest to use list comprehension:
[ str(i) for i in a ]
# equivalent to
[ str(a[i]) for i in range(len(a)) ]
# equivalent to
map(str, a) # most concise, use if you want to feel incredibly clever...
So you can write the function:
def to_str2(a):
''.join([str(i) for i in a]) # concatenates the list as a list of strings
.
Your code nearly does this:
def to_str(a):
new_a = [] # rather than use the same a!
for i in a:
new_a.append(str(i)) #convert i to string before appending
return new_a
The code meeting all the task criteria is rather something like:
def to_str(a):
return reduce(lambda x, y: x + str(y), a, '')
Which does things exactly in the mentioned way: first converts them to strings, then adds to the string made from already processed elements (which at the beginning is just emty string).
EDIT: The clearer (and supported by Python 3) way is to use explicit looping through elements. It does exactly the same, clarifying at the same time how reduce() works:
def to_str(a):
result = '' # third argument of reduce()
for item in a:
result += str(item) # does what reduce() lambda argument was doing
return result
the simplest way is:
[str(i) for i in a]
if you want a function:
def to_str(a):
return [str(i) for i in a]
Related
My question is to write a function which returns the longest string and ignores any non-strings, and if there are no strings in the input list, then it should return None.
my answer:
def longest_string(x):
for i in max(x, key=len):
if not type(i)==str:
continue
if
return max
longest_string(['cat', 'dog', 'horse'])
I'm a beginner so I have no idea where to start. Apologies if this is quite simple.
This is how i would do it:
def longest_string(x):
Strings = [i for i in x if isinstance(i, str)]
return(max(Strings, key=len)) if Strings else None
Based on your code:
def longest_string(x):
l = 0
r = None
for s in x:
if isinstance(s, str) and len(s) > l:
l = len(s)
r = s
return r
print(longest_string([None, 'cat', 1, 'dog', 'horse']))
# horse
def longest_string(items):
try:
return max([x for x in items if isinstance(x, str)], key=len)
except ValueError:
return None
def longest_string(items):
strings = (s for s in items if isinstance(s, str))
longest = max(strings, key=len) if strings else None
return longest
print(longest_string(['cat', 'dog', 'horse']))
Your syntax is wrong (second-to-last line: if with no condition) and you are returning max which you did not define manually. In actuality, max is a built-in Python function which you called a few lines above.
In addition, you are not looping through all strings, you are looping through the longest string. Your code should instead be
def longest_string(l):
strings = [item for item in l if type(item) == str]
if len(strings):
return max(strings, key=len)
return None
You're on a good way, you could iterate the list and check each item is the longest:
def longest_string(x)
# handle case of 0 strings
if len(x) == 0:
return None
current_longest = ""
# Iterate the strings
for i in x:
# Handle nonestring
if type(i) != str:
continue
# if the current string is longer than the longest, replace the string.
if len(i) > len(current_longest):
current_longest = i
# This condition handles multiple elements where none are strings and should return None.
if len(current_longest) > 0:
return current_longest
else:
return None
Since you are a beginner, I recommend you to start using python's built-in methods to sort and manage lists. Is the best when it comes to logic and leaves less room for bugs.
def longest_string(x):
x = filter(lambda obj: isinstance(obj, str), x)
longest = max(list(x), key=lambda obj: len(obj), default=None)
return longest
Nonetheless, you were in a good way. Just avoid using python´s keywords for variable names (such as max, type, list, etc.)
EDIT: I see a lot of answers using one-liner conditionals, list comprehension, etc. I think those are fantastic solutions, but for the level of programming the OP is at, my answer attempts to document each step of the process and be as readable as possible.
First of all, I would highly suggest defining the type of the x argument in your function.
For example; since I see you are passing a list, you can define the type like so:
def longest_string(x: list):
....
This not only makes it more readable for potential collaborators but helps enormously when creating docstrings and/or combined with using an IDE that shows type hints when writing functions.
Next, I highly suggest you break down your "specs" into some pseudocode, which is enormously helpful for taking things one step at a time:
returns the longest string
ignores any non-strings
if there are no strings in the input list, then it should return None.
So to elaborate on those "specifications" further, we can write:
Return the longest string from a list.
Ignore any element from the input arg x that is not of type str
if no string is present in the list, return None
From here we can proceed to writing the function.
def longest_string(x: list):
# Immediately verify the input is the expected type. if not, return None (or raise Exception)
if type(x) != list:
return None # input should always be a list
# create an empty list to add all strings to
str_list = []
# Loop through list
for element in x:
# check type. if not string, continue
if type(element) != str:
pass
# at this point in our loop the element has passed our type check, and is a string.
# add the element to our str_list
str_list.append(element)
# we should now have a list of strings
# however we should handle an edge case where a list is passed to the function that contains no strings at all, which would mean we now have an empty str_list. let's check that
if not str_list: # an empty list evaluates to False. if not str_list is basically saying "if str_list is empty"
return None
# if the program has not hit one of the return statements yet, we should now have a list of strings (or at least 1 string). you can check with a simple print statement (eg. print(str_list), print(len(str_list)) )
# now we can check for the longest string
# we can use the max() function for this operation
longest_string = max(str_list, key=len)
# return the longest string!
return longest_string
If I try the following snippets of code on a binary tree, and try to print the arr and string later, arr gives me the correct result but string is empty. Any thoughts? Is it something to do with lists being passed by reference and strings passed by value?
def post_order(root, arr = []):
if(root is not None):
post_order(root.left, arr)
post_order(root.right, arr)
arr.append(root.value)
def post_order1(root, string = ''):
if(root is not None):
post_order1(root.left, string)
post_order1(root.right, string)
string += str(root.value)
# assume I've made my binary tree
arr, string = [], ''
post_order(root, arr)
post_order1(root, string)
print arr, string
# arr holds the correct post-order sequence
# string is empty
In Python, lists are mutable and strings are immutable. This means that a list can be modified, but a string cannot. Strings can only be reassigned.
In your function, you are modifying the list using .append(), but you are only reassigning your string +=
Arr is an array, which you extend. String passed to post_order1 is an immutable object and a copy is created when updated. As a result, the original string stays unchanged.
You should rectify your code like this:
def post_order1(root, string = ''):
if not root : return string
left = post_order1(root.left, string)
right = post_order1(root.right, left)
return right + str(root.value)
I'm trying to write a recursive python function that takes in a list for example [1,2,3,4] and returns an integer 1234. Any help on how to do this
def listtoint(lst):
if lst==[]:
return 0
return lst[-1:]+clti(lst/10)
I know you can't divide the list but I would like a way to get around it
def listtoint(lst):
if lst == []:
return 0
s = ''.join([str(i) for i in lst])
return int(s)
How this works is: ''.join(some_list) takes every element of the list and concatenates them into one long string. every element of some_list here must already be a string, thus the list comprehension in the code above.
int is then used to turn the resulting string into an integer.
There should be error checking but you can deal with that. Also, this isn't recursive and doesn't need to be.
To do this recursively...
def listtoint(lst):
if lst==[]:
return 0
iPower = 10**(len(lst)-1)
return lst[0]*iPower + listtoint(lst[1:])
Can someone explain the structure of reduce() in the following example:
def f2(list):
return reduce(lambda string, item: string + chr(item), list, "")
I know that f2 converts a list of int's into a string, but my problem is understanding
reduce in this context.
I know the basic structure of reduce is reduce(function, sequence[, initial]) but this
is somehow confusing to me.
Can someone explain reduce(lambda string, item: string + chr(item), list, "") and give me some similar examples ?
Thanks in advance.
return reduce(lambda string, item: string + chr(item), list, "")
roughly translates to
string = ""
for item in list:
string = string + chr(item)
return string
Reduce does something usually called a fold. E.g., if you have a list ls = [a,b,c,d] and a binary operation def plus(x,y): x + y, then reduce(plus, ls) gets folded to
plus(plus(plus(a, b), c), d)
which equals
(((a+b)+c)+d)
Your f2 is doing something similar, namely appending strings (after converting them from integers): (I really hope those parens match...)
(((("" + chr(a)) + chr(b)) + chr(c)) + chr(d))
with a supplied initial value of "" (which is needed when a folding operation has two different input types)
# python experts: I'm not sure if reduce is a left fold, it seemed more naturally to me. Please tell me if I'm wrong.
The code applies chr() to every element of the list, and concatenates the results into a single string.
The reduce() call is equivalent to the following:
return "" + chr(list[0]) + chr(list[1]) + ... + chr(list[list.length - 1])
The "" is the third argument to reduce(). The lambda function in
return reduce(lambda string, item: string + chr(item), list, "")
is called for every item in the list. It simply appends chr(item) to the result of the previous iteration.
For more examples of using reduce(), see Useful code which uses reduce() in python
I'm stumped on how to construct a function that works on lists within lists from inside out (I guess that's how you could poorly describe it).
I'm trying to dynamically turn a list like
res = SomeDjangoQuerySet
x = ['neighborhood', ['city', ['metro', 'metro']]]
into:
getattr(getattr(getattr(getattr(res, 'neighborhood'), 'city'), 'metro'), 'metro')
AKA:
getattr(getattr(getattr(getattr(res, x[0]), x[1][0]), x[1][1][0]), x[1][1][1])
Basically, the first value will always be a string, the second value will either be a string or a list. Each list will follow this pattern (string, string OR list). The depth of lists within lists is indeterminate. The innermost first value of the getattr() will be an outside variable ('res' in this case). Any advice?
This sounds like recursion and iteration might be useful. Does this do what you want?
def flatten(data):
res = []
if hasattr(data, '__iter__'):
for el in data:
res.extend(flatten(el))
else:
res.append(data)
return res
reduce(getattr, flatten(x), res)
I ended up putting in some time and learning about recursion and found this to be the simplest solution (although, credit to David Zwicker who also provided a working solution).
def recursion(a, b):
if type(b) is list:
return recursion(getattr(a, b[0]), b[1])
else:
return getattr(a, b)
recursion(res, x)
def nestattr(x, y):
if isinstance(y, str):
return getattr(x, y)
elif isinstance(y, list):
return nestattr(getattr(x, y[0]), y[1])
nestattr(res, x)
So you start off with the first string in the list, and you have the getattr of (1) the query with (2) that string. Then you recurse using the rest of that list, and if it's a string, you just do the getattr on (1) the result of the previous getattr with (2) this string. Otherwise, if it's still a list, you repeat. I think this is what you're looking for? Correct me if I'm wrong.