Explain why unpack() returns different results in Lua
Change
primes[idx] = n
to
primes[#primes+1] = n
The reason is that idx is not sequential as not every number is a prime.
Unzipping and the * operator
When used like this, the * (asterisk, also know in some circles as the "splat" operator) is a signal to unpack arguments from a list. See http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists for a more complete definition with examples.
Why does x,y = zip(*zip(a,b)) work in Python?
The asterisk in Python is documented in the Python tutorial, under Unpacking Argument Lists.
Multiple assignments via walrus := operator?
Iterable packing and unpacking is one difference between =
and :=
, with only the former supporting them. As found in PEP-572:
# Equivalent needs extra parentheses
loc = x, y # Use (loc := (x, y))
info = name, phone, *rest # Use (info := (name, phone, *rest))
# No equivalent
px, py, pz = position
name, phone, email, *other_info = contact
Visually Representing X and Y Values
Perhaps you could try something like this (also see):
import numpy as np:
xs=[]; ys=[]
for x,y in xy_list:
xs.append(x)
ys.append(y)
xs=np.asarray(xs)
ys=np.asarray(ys)
plot(xs,ys,'ro')
Maybe not the most elegant solution, but it should work. Cheers, Trond
Making a list comprehension for a dict within a list within a list
Animals = [[d['animal'] for d in sub] for sub in MyList]
Colors = [[d['color'] for d in sub] for sub in MyList]
Gives the desired result:
[['A', 'B'], ['C', 'D']]
[['blue', 'red'], ['blue', 'Y']] # No second 'red'.
What I have done here is take each sub-list, then each dictionary, and then access the correct key.
Python: TypeError: argument after * must be a sequence
You need to add a comma - ,
- after your variable s
. Sending just s to args=() is trying to unpack a number of arguments instead of sending just that single arguement.
So you'd have threading.Thread(target=send, args=(s,)).start()
Also the splat - *
- operator might be useful in this question explaining it's usage and unzipping arguments in general
How to unpack tuples in nested list?
>>> a = [ [(2, 3, 5), (3, 4, 6), (4, 5, 7), (1, 1, 1), (1, 2, 3)],
... [(4, 9, 2), (8, 8, 0), (3, 5, 1), (2, 6, 8), (2, 4, 8)],
... [(8, 7, 5), (2, 5, 1), (9, 2, 2), (4, 5, 1), (0, 1, 9)]]
>>> zip(*(zip(*x) for x in a))
[((2, 3, 4, 1, 1), (4, 8, 3, 2, 2), (8, 2, 9, 4, 0)), ((3, 4, 5, 1, 2), (9, 8, 5, 6, 4), (7, 5, 2, 5, 1)), ((5, 6, 7, 1, 3), (2, 0, 1, 8, 8), (5, 1, 2, 1, 9))]
>>> for row in _:
... print row
...
((2, 3, 4, 1, 1), (4, 8, 3, 2, 2), (8, 2, 9, 4, 0))
((3, 4, 5, 1, 2), (9, 8, 5, 6, 4), (7, 5, 2, 5, 1))
((5, 6, 7, 1, 3), (2, 0, 1, 8, 8), (5, 1, 2, 1, 9))
If it must be lists
>>> map(list, zip(*(map(list, zip(*x)) for x in a)))
[[[2, 3, 4, 1, 1], [4, 8, 3, 2, 2], [8, 2, 9, 4, 0]], [[3, 4, 5, 1, 2], [9, 8, 5, 6, 4], [7, 5, 2, 5, 1]], [[5, 6, 7, 1, 3], [2, 0, 1, 8, 8], [5, 1, 2, 1, 9]]]
>>> for row in _:
... print row
...
[[2, 3, 4, 1, 1], [4, 8, 3, 2, 2], [8, 2, 9, 4, 0]]
[[3, 4, 5, 1, 2], [9, 8, 5, 6, 4], [7, 5, 2, 5, 1]]
[[5, 6, 7, 1, 3], [2, 0, 1, 8, 8], [5, 1, 2, 1, 9]]
Related Topics
How to Install Pip for Python 2.6
Same Output in Different Workers in Multiprocessing
Python: My Function Returns "None" After It Does What I Want It To
Simple Animation Using Tkinter
Calling Functions from a Tkinter Frame to Another
Why Doesn't 2._Add_(3) Work in Python
Why Can't I Repeat the 'For' Loop for CSV.Reader
String Comparison Doesn't Seem to Work for Lines Read from a File
How Can a Recursive Regexp Be Implemented in Python
How to Get All Combinations of Length N in Python
Pygame 2 Dimensional Movement of an Enemy Towards the Player, How to Calculate X and Y Velocity
Pandas To_Datetime Parsing Wrong Year
Managing Dynamic Plotting in Matplotlib Animation Module