Python list of lists to list [duplicate] - python

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]

Related

Pythonic way to group a list of lists? [duplicate]

This question already has answers here:
Transpose list of lists
(14 answers)
Closed 12 months ago.
So lets say I have a hypothetical list of lists of file names in Python defined like so:
l = [["user1/stats1.csv", "user1/stats2.csv", "user1/stats3.csv"],
["user2/stats1.csv", "user2/stats2.csv", "user2/stats3.csv"]]
What would be the most pythonic way to group it by the number in statsN.csv such that the list would look like:
l = [["user1/stats1.csv", "user2/stats1.csv"],
["user1/stats2.csv", "user2/stats2.csv"],
["user1/stats3.csv", "user2/stats3.csv"],
For reference, the original list was obtained by using glob with the * wild card a la glob.glob("user1/stats*.csv") and glob.glob("user2/stats*.csv")
You could unpack the sublists and zip:
out = list(map(list, zip(*l)))
Output:
[['user1/stats1.csv', 'user2/stats1.csv'],
['user1/stats2.csv', 'user2/stats2.csv'],
['user1/stats3.csv', 'user2/stats3.csv']]
If you're using numpy, you can simply transpose:
>>> np.array(l).T.tolist()
[['user1/stats1.csv', 'user2/stats1.csv'],
['user1/stats2.csv', 'user2/stats2.csv'],
['user1/stats3.csv', 'user2/stats3.csv']]

Split list element into lists in python [duplicate]

This question already has answers here:
Apply function to each element of a list
(4 answers)
Python Splitting Array of Strings
(6 answers)
Split each string in list and store in multiple arrays
(4 answers)
Closed 1 year ago.
I have a list
list = ['a:b:c:d', 'e:f:g:h', 'i:j:k:l']
What I'm trying to do is make a list of each of it's elements. Something like this:
listelement[0] = list[0].split(':')
print(listelement[0][1])
output: b
I can't seem to figure out how to create something that works similarly to this.
You can try list comprehension to do what you want.
new_list = [element.split(':') for element in list].
Also I would advise against the use of list as a name, since it's a reserved word.

How can I split a list into smaller lists [duplicate]

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)]

Making a multidimensional list from a one dimensional list [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 5 years ago.
I know the title is a bit confusing but I couldn't think of a better way of describing it. This is python code.
Say I have a list a=["blue","yellow", "red", "green"]. How would I split it so it looks like this: a=[["blue","yellow"],["red","green"]]?
Just make appropriate slices and put them in a list of their own:
a = [a[:2], a[2:]]
If you don't know the length of the list beforehand, you can use a comprehension:
chunksize = 2
a = [a[i:i+chunksize] for i in range(0, len(a), chunksize)]

Python iterate through a list of tuples [duplicate]

This question already has answers here:
Find an element in a list of tuples
(10 answers)
How to check if all elements of a list match a condition?
(5 answers)
Closed 5 years ago.
I am new in Python and I was curious if I can do something like this:
Let's say that I have a list of tuples and one variable like this:
list = [(123,"a"),(125,"b")]
variable = (123,"c")
Is it possible to search for the first element of the variable in the list like this?
if variable[0] in list[0]:

Categories