954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

reading a list

say you have a list:

[php]
list = [1,a,2,b,3,c,4,d,5,e,6,f,7,g,8,h,9,i,10,j,11,k,12,l,13]
[/php]
What's the easiest loop set up if I want to store each number (every other slot) into another list? The above list is actually text from a file read and then split with "1 a" and such on each line of the file. After I do that, I want the other slots put into another list. Also, let's say the above letters are different and there are multiples of the same letter, I want to delete the multiples and be left with a list of all the unique letters in the original list.

Ex: if the list is [1,a,2,a,3,v,4,t,5,v,9,c], I want [1,2,3,4,5,9] and [a,c,t,v] (numerical and alphabetic order respectively)

Thanks

LaMouche
Posting Whiz in Training
269 posts since Oct 2006
Reputation Points: 83
Solved Threads: 39
 

say you have a list:

[php] list = [1,a,2,b,3,c,4,d,5,e,6,f,7,g,8,h,9,i,10,j,11,k,12,l,13] [/php] What's the easiest loop set up if I want to store each number (every other slot) into another list? The above list is actually text from a file read and then split with "1 a" and such on each line of the file. After I do that, I want the other slots put into another list. Also, let's say the above letters are different and there are multiples of the same letter, I want to delete the multiples and be left with a list of all the unique letters in the original list.

Ex: if the list is [1,a,2,a,3,v,4,t,5,v,9,c], I want [1,2,3,4,5,9] and [a,c,t,v] (numerical and alphabetic order respectively)

Thanks

hmm.list comprehension?

alpha = [ c for c in list if str(c).isalpha()]
number = [ c for c in list if str(c).isdigit()]


if you want to get unique elements, use set

ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

This tiny code snippet will separate your list and make the alpha list unique. I also added the typical list sort:
[php]raw_list = [1, 'a', 2, 'a', 3, 'v', 4, 't', 5, 'v', 9, 'c']

alpha_list = []
number_list = []
for c in raw_list:
# make the alpha_list unique
if alpha_list.count(c) < 1 and str(c).isalpha():
alpha_list.append(c)
elif str(c).isdigit():
number_list.append(c)

# sort list in place
alpha_list.sort()

print alpha_list # ['a', 'c', 't', 'v']
print number_list # [1, 2, 3, 4, 5, 9]
[/php]Added php tags to make Python code color highlighted.

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

Thanks. That was helpful, but I don't understand this line:

[PHP]if alpha_list.count(c) < 1[/PHP]

LaMouche
Posting Whiz in Training
269 posts since Oct 2006
Reputation Points: 83
Solved Threads: 39
 

[php]alpha_list.count(c)
[/php]... simply counts the number of times c is in the list. If the count < 1 then it isn't present yet and you can append the list.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You