-1

I want to use the items in my list to create a new list with the name of the list being item from the first list.

Cs = ['a', 'b', 'c']

List in separate file:

import cs
from os import path

for i in cs.CsNYSE: 
    destination = 'C:\\Users\\mike\\Desktop\\SMP\\S\\' + i + '.txt'
    f = open(destination)
    lines = f.readlines()
    linNum = len(lines)
    X = 1
    while X <= linnum:
        Name = i
        Y = name + []
        Y.append(lines[x])
    X += 2

I receive a syntax error stating that I cannot append str with append. I am pulling names from a very long list, trying to create a list with those names to be run through several algorithms. Can someone point me where I went wrong. Still new to python programming. So, a little explaining would go a long way and be very appreciated. Thanks in advance.

CristiFati
  • 38,250
  • 9
  • 50
  • 87
Michael Smith
  • 77
  • 1
  • 10
  • use `+` operator if you want to "append" to strings. `.append` is for lists only. Also `Y=name+[]` makes no sense – letsc Nov 23 '15 at 21:56
  • your question is unclear. Please read [this](http://stackoverflow.com/help/mcve) – Pynchia Nov 23 '15 at 21:58
  • explaining will take a while. I can tell you to learn: 1) how to loop on a sequence without using indexes. 2) how to manage and read from/loop on files – Pynchia Nov 23 '15 at 22:01
  • ..also you mixed up lower and uppercase in your variables. – Klaus D. Nov 23 '15 at 22:01
  • I think I finally understood what the OP wants. He wants to use the elements in the list `Cs` to create lists named after those elements aka `a=[], b=[], c=[]` . If thats what you want. Read through this - http://stackoverflow.com/questions/19122345/to-convert-string-to-variable-name-in-python – letsc Nov 23 '15 at 22:13
  • [Here](https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files) are some examples about handling files (don't forget to close the file at the end), but you already got the idea: `f.readlines()` returns a [list](https://docs.python.org/2/tutorial/datastructures.html#more-on-lists), that you can [iterate](https://docs.python.org/2/tutorial/controlflow.html#for-statements) on. – CristiFati Nov 23 '15 at 22:28
  • letsc thank you that is exactly what I was trying to do. I pulling from a list of names to open a file with that name to read from. Then with that file open create a list with every other line being added to a new list with the name of the list being the name pulled from the first list. – Michael Smith Nov 23 '15 at 22:41
  • @MichaelSmith, you really need to edit your question with a clear example. – Mark Tolonen Nov 23 '15 at 22:45
  • Would it be easier to switch my list to a dictionary, the nest the info into the dictionary under each name? – Michael Smith Nov 23 '15 at 22:46
  • List1 is in a different location containing names. – Michael Smith Nov 23 '15 at 22:48
  • List1 is in a different location containing names. I'm importing this list into the new program . I am calling each individual item from List1, in order to open a file which contains dates, times, and a value. I am extracting the value from this file in which I would like to add to a list. I would like this new list be named after the item from List1. So in the next step in program I can call these values to run through algorythyms. If I don't have them in a list to be added to my algorythym. If they aren't in a list I cannot run the algorythym accurately. – Michael Smith Nov 23 '15 at 22:56

1 Answers1

0

Based on the comment of @letsc, if that is what you think you want, what you really need is a dictionary:

D = { 'a':[], 'b':[], 'c':[] }

That is three lists whose names are the keys in the dictionary. The way to create that would be:

>>> Cs = ['a', 'b', 'c']
>>> D = {name:[] for name in Cs}
>>> D
{'a': [], 'b': [], 'c': []}

The syntax in the second line is called a dictionary comprehension. It creates a key:value pair in the dictionary for each item in a sequence.

Accessing the named lists:

>>> D['a']
[]
>>> D['b']
[]
>>> D['a'].append(1)
>>> D['a']
[1]
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251