List of lists to list of arrays


#1

Greetings,

I need urgently to transform this:

[['STORE_ID=2', 'Pasta=3', 'Soup=2'],
['STORE_ID=2',  'Soup=1',  'Fresh Vegetables=3',  'Milk=3',  'Plastic Utensils=2'],
['STORE_ID=2', 'Cheese=2', 'Deodorizers=1', 'Hard Candy=2', 'Jam=2'],
['STORE_ID=2', 'Fresh Vegetables=2']]

into this:


["STORE_ID=1,Pasta=3,Soup=2",

   "STORE_ID=2,Soup=1,Fresh Vegetables=3,Milk=3,Plastic Utensils=2", 

   "STORE_ID=3,Cheese=2,Deodorizers=1,Hard Candy=2,Jam=2",

   "STORE_ID=4,Fresh Vegetables=2"]

I mean, I have to transform a list of lists in a list of arrays. Can you guys help?

Due to the time I have available to accomplish this task and the amount of time I wasted trying to do it for myself, you’re like my last hope.

Thank you.


#2

and what is your definition of help? That we code the solution for you?

I happily help you to get to a solution, but i am not going to code the solution. What you have done so far? Where did you feel you where going in the right direction/getting close?


#3

I don’t want you to do it for me, just want some help. This is somehow kind of frustrating…

I’ve been trying to do it for about four hours and I couldn’t.

What I really need as final result is this:


['Pasta', 'Soup']
['Soup', 'Fresh Vegetables', 'Milk', 'Plastic Utensils']
['Cheese', 'Deodorizers', 'Hard Candy', 'Jam']
['Fresh Vegetables']

I’ve use this code to remove the store_id and the quantities of items. For now I just need the items:

L=["STORE_ID=1,Pasta=3,Soup=2",

   "STORE_ID=2,Soup=1,Fresh Vegetables=3,Milk=3,Plastic Utensils=2", 

   "STORE_ID=3,Cheese=2,Deodorizers=1,Hard Candy=2,Jam=2",

   "STORE_ID=4,Fresh Vegetables=2"]

for a in L:

    fs=a.split(",")

    fss=[x.split("=")[0] for x in fs[1:]]

    print(fss)

wich gives the result I want.

As you see, now I only need to transform my inicial list of lists in a list of strings, so I can transform this second list in the final result I want.

I tried to convert each element of the list of lists in a string, using a for cycle and the comand str().


#4

isn’t easier to do this in one go? without the step in between?

for example:

l = [['STORE_ID=2', 'Pasta=3', 'Soup=2'],
['STORE_ID=2',  'Soup=1',  'Fresh Vegetables=3',  'Milk=3',  'Plastic Utensils=2'],
['STORE_ID=2', 'Cheese=2', 'Deodorizers=1', 'Hard Candy=2', 'Jam=2'],
['STORE_ID=2', 'Fresh Vegetables=2']]

# loop over the list in the outer list
for a in l:
  # loop over products, ignoring store
  for b in a[1:]:
    # get product without quantity
    print(b.split("=")[0])

then all we need to is add an array n the first lop:

for a in l:
    products = [] # products for a specific store

and .append() the products to list in the second for loop, then after the second for loop print the products belonging to store.

once you got this, you can go refactor and use list comprehension


#5

Thanks for the help,

But after the first segment of code we got the items, one per line, regardless of the store ID. And even though I don’t want to display the store ID, I need the items grouped.

The list I gave you was an exemple. the real contais about 70.000 entrances. I can’t .append() all…


#6

Anyway, thanks for the help.


#7

why can’t you append them all? Sounds like a better solution then your string conversion. we could simple do:

for a in l:
  result = []
  for b in a[1:]:
    # get product without quantity
    result.append(b.split("=")[0])
  print(result)

the loop is doing the append work, and the second for loop could surely be refactored to a list comprehension. I don’t see why this is worse then your suggested string conversion


#8

It’s not worst, It’s actually better.

I was thinking in another, more difficult and inneficient, way of doing it. But I guess it’s part of being a noob in python.

Thank you very much for all you help.


#9

then i was confused why you said .append() wouldn’t work, of course we are not going to append all the items manually :wink:

Hope this helps you further


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.