List of string to element [duplicate] - python

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Python Convert String List to List
(3 answers)
Closed 5 years ago.
I have a list of strings like this:
L=['[1,2,3]','[3,4,5,6]','[9,8,7]']
I want to make a list of list elements like this:
Y=[[1,2,3],[3,4,5,6],[9,8,7]]
How can I do it in python?

You can use ast.literal_eval to evaluate each string as a python literal within a list comprehension
>>> from ast import literal_eval
>>> [literal_eval(i) for i in L]
[[1, 2, 3], [3, 4, 5, 6], [9, 8, 7]]

Every element of your array is a valid JSON so you could do
import json
Y = [json.loads(l) for l in L]

You could also do this manually:
>>> L=['[1,2,3]','[3,4,5,6]','[9,8,7]']
>>> [[int(x) for x in l[1:-1].split(',')] for l in L]
[[1, 2, 3], [3, 4, 5, 6], [9, 8, 7]]
Or without split(), as suggested by #Moinuddin Quadri in the comments:
>>> [[int(x) for x in l[1:-1:2]] for l in L]
[[1, 2, 3], [3, 4, 5, 6], [9, 8, 7]]

Related

How do I consolidate multiple lists (nested list) into a single list? [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed last month.
for example, I get
[[], [2, 3, 4, 5]]
[[1], [3, 4, 5]]
[[1, 2], [4, 5]]
[[1, 2, 3], [5]]
[[1, 2, 3, 4], []]
And I want to convert first list into [2,3,4,5], next [1,3,4,5], et cetera.
I try to mutiply them all together but of course a list cannot mutiply a interger.
You can use itertools.chain:
>>> from itertools import chain
>>> list(chain.from_iterable([[1, 2], [4, 5]]))
[1, 2, 4, 5]

Identifying unique index numbers from a list [duplicate]

This question already has answers here:
Get unique values in List of Lists
(6 answers)
Find unique rows in numpy.array
(20 answers)
Count Distinct Values in a List of Lists
(5 answers)
Closed 6 months ago.
I have a list A containing an array with indices. I want to print all the unique index numbers by scanning each [i,j]. In [0,3], i=0,j=3. I present the expected output.
import numpy as np
A=[np.array([[[0, 1],
[0, 3],
[1, 3],
[3, 4],
[3, 6],
[4, 5],
[4, 7],
[5, 7],
[6, 4]]])]
The expected output is
A=[0,1,3,4,5,6,7]
numpy has this very cool function np.unique.
Simply to get the output A=[0,1,3,4,5,6,7]
B = np.unique(A[0])
Output :
[0 1 3 4 5 6 7]
A=[[0, 1],[0, 3],[1, 3],[3, 4],[3, 6],[4, 5],[4, 7],[5, 7],[6, 4]]
K = []
for _ in range(len(A)):
K.extend(A[_])
print(set(K))
OUTPUT:
{0, 1, 3, 4, 5, 6, 7}
A is basically a list and within list you make an array.
Do this:
def unique(A):
x = np.array(A)
print(np.unique(x))
unique(A)
if A is an array we can simply do this:
np.unique(A)

How to turn lists of lists into integers [duplicate]

This question already has answers here:
List of list, converting all strings to int, Python 3
(4 answers)
Closed 11 months ago.
I know how to transform a single list of strings into integers...
But how do I transform lists of lists (strings) from into integers
I want this list:
['[1,', '3]', '[3,', '4]', '[5,', '8]', '[6,', '10]']
become a list of integers:
[1, 3], [3, 4], [5, 8], [6, 10]
The main problem with your example data, is that there's no commas between lists, you could do something like this to parse the different lists:
>>> parsed = re.findall(r'\[.+?\]', ''.join(data))
['[1,3]', '[3,4]', '[5,8]', '[6,10]']
>>> ast.literal_eval(', '.join(parsed))
([1, 3], [3, 4], [5, 8], [6, 10])

How do you increase the size of lists inside a list [duplicate]

This question already has answers here:
Append value to each sublist in a list
(3 answers)
Closed 2 years ago.
I'm trying to figure out how to increase the size of the lists inside a list of lists. For example [[1,2,3,4], [1,2,3,4],[1,2,3,4]] and want to be able to add 1 more onto it so it is [[1,2,3,4,5], [1,2,3,4,5],[1,2,3,4,5]]. The numbers are just to show i want them to now have a new element in each.
You can use a for-loop and .append().
>>> L = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
>>> for sublist in L:
... sublist.append(5)
...
>>> L
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]

Finding the intersection of nested lists in Python? [duplicate]

This question already has answers here:
Python -Intersection of multiple lists?
(6 answers)
Closed 4 years ago.
def query_RR(postings, qtext):
words = tokenize(qtext)
allpostings = [postings[w] for w in words]
for a in allpostings:
print a.keys()
And this was the result of the query [0, 2, 3, 4, 6] [1, 4, 5] [0, 2, 4] [4, 5]
The query is taking a user inputted term ('qtext'), tokenizing and generating a postings list for each token.
The posting list is a list of nested dictionaries (e.g. [{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]. I am attempting to find the intersection for these nested dictionaries using the keys
Assuming the order does not matter, you could use set.intersection():
>>> lst = [[0, 2, 3, 4, 6], [1, 4, 5], [0, 2, 4], [4, 5]]
>>> set.intersection(*map(set,lst))
{4}
>>> set(lst[0]).intersection(*lst[1:])
{4}

Categories