我是python的菜鸟,我被困在一个简单的任务上。我在stackoverflow上查看了类似的问题,但无法应用于我自己的问题。我创建了一个包含名称列表的文件。如何创建一个新文件,其中它读取原始文件(假装我不知道文件中有多少名称)并反转名称(人们的名字相同,但名字前有姓氏)。以下是我的剧本:
firstname1 = "Morty"
lastname1 = "Smith"
fullname1 = firstname1 + " " + lastname1
firstname2 = "Donna"
lastname2 = "Gueterman"
fullname2 = firstname2 + " " + lastname2
firstname3 = "Beth"
lastname3 = "Smith"
fullname3 = firstname3 + " " + lastname3
firstname4 = "Rick"
lastname4 = "Sanchez"
fullname4 = firstname4 + " " + lastname4
firstname5 = "Little"
lastname5 = "Dipper"
fullname5 = firstname5 + " " + lastname5
formal_names = fullname1,fullname2,fullname3,fullname4,fullname5
f = open("names_normal.txt","w")
f.write("%s " % (formal_names, ))
f.close()
def reverse_list(lst):
new = []
count = len(lst)-1
while count >=0:
new.append(lst[count])
count -= 1
return new
x = reverse_list(formal_names)
n = open("names_new.txt","w")
n.write("%s " % (x, ))
n.close()
这些是我的结果:
第一档: ('Morty Smith','Donna Gueterman','Beth Smith','Rick Sanchez', '小北斗星')
第二档:('小北斗星','里克桑切斯','贝丝史密斯','唐娜盖特曼','莫蒂史密斯')
我想要的是:('Smith Morty','Gueterman Donna','Smith Beth','Sanchez Rick','Dipper Little')
任何帮助将不胜感激!非常感谢!
答案 0 :(得分:0)
def reverse_list(lst):
new = []
for name in lst:
temp = name.split(" ")
new += [temp[1]+" "+temp[0]]
您只是更改数组中单词的顺序,您应该通过从空格分割来更改名称和姓氏的顺序。
答案 1 :(得分:0)
def reverse_name(name):
# Try to fill here and update if doesn't work
def reverse_list(lst):
new = []
for name in lst:
new.append(reverse_name(name))
return new
答案 2 :(得分:0)
这是我攻击这个问题的方法。如上所述,最好将其置于一个功能中。
firstname1 = "Morty"
lastname1 = "Smith"
fullname1 = firstname1 + " " + lastname1
firstname2 = "Donna"
lastname2 = "Gueterman"
fullname2 = firstname2 + " " + lastname2
firstname3 = "Beth"
lastname3 = "Smith"
fullname3 = firstname3 + " " + lastname3
firstname4 = "Rick"
lastname4 = "Sanchez"
fullname4 = firstname4 + " " + lastname4
firstname5 = "Little"
lastname5 = "Dipper"
fullname5 = firstname5 + " " + lastname5
formal_names = [fullname1,fullname2,fullname3,fullname4,fullname5]
reversed = []
for i in range(0, (len(formal_names))):
rev = formal_names[i].split()
rev_name = rev[1] + ' ' + rev[0]
reversed.append(rev_name)
print(reversed)
['Smith Morty', 'Gueterman Donna', 'Smith Beth', 'Sanchez Rick', 'Dipper Little']
现在你只需将列表写入文件
with open('output.txt', 'w') as file:
for name in reversed:
file.write(name)
答案 3 :(得分:0)
对于每个人的名字,使用单独的变量使事情变得复杂。在Python中,更好的处理方法是使用python的数据结构之一,在本例中为list
,每个名称都包含在tuple
中。这样做的好处是你可以迭代列表并按位置引用名称的部分,例如:names[0][1]
代表“史密斯”。
names = [("Morty","Smith"),
("Donna","Gueterman"),
("Beth","Smith"),
("Rick","Sanchez"),
("Little","Dipper")]
或者,如果您希望可以使用namedtuple
来清楚地表明事情,特别是如果您为每个人添加其他数据项。
import collections
Person = collections.namedtuple('Person', 'first last')
names = [Person("Morty","Smith"),
Person("Donna","Gueterman"),
Person("Beth","Smith"),
Person("Rick","Sanchez"),
Person("Little","Dipper")]
然后可以使用列表推导创建输出:
normal = ", ".join([name.first + " " + name.last for name in names])
reversed = "; ".join([name.last + ", " + name.first for name in names])
print(normal)
print(reversed)
您可以更改标点符号:第一个字符串", "
位于名称之间,第二个字符串" "
位于单词之间。然后,使用先前答案中建议的方法将字符串存储在文件中。
(我建议在最后和第一个之间使用标点符号,否则你可能会遇到像“Alan Simon”这样的名字的问题。)