tlqn
tlqn

Reputation: 399

Separate each value in the list Python

I currently have this transaction list. Each row is 1 transaction that contains the bought products. However, with the current list it understood the whole row as 1 (and doesn't recognize the products separately):

transaction_list
[['8HN,M7E,K09'],
 ['ANV'],
 ['UKQ,8HN,SOE,Z1G']]

What I really would like to do is to recognize each product of the transaction as a separate value. So the first transaction has product '8HN', 'M7E', and 'K09' like this:

transaction_list
[['8HN','M7E','K09'],
 ['ANV'],
 ['UKQ','8HN','SOE','Z1G']]

I'm actually not sure what would be the best way to approach this. I tried to use split() but it said 'list' object has no attribute 'split'. Could anyone please give me a suggestion?

Upvotes: 0

Views: 117

Answers (3)

Valdi_Bo
Valdi_Bo

Reputation: 30991

Initially you marked this question also with pandas tag, so I assume that you want not plain pythonic list, but rather a Series or DataFrame.

The way you created your transaction_list is a bit unclear, so I assumed that it was created something like:

transaction_list = pd.Series([['8HN,M7E,K09'], ['ANV'], ['UKQ,8HN,SOE,Z1G']])

so it is:

  • a Series,
  • with each element containing a list,
  • with a single string.

To convert it to a DataFrame, splitting each element on a comma, you can run:

result = transaction_list.apply(lambda tt: pd.Series(tt[0].split(','))).fillna('')

getting:

     0    1    2    3
0  8HN  M7E  K09     
1  ANV               
2  UKQ  8HN  SOE  Z1G

Note that I added .fillna(''), otherwise non-existing element would have been filled with NaN.

Upvotes: 0

DavideBrex
DavideBrex

Reputation: 2414

You got an error because you were trying to split on a list instead of the string within the list: [i.split(",")[0] for i in transaction_list]. Here i is a list, and you can't apply the function split on it. So first you do i[0] and then split on it. Like this:

l =[['8HN,M7E,K09'],
 ['ANV'],
 ['UKQ,8HN,SOE,Z1G']]

[ a[0].split(",") for a in l]

Output:

[['8HN', 'M7E', 'K09'], ['ANV'], ['UKQ', '8HN', 'SOE', 'Z1G']]

Upvotes: 1

ComedicChimera
ComedicChimera

Reputation: 476

This is a great situation to employ a list comprehension. The code is written like so:

[lst[0].split(',') for lst in transaction_list]

In short, we are going to first comprehend each list in our transaction list and then access the first element (where the string you want to split is located) and split it into a list. The resulting split lists then become the elements of list originally being comprehended.

If you are unfamiliar with list comprehensions: check out https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

I know this answer is redundant, but it should be a bit more explanatory.

Upvotes: 0

Related Questions