I have a list of tuples with one element as NaN:
l = [('a', 7.0), ('b', float('nan'))]
And I want to find the index of tuple ('b', float('nan')) in the above list.
l.index(('b', float('nan')) is unable to find the element in the list even though its index is 1. It is raising ValueError exception as:
>>> l.index(('b', float('nan'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: ('b', nan) is not in list
Most likely this is due to the fact that each float('nan') is an independent NaN object, meaning that the two tuples are different objects as well.
How do I solve this problem in general?
float('nan') == float('nan') returns False because it is designed to not to match with itself. That's why list.index() function is unable to find a match for NaN value and is raising ValueError exception.
Please read Why is NaN not equal to NaN? to know more about this behaviour.
Below is a custom function check_nan_match() to check whether passed objects are having same value or not. This function will be able to match for NaN objects too based on the above property i.e. NaNs return False when matched with itself.
# Function too check passed values are match, including `NaN`
def check_nan_match(a, b):
return (b != b and a != a) or a == b
# ^ ^ `NaN` property to return False when matched with itself
To get the index of tuple in the list containing NaN, here I am creating another custom function as get_nan_index. This function accepts my_list and my_tuple as param, iterates over the my_list to get the index of my_tuple. To check for the equality, I am using previously created check_nan_match function which is capable to match NaN values too.
# Get index from list of tuple , when tuple is passed
def get_nan_index(my_list, my_tuple):
for i, t in enumerate(my_list):
if all(check_nan_match(x, y) for x, y in zip(t, my_tuple)):
return i
else:
raise ValueError # Raise `ValueError` exception in case of no match.
# Similar to `list.index(...)` function
Sample run:
# check for tuple with `NaN`
>>> get_nan_index([('a', 7.0), ('b', float('nan'))], ('b', float('nan')))
1
# check for tuple without `NaN`
>>> get_nan_index([('a', 1), ('b', 2)], ('b', 2))
1
# `ValueError` exception if no match
>>> get_nan_index([('a', 7.0), ('b', 3)], ('b', float('nan')))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in get_nan_index
ValueError
Related
Recently I noticed that when I pass to the function argument which is tuple value from dictionary it has trailing comma at the end.
Bellow is the simple code example of my issue.
def myfun(*args):
print(f'args={args}')
x, y = args
print(f'x={x}, y={y}')
myfun(1, 2) # passing arguments this way works fine
arg_dict = {0: (1, 2), 1: (2, 3)}
print(f'arg_dict[0]={arg_dict[0]}') # when I print dictionary value it seems quite OK.
myfun(arg_dict[0]) # passed dictionary value has trailing comma.
Here is the output:
args=(1, 2)
x=1, y=2
arg_dict[0]=(1, 2)
args=((1, 2),)
Traceback (most recent call last):
File "c:\Users\name\Documents\pname\test.py", line 28, in <module>
myfun(arg_dict[0])
File "c:\Users\name\Documents\pname\test.py", line 21, in myfun
x, y = args
ValueError: not enough values to unpack (expected 2, got 1)
I am wondering why python interpreter decides to pack tuple from dictionary like this?
I am using python3.6.
You pass tuple as a first argument.
You need to unpack values:
myfun(*arg_dict[0])
i would like to know how to convert a generator with only one element into list.For example if f is a generator with one element,list(f) will raise "not iterable".how to solve this problem ?thanks in advance!
nx.adamic_adar_index(g, (0, 1))
list(nx.adamic_adar_index(g, (0, 1)))
this will raise TypeError: 'int' object is not iterable,the following is OK
nx.adamic_adar_index(g, [(0, 1),(2,3)])
list(nx.adamic_adar_index(g, [(0, 1),(2,3)]))
the result is [(0, 1, 2.3878841007006875), (2, 3, 0.9282626109897467)]
If I understand correctly, this: [(0, 1),(2,3)] is a list of two elements (each being a tuple of two elements) but (0, 1) is a single element, could it be that you need to be a list of a single element, like [(0, 1)]?
I'd guess that:
list(nx.adamic_adar_index(g, [(0, 1)]))
will do what you want.
Could this be it?
The generator must be evaluated to be listed. You can't list the generator itself.
>>> def g():
... yield 1
...
>>> list(g())
[1]
>>> list(g)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: 'function' object is not iterable
I have a list
try_list = ["[('aaaa', 34), ('bbbb', 1), ('cccc', 1)]", "[('dddd', 4), ('eeee', 1)]"]
Each entry within "" is an element of a list.
When I say
for i in try_list:
print i
I get the individual elements,
[('aaaa', 34), ('bbbb', 1), ('cccc', 1)]
[('dddd', 4), ('eeee', 1)]
But if I try to pass each individual to a function (say func) as
for i in try_list:
item = func(i)
I get an error.
If I test the function func with each element, one by one, it works fine so func is fine. So the error is in passing the lists as arguments, one at a time. What is the correct way of doing this?
Here is func:
def func(my_list):
ans = []
for i,name in enumerate(my_list):
print name[0],name[1]
ans.append(name)
return ans
If I say
func([('aaaa', 34), ('bbbb', 1), ('cccc', 1)])
I get the correct output
aaaa 34
bbbb 1
cccc 1
[('aaaa', 34), ('bbbb', 1), ('cccc', 1)]
But if I say
for i in try_list:
item = func(i)
I get the error
Traceback (most recent call last):
File "", line 2, in
File "", line 4, in func
IndexError: string index out of range
Quite (un)fortunately, the return value of __repr__ for the list [('aaaa', 34)] is the same as the string "[('aaaa', 34)]".
That's why you have print outputting something that looks like a list. But what you have is not a list of lists but a list of strings.
>>> print("[('aaaa', 34)]")
[('aaaa', 34)]
>>>
>>> print([('aaaa', 34)])
[('aaaa', 34)]
You probably want to convert your string into a list before passing as parameter to your function.
You can have a look at ast.literal_eval if you need to safely make the conversion from the string you have into a list.
I started learning python today and found this very nice code visualization tool pythontutor.com, the problem is that I still don't quite get some of the syntax on the example code.
def listSum(numbers):
if not numbers:
return 0
else:
(f, rest) = numbers
return f + listSum(rest)
myList = (1, (2, (3, None)))
total = listSum(myList)
What does (f, rest) = numbers means?
It's tuple unpacking.
There needs to be 2 items in the tuple when used in this way. More or less will result in an exception, as shown below.
>>> numbers = (1, 2, 3, 4, 5)
>>> (f, rest) = numbers
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> numbers = (1, 2)
>>> (f, rest) = numbers
>>> print f
1
>>> print rest
2
>>> numbers = (1)
>>> (f, rest) = numbers
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> numbers = (1,)
>>> (f, rest) = numbers
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
Note that (1) and (1, )are syntactically different, with only the latter being a tuple.
See the Python Doc on Tuples and Sequences for more details.
(f, rest) = numbers
unpacks the tuple. That is, it takes the two values stored in numbers and stores them in f and rest, respectively. Note that the number of variables you unpack into must be the same as the number of values in the tuple, or else an exception will be thrown.
Tupple is a data structure in which you can store multiple items under one name.
Lets say that we have a tupple(t) with two items.
Then t[0] = first_item and t[1] = sencond_item
Another way of accessing the tupple item is:
(f, rest) = numbers
In this syntax numbers (tupple) must have 2 items only otherwise it is an exception
f = numbers[0]
rest = numbers[1]
I am trying to get a list of list of tuples : something like [ [(1,0),(2,0),(3,0)],[(1,1),(2,1),(3,1)....]]
I used this statement
set([(a,b)for a in range(3)]for b in range(3))
But it gives me an error
TypeError: unhashable type: 'list'
I have 2 questions for the Python Guru's:
a) When I look at the Python definition of Hashable -
"An object is hashable if it has a hash value which never changes during its lifetime (it needs a hash() method)"
when I used dir function on the expression above
dir([(a,b)for a in range(3)]for b in range(3))
it seems to say the __hash__ is there. So, why do I get the error?
I was able to get [[(0, 0), (1, 0), (2, 0)], [(0, 1), (1, 1), (2, 1)], [(0, 2), (1, 2), (2, 2)]]
by using the list command :
list(list((a,b) for a in range(3)) for bin range(3))
b)list and set both takes Iterable as parameter. How come one works(list) and another doesn't (set)?
You are creating a set via set(...) call, and set needs hashable items. You can't have set of lists. Because list's arent hashable.
[[(a,b) for a in range(3)] for b in range(3)] is a list. It's not a hashable type. The __hash__ you saw in dir(...) isn't a method, it's just None.
A list comprehension returns a list, you don't need to explicitly use list there, just use:
>>> [[(a,b) for a in range(3)] for b in range(3)]
[[(0, 0), (1, 0), (2, 0)], [(0, 1), (1, 1), (2, 1)], [(0, 2), (1, 2), (2, 2)]]
Try those:
>>> a = {1, 2, 3}
>>> b= [1, 2, 3]
>>> type(a)
<class 'set'>
>>> type(b)
<class 'list'>
>>> {1, 2, []}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> print([].__hash__)
None
>>> [[],[],[]] #list of lists
[[], [], []]
>>> {[], [], []} #set of lists
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
TLDR:
- You can't hash a list, a set, nor a dict to put that into sets
- You can hash a tuple to put it into a set.
Example:
>>> {1, 2, [3, 4]}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> {1, 2, (3, 4)}
set([1, 2, (3, 4)])
Note that hashing is somehow recursive and the above holds true for nested items:
>>> {1, 2, 3, (4, [2, 3])}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Dict keys also are hashable, so the above holds for dict keys too.
A list is unhashable because its contents can change over its lifetime. You can update an item contained in the list at any time.
A list doesn't use a hash for indexing, so it isn't restricted to hashable items.
The real reason because set does not work is the fact, that it uses the hash function to distinguish different values. This means that sets only allows hashable objects. Why a list is not hashable is already pointed out.
... and so you should do something like this:
set(tuple ((a,b) for a in range(3)) for b in range(3))
... and if needed convert back to list
You'll find that instances of list do not provide a __hash__ --rather, that attribute of each list is actually None (try print [].__hash__). Thus, list is unhashable.
The reason your code works with list and not set is because set constructs a single set of items without duplicates, whereas a list can contain arbitrary data.