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

时间:2017-03-22 19:38:00

标签: python python-3.x

我有一个包含2个动物信息的列表 [('alyson', '5', 'dog'), ('merlin', '6', 'cat')] 我需要将列表重新排列为动物品种,名称,年龄的顺序,然后删除特殊字符并将它们写回.txt文件。 我已经尝试.remove作为列表了。我的下一个试验和错误是将列表转换为字符串并尝试使用.replace方法。没有一种方法有效。我可能错误地使用了它们。关于我可以调查的地方的任何建议都非常感谢

编辑:该程序假设提取以下格式的动物信息列表:品种,姓名,年龄。在我的一个功能中,我需要重新排列它并输出名称,年龄,品种。然后用户会提示动物是否被采用或是否有新动物进入并且我的宠物清单会反映出来。用户完成后,程序将更新的列表保存回原始格式。我遇到了特殊字符的问题。我做了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()

2 个答案:

答案 0 :(得分:2)

如果您有元组(或列表)列表,可以使用itemgetter按多个键对其进行排序。虚拟数据的示例:

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))

调用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')

然后,您可以编写一个函数来接受您的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')

然后遍历您的列表并使用您想要的任何分隔符写入文本文件。

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

答案 1 :(得分:0)

如果您只想重新排列每个项目并写入文件。你可以在pet_list中对每个项目进行字符串格式化。要将(品种,姓名,年龄)转换为(姓名,年龄,品种),您可以使用以下内容:

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

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

结果

alyson 5 dog
merlin 10 cat

这里我展示了如何打印,但不是打印,而是可以写入文件。

相关问题