0

If i have lists for example:

['6'] #Number
['!'] #Punctuation
['r'] #Alphabet
['8'] #Number
['/'] #Punctuation
['e'] #Alphabet
['5'] #Number
[':'] #Punctuation
['l'] #Alphabet

I use data = line.strip().split(' ') to convert it into this form from a csv file. I am trying to assign the elements in the lists to their respective variable For example number will contain the lists that have numbers in it, punctuation will contain the lists that have punctuation in it and alphabet will have lists with alphabets.

What I can't understand is if I do something like

number = data[0], punc = data[1], alpha = data[2]

I get an error:

List index out of range.

So how can i solve this problem?

My code,

for line in new_file:
      text = [line.strip() for line in line.split(' ')]
5
  • 3
    Can you add your code to question? Commented Nov 7, 2015 at 19:45
  • Okay i have entered my code in the question!! Commented Nov 7, 2015 at 19:51
  • @Luke please edit the question with your code Commented Nov 7, 2015 at 19:51
  • Could you please change [line.strip() for line in line.split(' ')] to something like [word.strip() for word in line.split(' ')]? That makes much more sense... Commented Nov 7, 2015 at 19:54
  • okay i will do that! Commented Nov 7, 2015 at 19:55

2 Answers 2

1

This part of your code appears to be fine

for line in new_file:
      text = [line.strip() for line in line.split(' ')]

however if you are doing the following

for line in new_file:
      text = [line.strip() for line in line.split(' ')]
      number = text[0], punc = text[1], alpha = text[2]

You may ran into problems..take for example a line in your file below

"hello world"

if you split this line you will have a list like ["hello", "world"].This list contains two elements.

Now if you assign this result like text=["hello", "world"]

and you place this result in a variable like

alpha = text[2]

You will certainly recieve List index out of range. ..Why?

Because text[2] does not exist!

Some lines may contain less then 3 words (like in this example)

Revise your approach

Try using a dictionary approach

alpha={"alphabet":[]}
numb={"alphabet":[]}
punc={"punctuation":[]}

..iterate through the file and use list comprehension to select all punctuation, letters, etc and add it your the respective dictionary elements... If you are having trouble post your revised codes

EDIT A WORKING EXAMPLE HOW I WOULD TACKLE THIS

Let say I have a file named new_file and has the content below

hello my name is repzERO
AND THIS IS my age: 100 years

A python script I tried

import re
new_file=open("new_file","r")
alpha={"alphabet":[]}
numb={"number":[]}
punc={"punctuation":[]}
all_punctuation=""

for line in new_file:
    alpha["alphabet"]+=[c for c in line if re.search("[a-zA-Z ]",c)]
    numb["number"]+=[c for c in line if re.search("[0-9]",c)]
    punc["punctuation"]+=[c for c in line if re.search("[^\w\s]",c)]

print(alpha)
print(numb)
print(punc)

output

{'alphabet': ['h', 'e', 'l', 'l', 'o', ' ', 'm', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', ' ', 'r', 'e', 'p', 'z', 'E', 'R', 'O', 'A', 'N', 'D', ' ', 'T', 'H', 'I', 'S', ' ', 'I', 'S', ' ', 'm', 'y', ' ', 'a', 'g', 'e', ' ', ' ', 'y', 'e', 'a', 'r', 's']}
{'number': ['1', '0', '0']}
{'punctuation': [':']}
Sign up to request clarification or add additional context in comments.

2 Comments

I am trying really hard but cannot get anywhere! Using dictionary is good idea but how my program proceeds, it would be impossible to locate certain elements! My main question was that if i had that list i posted in my question, how would i spot a particular element let's say ['r'] which is at line 3. Using index i suppose! data['x'] where x is some index i don't know how to use to let the program print just ['r'] from that entire file carrying lists!
Ah! the search function.. Helps me! Thanks
0

Your lists seems to have less elements.

Something like this:

yourVariableName = ["what", "ever", "elements", "are", "here"]

is called a list. The list above has 5 elements. You can access the elements with a numeric index i:

yourVariableName[i]

where i is in this case either 0, 1, 2, 3 or 4 (or negative number when you want to count from the end). When you try

yourVariableName[5]

or even higher, you get an "index out of range" error.

5 Comments

I know how to do that, my list has less elements but my task is to assign it to a variable! if line 1 has ['5'] that mean number = data['i dont know what index goes here'] same for puctuation and alphabets! It's just this index usage i can't understand. What you explained is pretty basic and i know how to do that
Do you mean you want to append an element to the list? Like list.append("whatever")? Or do you rather want a dictionary?
Yes, i want to append. I believe dictionary would be an easier approach!!
Can we append this to a dictionary?? Considering a key to be number and values to be the numbers in the list!
Yes you can append to the Value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.