简体   繁体   中英

rearranging list and removing special characters

I have a list that is holding 2 animal information [('alyson', '5', 'dog'), ('merlin', '6', 'cat')] I need to rearrange the list to the order of animal breed, name, age and then to remove the special characters and write them back onto a .txt file. I've tried .remove for it as a list. My next trial and error was converting the list into a string and tried using the .replace method. None of the methods worked. I may have used them incorrectly. Any advice on where i can look into is much appreciated

edit:The program is suppose to pull a list of animal information that is in the format of:breed, name, age. In one of my function, I need to rearrange it and output it in name,age,breed. Then the user would prompt whether the animal is adopted or a new animal comes in and my pet list would reflect upon that. Once the user is complete, the program is to save the updated list back to its original format. I'm having issues with the special characters. I did a test print of pet_list[0] and it would give me an output of ('alyson',

#This function opens the file and arranges it into an output order
#format of the file would be: dog, alyson, 5
def get_pet(file):
    f = open(file)
    pet_info = f.readlines()
    pet = []
    for line in pet_info:
        info = line.split()
        pet_type = info[0]
        name = info[1]
        age = info[2]
        pet.append((name, age, pet_type))
    return pet

#this is the function where i was planning on rearranging the list back to how it originally was and 
#rewrite them back onto a text file
def do_save(pet_list, current_file, transfer, adopt_list, transfer_list):
    shelter_list = open(current_file, 'w')
    print(pet_list)
    print(type(pet_list))
    for line in pet_list:
        print(type(line))
        shelter_list.write(str(line))
    shelter_list.close()
    adopt = open("adopted.txt", 'w')
    adopt.write(str(adopt_list))
    adopt.close()
    transfer_file = open(str(transfer), 'w')
    transfer_file.write(str(transfer_list))

    transfer_file.close()

If you have a list of tuples (or lists), you can sort it by multiple keys with itemgetter. An example with dummy data:

import operator
mylist=[
('an2d4', '1', 'kitty'), 
('an2d4', '2', 'kitty'),
('an2d4', '3', 'kitty'),
('an2d4', '1', 'puppy'),
('an2d4', '2', 'puppy'),
('an2d4', '3', 'puppy'),
('b@ddy', '1', 'bear'), 
('b@ddy', '2', 'bear'),
('b@ddy', '3', 'bear'),
('b@ddy', '1', 'wombat'),
('b@ddy', '2', 'wombat'),
('b@ddy', '3', 'wombat')
]

mylist.sort(key=operator.itemgetter(2,0,1))

calling mylist then shows that it's been sorted first by the item in the 2nd index (species), then the 0th index (name), then 1st index (age).

for i in mylist:
    print i

('b@ddy', '1', 'bear')
('b@ddy', '2', 'bear')
('b@ddy', '3', 'bear')
('an2d4', '1', 'kitty')
('an2d4', '2', 'kitty')
('an2d4', '3', 'kitty')
('an2d4', '1', 'puppy')
('an2d4', '2', 'puppy')
('an2d4', '3', 'puppy')
('b@ddy', '1', 'wombat')
('b@ddy', '2', 'wombat')
('b@ddy', '3', 'wombat')

You can then write a function to accept your 3-item-tuple entry and return the entry without special characters in the name, and iterate through your list of lists.

def fixName(entry):
    name = entry[0]
    fixedName = ''.join([x for x in name if x.isalpha()])
    return((fixedName, entry[1], entry[2]))

mylist=[ fixName(x) for x in mylist]

for i in mylist:
    print i

('bddy', '1', 'bear')
('bddy', '2', 'bear')
('bddy', '3', 'bear')
('and', '1', 'kitty')
('and', '2', 'kitty')
('and', '3', 'kitty')
('and', '1', 'puppy')
('and', '2', 'puppy')
('and', '3', 'puppy')
('bddy', '1', 'wombat')
('bddy', '2', 'wombat')
('bddy', '3', 'wombat')

Then iterate through your list and write to text file, with whatever seperator you want.

with open('out.txt', 'w') as outfile:
    for entry in mylist:
        outfile.write('\t'.join(entry)+'\n')

If you just want to rearrange each item and write to file. you can string format each item in pet_list. To convert (breed, name, age) to (name,age,breed) you could use something like this:

pet_list = [( 'dog', 'alyson', '5'), ( 'cat','merlin','10')]

for i in pet_list:
    print("{1} {2} {0}".format(*i))

results in

alyson 5 dog
merlin 10 cat

Here I show how to print, but instead of printing you can write to a file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM