简体   繁体   English

重新排列列表并删除特殊字符

[英]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. 我有一个包含2个动物信息的列表[('alyson', '5', 'dog'), ('merlin', '6', 'cat')]我需要按照动物的顺序重新排列列表品种,名称,年龄,然后删除特殊字符并将其写回到.txt文件中。 I've tried .remove for it as a list. 我已经尝试过.remove作为列表。 My next trial and error was converting the list into a string and tried using the .replace method. 我的下一个试验和错误是将列表转换为字符串,然后尝试使用.replace方法。 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', 我对pet_list [0]进行了测试打印,结果是('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. 如果您有一个元组列表(或多个列表),则可以使用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). 调用mylist会显示它首先按照第二个索引(种类)中的项目排序,然后是第0个索引(名称),然后是第一个索引(年龄)。

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. 然后,您可以编写一个函数来接受您的3项元组条目,并返回名称中不包含特殊字符的条目,并遍历列表列表。

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. 您可以字符串格式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. 在这里,我展示了如何打印,但是除了打印之外,您还可以写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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