This question already has answers here:
Shuffling a list of objects
(25 answers)
Closed 6 years ago.
A similar question I saw on Stack Overflow dealt with a dict of lists (was a bad title), and when I tried using random.shuffle on my list of dicts per that answer, it made the whole object a None-type object.
I have a list of dictionaries kind of like this:
[
{'a':'1211', 'b':'1111121','c':'23423'},
{'a':'101', 'b':'2319','c':'03431'},
{'a':'3472', 'b':'38297','c':'13048132'}
]
I want to randomly shuffle like this.
[
{'a':'3472', 'b':'38297','c':'13048132'},
{'a':'1211', 'b':'1111121','c':'23423'},
{'a':'101', 'b':'2319','c':'03431'}
]
How can I do this?
random.shuffle should work. The reason I think you thought it was giving you a None-type object is because you were doing something like
x = random.shuffle(...)
but random.shuffle doesn't return anything, it modifies the list in place:
x = [{'a':'3472', 'b':'38297','c':'13048132'},
{'a':'1211', 'b':'1111121','c':'23423'},
{'a':'101', 'b':'2319','c':'03431'}]
random.shuffle(x) # No assignment
print(x)
Related
This question already has answers here:
How can I find same values in a list and group together a new list?
(6 answers)
Closed 2 years ago.
I have a list like so:
[10,10,10,20,20,20,20,30,40,40,40]
I want to split into X amount of lists, where X = how many unique elements there are, in the case above there are 4. So I would want 4 lists like so:
[[10,10,10],[20,20,20,20],[30],[40,40,40]]
Might be a dumb question and there is an easy way to do this but any help is appreciated, language is python3.
itertools.groupby does what you need, except it returns iterators instead of lists. Converting to lists is easy though:
[list(g) for _, g in itertools.groupby(my_list)]
This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 6 years ago.
I'm new to python and in the process of rewriting an old python script and I came across the following line:
some_list = #some list with data
some_variable = [x[0] for x in some_list]
what's x[0]? I don't see x declared previously, is it being created on this line?
what would the value or some_variable be? a list?
update
by 'x' i'm referring to x[0], not the x in the for loop
In this case some_list is a list of sequences, e.g. Lists, tuples, dicts or strings. In your brackets, called a list comprehension, you iterate through some_list. x is the current element of some_list, from which you take the first element x[0] and put it in the new list.
x is the iterating element in your for-loop
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 9 years ago.
I am trying to make a list of lists of about 5000 lists and it keeps messing up.
right now I just do this:
array = [[]]*5000
for line in f2:
a = line.split()
grid = int(a[0])
array[grid].append(a[1])
print Counter(array[0]).most_common(10)
the problem is when I make the counter it does it as if the whole array of lists was actually just one list. Is there something obvious that I am doing wrong?
Thanks
Using [[]]*5000, you are creating 5000 reference to the same list in your outer list. So, if you modify any list, it will modify all of them.
You can get different lists like this:
a = [[] for _ in xrange(5000)]
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 10 years ago.
I need to delete elements from a list whilst iterating over it (I cannot create a new list as it is referenced in code I have no control over).
The best I've come up with is:
last_index = len(somelist) - 1
for (index,item) in enumerate(reversed(somelist)):
if somecondition(item):
del somelist[last_index - index]
Are there better alternatives? I've seen this post and this one too, but none of the solutions provided are as efficient or as concise (IMHO).
You can use a list comprehension + slice assignment, which is certainly more concise -- I don't know about efficiency (although I would expect it to do better in that regard as well since you don't need to keep shifting elements in the list over every time you delete an item ...)
somelist[:] = [ x for x in somelist if not somecondition(x) ]
This question already has answers here:
Closed 10 years ago.
I have a fairly simple question (I think).
I have a list of lists in python, and the elements are strings. I wish to have a single list, with elements that are floats.
For example:
lst= [['0.0375'], ['-0.1652'], ['0.1841'], ['-0.0304'], ['0.0211'], ['0.1580'], ['-0.0252'], ['-0.0080'], ['-0.0915'], ['0.1208']]
And I need to have something like:
lst= [0.0375, -0.1652, 0.1841, -0.0304, 0.0211, 0.1580, -0.0252, -0.0080, -0.0915, 0.1208]
[float(x) for (x,) in your_list]