I have tried this statement
N,T,M = list(map(int,input().split()))
and python is showing me
N,T,M = list(map(int,input().split()))
ValueError: not enough values to unpack (expected 3, got 1)
What to do?
It seems you're receiving this error because you are trying to assign three distinct values (N, T, and M) even though your list only has one value:
By running this in the Python3 terminal, we see the same error you reported:
>>>N,T,M=list([1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 1)
With 2 items in the list, the error message changes:
>>> N,T,M=list([1, 2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 2)
If you have 3 items in your list, Python will assign one item to each of your variables:
>>>N,T,M=list([1, 2, 3])
# This works and no error is received
Then, with 4 items in the list, Python complains:
>>> N,T,M=list([1, 2, 3, 4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 3)
Essentially, you need to ensure that the number of items on the right side of your expression will be three in order to instantiate three separate variables.
Related
Hi I am trying to make edits to every element in the array to return an array of edited(sliced) elements. However, I am getting the error below. Any help is appreciated.
Traceback
>>> p=Playlist.objects.get(id=3)
>>> l=p.song.values_list('link', flat=True)
>>> print(l)
<QuerySet ['https://www.youtube.com/watch?v=_DqmVMlJzqA', 'https://www.youtube.com/watch?v=_DqmVMlJzqA', 'https://www.youtube.com/watch?v=_DqmVMlJzqA', 'https://www.youtube.com/watch?v=k6PiQr-lQY4', 'https://www.youtube.com/watch?v=gqOEoUR5RHg']>
>>> print([l[i][17:] if l[i][0:17] == 'https://youtu.be/' else l[i][32:] for i in l])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<console>", line 1, in <listcomp>
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 278, in __getitem__
raise TypeError
TypeError
this is mi first attempt with Python. I'm trying to use python with Apache Spark.
This is what i want to do:
l = sc.textFile("/user/cloudera/dataset.txt")
l = l.map(lambda x: map(int, x))
then i use cartesian function to obtain all possible combination of elements
lc = l.cartesian(l)
now for every couple i apply a function:
output = lc.map(lambda x: str(x[0]) + ";" + str(x[1]) + ";" + str(cosineSim(x[0], x[1])))`
my objective is to obtain strings like:
element1; element1; similarity
element1; element2; similarity
...
and so on..
when i call output.first() this is my output:
[45, 12, 7, 2, 2, 2, 2, 4, 7];[45, 12, 7, 2, 2, 2, 2, 4, 7];1.0
this is a string, indeed if i a do:
s = output.first()
type(s)
<type 'str'>
but if i execute output.collect() or output.saveAsTextFile(path) i have this error:
15/02/13 06:06:18 WARN TaskSetManager: Lost task 1.0 in stage 61.0 (TID 183, 10.39.127.148): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
File "/usr/lib/spark/python/pyspark/worker.py", line 107, in main
process()
File "/usr/lib/spark/python/pyspark/worker.py", line 98, in process
serializer.dump_stream(func(split_index, iterator), outfile)
File "/usr/lib/spark/python/pyspark/serializers.py", line 227, in dump_stream
vs = list(itertools.islice(iterator, batch))
File "<stdin>", line 2, in <lambda>
ValueError: invalid literal for int() with base 10: ''
what's wrong?
I think there must be an error in this formula:
l = l.map(lambda x: map(int, x))
Can you check that the l RDD always has values (no '')? If it doesn't, you get a typical Python error:
> In [32]: int('')
--------------------------------------------------------------------------- ValueError Traceback (most recent call
last) in ()
----> 1 int('')
ValueError: invalid literal for int() with base 10: ''
Moving forward, keep in mind that map are lazy evaluations which means that they are not computed until the next action (collect and save are actions) is instructed.
I can do
>>> s = {1}
>>> type(s)
<class 'set'>
but
>>> s = set(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
What is the difference?
The difference is that the set() constructor takes an iterable. A single number is not an iterable.
s = set((1,))
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]
So here I have x1vals:
>>> x1Vals
[-0.33042515829906227, -0.1085082739900165, 0.93708611747433213, -0.19289496973017362, -0.94365384912207761, 0.43385903975568652, -0.46061140566051262, 0.82767432358782367, -0.24257307936591843, -0.1182761514447952, -0.29794617763330011, -0.87410892638408, -0.34732294121174467, 0.40646145339571249, -0.64082861589870865, -0.45680189916940073, 0.4688889876175073, -0.89399689430691298, 0.53549621114138612]
And here is the list of x1Vals indices that I want to select
>>> np.where(np.dot(XValsOnly,newweights) > 0)
>>>(array([ 1, 2, 4, 5, 6, 8, 9, 13, 15, 16]),)
But when I try to get the values of x1Vals the Matlab way, I get this error:
>>> x1Vals[np.where(np.dot(XValsOnly,newweights) > 0)]
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
x1Vals[np.where(np.dot(XValsOnly,newweights) > 0)]
TypeError: list indices must be integers, not tuple
>>> np.where(np.dot(XValsOnly,newweights) > 0)
Is there a way around this?
The problem is that your x1Vals is a list object, which does not support fancy indexing. You just have to build an array out of it:
x1Vals = np.array(x1Vals)
and your approach will work.
A faster approach would be to use np.take:
np.take(x1Vals, np.where(np.dot(XValsOnly,newweights) > 0))