error<0x275b990> - what does this mean [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this function, but i'm not familiar with the error or how to correct it.
def intify(file1):
numbers=range(0,10)
strnum=[]
for x in numbers:
strnum.append(str(x))
number1=[]
for line in file1:
for split in line.split(' '):
number1.append(split)
listnum=[]
for x in number1:
if x[0] in strnum:
listnum.append(x)
w=map(float, listnum)
#return w
print(w)
error map object at 0x275b990

error map object at 0x275b990
It is not an error - you just print address of iterator, returned by map. You could print list(w) to make sure that everything's alright. Of course, you should not return list(w) since it is unnecessary and expensive.

In Python 3+, map() is a class, not a function (documentation here). A map instance can be iterated over and each item in the sequence will be processed only as needed ("lazy evaluation"). This way an actual list need never be created, saving time and memory, and if you don't need all the items (e.g., you stop processing when you hit a certain value) then you can save a bunch of function calls, too. In general, then, you will want to do something like this with map():
for num in map(float, listnum):
print(num) # or do something else
If you really do want an actual list, just convert it to one: the list() constructor accepts any iterable, including a map() instance:
w = list(map(float, listnum))
What you're seeing when you print a map object is not an error, but rather the default Python display (repr()) of objects. The code is the id() of the object in hexadecimal, which in CPython is its memory address; this is useful mainly for distinguishing one instance from others. You'll be seeing this a lot, so get used to it.

Related

If I have two output values from one function, can I use the outputs as arguments (type int) for an array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If I have a function in Python which returns two outputs (e.g. winner and loser) that are of type int, could that output be used as an index for an array? Like G[func()[0]] (where G is a numpy matrix or an array). More specifically, if that function contains a randomly generated number, will the output value be different each time it is called? Is there a way to extract the returned values from one function call?
When a function returns multiple values, it's actually returning a tuple. So you can certainly index it as func()[0] or func()[1] to get just one of them, since that's how you access tuple elements.
If you call it repeatedly, it will return whatever the function returns each time, which could be random. There's nothing special about calling a function as part of an expression.
The idiomatic way to extract the returned values is by using unpacking assignment:
winner, loser = func()
print(G[winner])

Can someone please explain how loop will work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
for x in s[:].split():
s = s.replace(x, x.capitalize())
I want to know how the for loop will progress and what exactly s[:] means and will do?
Assuming s is a string, s[:] makes a copy of the string, and split() splits the string at spaces and returns an array of substrings, the for loop will then iterate over the substrings.
It's actually unnecessary because split returns an array, so even the though the for loop modifies the original string, the loop iterable isn't reevaluated multiple times, so you don't need to copy it.
s is very likely to be a string because the split is a method in str (of course, you can also say that s is an instance, which comes from a class that is defined by the user in which also has a split method ...)
Normally s[:] is like a slice. The following can help you to understand.
s ='abc AB C dd'
print(s)
print(s[:]) # same as s
print(s[:3]) # abc
print(s[4:6]) # AB
print(s[-1]) # d
for x in s[:].split():
s = s.replace(x, x.capitalize())
print(s) # Abc Ab C Dd # Now you know the capitalize is what, right?
digression
The following is a digression.
I think your question is very bad,
First, this question is very basic.
second, its subject is not good.
Note that an ideal Stack Overflow question is specific and narrow -- the idea is to be a huge FAQ.
And now, you tell me searching how the loop will work? I mean, if you are a programmer who must know how the loop it is.
so when you ask a question, you have to think twice about what the title name can benefit everyone. (not just you only)
I suggest that you can delete this question after you understand it.

TypeError: 'dict_keyiterator' object is not subscriptable - Python 2 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to get the nth predecessor of an object:
u = G.predecessors(v)[0]
But this returns the following error on Python 2:
TypeError: 'dict_keyiterator' object is not subscriptable
And on Python 3:
TypeError: 'dict_keys' object is not subscriptable
How can I fix this, please?
Your method is returning an iterator (that can be iterated element by element exactly once, destructively), not a sequence (that can be indexed, iterated repeatedly, etc.).
If you just want to pull the first element from the iterator and discard the rest, change the code to:
u = next(G.predecessors(v))
This will raise StopIteration if nothing is in the iterator at all, otherwise, it returns the first value. To make it silently return a default when the iterator is empty, pass a second argument to next, e.g. to get None:
u = next(G.predecessors(v), None)
If you need the first element, but might also need the rest later, a useful trick is unpacking:
u, *rest = G.predecessors(v)
which puts the first value in u, then stores the rest to a list named rest that can be indexed or iterated on demand. Or just convert the whole thing to a list and use it as such:
allvals = list(G.predecessors(v))
u = allvals[0] # And do other stuff with it

Is there some benefits for using enumerate instead of range in python? [duplicate]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Could you please tell me why it is considered as "not pythonic" when I need the index and the value when looping over a list and use:
a = [1,2,3]
for i in range(len(a)):
# i is the idx
# a[i] is the value
but rather it is recommended to use
for idx, val in enumerate(a):
print idx, val
who defines "pythonic" and why is the latter one better? I mean it's not that much better concerning readability, is it!?
Thanks in advance
First of all, the first way is ugly: You either need a separate variable assignment to get the element or use a[i] all the time which could theoretically be an expensive operation. Imagine a being a database cursor: When you iterate it (a.__iter__ being called) the object can safely assume that you are going to iterate over all its items. So all or at least multiple rows could be retrieved at once. When getting the length such an optimization would be stupid though since you surely don't want to retrieve data just because you want the number of items. Also, when retrieving a specific item you cannot assume that other items will be retrieved, too.
Additionally, using enumerate() works with any iterable while range(len()) only works with countable, indexable objects.

Pythonic: range vs enumerate in python for loop [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Could you please tell me why it is considered as "not pythonic" when I need the index and the value when looping over a list and use:
a = [1,2,3]
for i in range(len(a)):
# i is the idx
# a[i] is the value
but rather it is recommended to use
for idx, val in enumerate(a):
print idx, val
who defines "pythonic" and why is the latter one better? I mean it's not that much better concerning readability, is it!?
Thanks in advance
First of all, the first way is ugly: You either need a separate variable assignment to get the element or use a[i] all the time which could theoretically be an expensive operation. Imagine a being a database cursor: When you iterate it (a.__iter__ being called) the object can safely assume that you are going to iterate over all its items. So all or at least multiple rows could be retrieved at once. When getting the length such an optimization would be stupid though since you surely don't want to retrieve data just because you want the number of items. Also, when retrieving a specific item you cannot assume that other items will be retrieved, too.
Additionally, using enumerate() works with any iterable while range(len()) only works with countable, indexable objects.

Categories

Resources