1

I have a string which I read from a .csv file:

shopping_lists = 'apple pie,corn bread,red wine\nready soups,milk,coffee\nwhole milk' 

I want to get a list of sets like:

[{'apple pie','corn bread','red wine'},{'ready soups', 'milk', 'coffee'},{'whole milk'}]

I tried this code:

s = shopping_lists.split()
L = [set(i.split(',')) for i in s]

But I got the wrong result:

[{'apple'}, {'corn', 'pie'}, {'bread', 'red'}, {'wine'}, {'ready'}, {'coffee', 'soups', 'milk'}, {'whole'}, {'milk'}]

What went wrong, and how can I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Shi Wang
  • 19
  • 2
  • 1
    In your output, when you use braces like `{}`, is that supposed to denote a set? Thanks for clarifying and welcome to SO! – ggorlen Sep 05 '19 at 04:17
  • `['apple pie,corn bread,red wine\ready soups,milk,coffee']` is not a list of strings, but a single string that includes commas in a list. Is that excatly how the real data looks like? – Aryerez Sep 05 '19 at 04:20
  • It looks like what you are actually attempting to produce is a `list` of `sets` -- (e.g., if you put `{}` around values like this, `{'1st value', '2nd value', '3rd value'}` you are creating a `set`, not a `list`. It could be important to be aware of this because `sets` do not allow duplicate values. `{'1st value', '2nd value', '1st value'}` will create a `set` that looks like `{'1st value', '2nd value'}` -- the extra `'1st value'` will be removed. – MurrayW Sep 05 '19 at 04:22
  • Does "\" means "\n"? Is it `apple pie,corn bread,red wine\nready soups,milk,coffee`? – Waket Zheng Sep 05 '19 at 06:16
  • there seems to be some bad data post, you can't get your output from shopping_lists – kederrac Sep 05 '19 at 08:14
  • Thanks all. Sorry that I did not make it clear. I got a string from read a csv.file. String='apple pie,corn bread,red wine\nready soups,milk,coffee\nwhole milk' And I would like to get a list of sets which is L=[{'apple pie','corn bread','red wine'},{'ready soups', 'milk', 'coffee'},{whole milk}] – Shi Wang Sep 06 '19 at 22:23
  • This question was a complete mess, and it's baffling to me that it got three attempts at an answer as asked. The example made no sense at all - it showed trying to call `.split` on a list (rather than using `.split` to *get* the list), then it described getting output that was completely different from the actual output after fixing that typo. Plus it was titled asking for something different from what the question actually asked for. In the end, the problem boils down to *another* typo: splitting the string on all whitespace instead of specifically newlines. – Karl Knechtel Aug 07 '22 at 02:46
  • But *even with all of that said*, the entire approach to the problem is wrong - the standard library `csv` module should be used to process the file instead. – Karl Knechtel Aug 07 '22 at 02:47

3 Answers3

1

Sometimes it is easier to break it down to a for loop; Also, I think you may have a typo in your original list.

Assuming a typo:

shopping_lists=['apple pie,corn bread,red wine','ready soups,milk,coffee']
L = []
for row in shopping_lists:
    L.append(set(row.split(',')))
print(L)

If no typo:

shopping_lists='apple pie,corn bread,red wine/ready soups,milk,coffee'
L = []
shopping_lists=shopping_lists.split('/')
for row in shopping_lists:
    L.append(set(row.split(',')))
print(L)

Both yield this result;

[{'red wine', 'corn bread', 'apple pie'}, {'milk', 'coffee', 'ready soups'}]

And if you really meant a list of lists, not a list of sets ...

shopping_lists=['apple pie,corn bread,red wine','ready soups,milk,coffee']
L = []
for row in shopping_lists:
    L.append(row.split(','))
print(L)

yields;

[['apple pie', 'corn bread', 'red wine'], ['ready soups', 'milk', 'coffee']]

SteveJ
  • 3,034
  • 2
  • 27
  • 47
-1

Use splitlines instead of split()

Python 3.8.0b4 (default, Sep  5 2019, 14:10:43)
[GCC 8.2.1 20180831] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> s = 'apple pie,corn bread,red wine\nready soups,milk,coffee'
>>> [set(i.split(',')) for i in s.splitlines()]
[{'apple pie', 'corn bread', 'red wine'}, {'ready soups', 'milk', 'coffee'}]
Waket Zheng
  • 5,065
  • 2
  • 17
  • 30
-1

you can use:

shopping_lists=['apple pie,corn bread,red wine\ready soups,milk,coffee']

[set(item.strip("'").split(',')) for e  in shopping_lists for item in ('%r' % e).split('\\')]

output:

[{'apple pie', 'corn bread', 'red wine'}, {'coffee', 'milk', 'ready soups'}]

you have a backslash which is the delimiter for your 2 sets, so first, you have to split by backslash and then you have to split by comma

kederrac
  • 16,819
  • 6
  • 32
  • 55