Reputation: 57
I'm very new to Python. I want to take the contents of 2 text files, convert them into 2 lists, merge them together and order them from smallest to biggest.
Converting them to lists seems easy, but I can't merge properly for the ordering.
They come out like this (a list of lists instead of a single list): ['[0, 4, 6, 6, 22, 23, 44]', '[1, 4, 5, 6, 7, 22, 777]']
Here's my code:
with open('numbers1.txt','r') as newfile1:
list1 = newfile1.read().splitlines()
print (list1)
with open('numbers2.txt','r') as newfile2:
list2 = newfile2.read().splitlines()
print (list2)
merged = []
for i in range(0, len(list1)) :
merged.append(list1[i])
merged.append(list2[i])
print(merged)
Thanks in advance for any assistance.
Upvotes: 2
Views: 1024
Reputation: 1740
First of all, to join to lists you can use simple +
operator:
>>> l1 = [1, 2, 3]
>>> l2 = [4, 5, 6]
>>> merged = l1 + l2
>>> print(merged)
[1, 2, 3, 4, 5, 6]
Now for sorting use python built-in sort()
function:
Python lists have a built-in list.sort() method that modifies the list in-place. By default, sort() doesn't require any extra parameters. However, it has two optional parameters:
reverse - If true, the sorted list is reversed (or sorted in Descending order)
key - function that serves as a key for the sort comparison
>>> l1 = [1, 2, 3]
>>> l2 = [4, 5, 6]
>>> merged = l2 + l1
>>> print(merged)
[4, 5, 6, 1, 2, 3]
>>> merged.sort(key=int)
>>> print(merged)
[1, 2, 3, 4, 5, 6]
@Murray above-mentioned solution works just fine but it doesn't fit your use case since in your text files you have list alike string:
This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777] – Murray 1 hour ago
Now when you read file as list1/list2
actually you'll get a list with one element which is a string.
>>> with open("numbers1.txt", "r") as f:
... print(list1)
... print(type(list1))
... print(len(list1))
... print(type(list1[0]))
...
['[0, 4, 6, 6, 22, 23, 44]']
<type 'list'>
1
<type 'str'>
In order to append those numbers from file you'll need to parse them first. It can be achieved like so (depending on your exact use case final solution may vary):
$ cat numbers1.txt
[0, 4, 6, 6, 22, 23, 44]
$ cat numbers2.txt
[1, 4, 5, 6, 7, 22, 777]
$ cat test.py
files = ['numbers1.txt', 'numbers2.txt']
merged = []
for f in files:
with open(f,'r') as lines:
for line in lines:
for num in line.rstrip().strip('[]').split(', '):
merged.append(int(num))
print(merged)
merged.sort()
print(merged)
$ python test.py
[0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
[0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]
$ python3 test.py
[0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
[0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]
Now let me break down this code into pieces:
with open...
every time I want to open something (It's just easier and the code is more readable).line.rstrip()
- remove a trailing newline.strip('[]')
- remove square brackets from string.split(', ')
- split string by comma and space to get array of string charactersmerged.append(int(num))
for each character parse it to int and append to list.Upvotes: 2