We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,687 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Join Lists

Hey all,

I am working on learning python, I have a question about lists.

When I call Variable x to equal numbers from a text file, I am getting the file returned in multiple lists.

with open('numbers.txt') as f:
    for item in f:
        n = item.split()
        print (n)

I end up with numbers in the format.

['1']
['2']
['3']

I want to join these numbers into one list. Unless i'm mistaken, I am calling variable n as this group of lists. When I try to combine I end up with errors.

Essentially, I want to eventually add these numbers, but I will get to that some other time.

Am I in the correct direction? Should i be reading these numbees a different way?

7
Contributors
9
Replies
23 Hours
Discussion Span
2 Months Ago
Last Updated
68
Views
Question
Answered
scubastevie
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

You can join lists like this:

listAB = listA + listB

Walara
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

The problem I am having is comprehending the fact that when I call variable n, I get it printed as two or more lists. I don't have it able to call them anything other than n right now.

scubastevie
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Looks you have only one number per line so you do not need to loop line by line. I am not going ot show directly how to make them int egers instead of str ings, but this gets you the strings:

number_strings = list(f)
pyTony
pyMod
Moderator
6,300 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

So, what will this do?

with open('numbers.txt') as f:
    for line in f:
        print(line, type(line))
Ene Uran
Posting Virtuoso
1,830 posts since Aug 2005
Reputation Points: 676
Solved Threads: 255
Skill Endorsements: 7
1
 <class 'str'>
2
 <class 'str'>
3
 <class 'str'>
4
 <class 'str'>
5
 <class 'str'>
6
 <class 'str'>
7
 <class 'str'>
8
 <class 'str'>
9
 <class 'str'>
10 <class 'str'>
>>> 
scubastevie
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

So, you have a filename called 'numbers.txt' which looks kinda like this:

'''
1
2
3
4
5
6
7
8
'''

You could make all that a list of strings, than work with them:

l = []
with open('numbers.txt') as f:
    l = list(f)
print l

Or, you could parse the entire file, line by line, and append your items to the list, also, converting them to int.
Useful links:
List operations
The 'with' statement

Good luck.

Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 11

Some more ways,i use numbers.txt as shown by Lucaci.

with open('numbers.txt') as f:
    print [i.strip() for i in f]

#['1', '2', '3', '4', '5', '6', '7', '8']

So we get a list of numbers that are strings.
To get a list with integer or float,use int() or float()

with open('numbers.txt') as f:
    print [float(i) for i in f]

#[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]

Rembere output is always strings when you read from file.
Can test with sum() and output is as expected.

with open('numbers.txt') as f:
    print sum(float(i) for i in f) #36.0
snippsat
Posting Shark
956 posts since Aug 2008
Reputation Points: 482
Solved Threads: 343
Skill Endorsements: 8

Just another way ...

with open('numbers.txt') as f:
    raw_list = f.readlines()

print raw_list

mylist = [eval(item) for item in raw_list]

print mylist

'''
['1\n', '2\n', '3\n', '4\n', '5\n', '6\n', '7\n', '8\n', '9\n', '10\n']
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
'''
vegaseat
DaniWeb's Hypocrite
Moderator
6,464 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,608
Skill Endorsements: 34

Or you could use the old fashion append style.

l = []
with open('numbers.txt') as f:
    for i in f: l.append(int(i))
print l

'''
[1, 2, 3, 4, 5, 6, 7, 8]
'''
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 11
Question Answered as of 2 Months Ago by Lucaci Andrew, vegaseat, Ene Uran and 3 others

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1056 seconds using 2.74MB