替换列表中的单词

时间:2019-02-17 11:24:34

标签: python

我想以以下格式在屏幕上显示居住在马德里中的所有用户:

居住在“ CITY”的用户“ NAME”的年龄为“ AGE”岁,其债务为:“ DEBT” EUR。

您可以看到保存马德里的不同方式,我想找到一种打印这些用户的方式,因为有些人用大写字母书写,而另一些人不使用大写字母,他们都居住在马德里。

students = [
    ('Marcos', 23, 'Madrid', 850, '2388711341'),
    ('Elena', 35, 'MaDrid', 360, '0387700342'),
    ('Carmen', 21, 'Getafe', 50, '0014871388'),
    ('Carlos', 41, 'MAdrid', 580, '00887118456'),
    ('Maria', 28, 'Madrixx', 150, '587')
]

对于学生中的项目:     学生,年龄,城镇,债务,身份证=项目

所需的结果:

The user Marcos lives in Madrid, has an age of 23 years old and its debt is: 850 EUR.

The user Elena lives in Madrid, has an age of 35 years and its debt is: 360 EUR.

The user Carlos lives in Madrid, has an age of 41 years old and its debt is: 580 EUR.

The user Maria lives in Madrid, has an age of 28 years old and its debt is: 150 EUR.

6 个答案:

答案 0 :(得分:1)

要过滤您的输出,您可以在全小写的if上使用简单的'madrid'条件:

students = [
    ('Marcos', 23, 'Madrid', 850, '2388711341'),
    ('Elena', 35, 'MaDrid', 360, '0387700342'),
    ('Carmen', 21, 'Getafe', 50, '0014871388'),
    ('Carlos', 41, 'MAdrid', 580, '00887118456'),
    ('Maria', 28, 'Madrixx', 150, '587')
]


for item in students:
    # decompose the item into its parts - this also fixes your NameError
    student, age, town, debt, something = item

    # use the decomposed variables instead of item[.]
    # make town all lowercase and compare - only print if matches
    if town.lower() == "madrid":
        print(f'The user {student} lives in {town.title()}, has an age of '
              f'{age} and its debt is: {debt} EUR.')

输出:

# if you simply print town
The user Marcos lives in Madrid, has an age of 23 and its debt is: 850 EUR.
The user Elena lives in MaDrid, has an age of 35 and its debt is: 360 EUR.
The user Carlos lives in MAdrid, has an age of 41 and its debt is: 580 EUR.

# with town.title()
The user Marcos lives in Madrid, has an age of 23 and its debt is: 850 EUR.
The user Elena lives in Madrid, has an age of 35 and its debt is: 360 EUR.
The user Carlos lives in Madrid, has an age of 41 and its debt is: 580 EUR.

@marmeladze指出我想念玛丽亚(Maria),她似乎无法使用她的正确城镇名称-您可以使用以下方法来减轻她的无能。

if "madrid".startswith(town.lower()[:4]):

代替

if town.lower() == "madrid":

获得以下输出:

The user Marcos lives in Madrid, has an age of 23 and its debt is: 850 EUR.
The user Elena lives in Madrid, has an age of 35 and its debt is: 360 EUR.
The user Carlos lives in Madrid, has an age of 41 and its debt is: 580 EUR.
The user Maria lives in Madrixx, has an age of 28 and its debt is: 150 EUR.

我只是希望您永远不会让'Madravingpeopletown'中的任何人进入您的列表-他们也会出现...

答案 1 :(得分:1)

确实很难确定您想要的内容,但是如果您要打印居住在马德里的校友数据,请尝试以下操作。

students = [
    ('Marcos', 23, 'Madrid', 850, '2388711341'), 
    ('Elena', 35, 'MaDrid', 360, '0387700342'),
    ('Carmen', 21, 'Getafe', 50, '0014871388'), 
    ('Carlos', 41, 'MAdrid', 580, '00887118456'),
    ('Maria', 28, 'Madrixx', 150, '587')
]



for student_detail in students:
    if student_detail[2].lower().startswith('madri'):
        print(f"The user {student_detail[0]}, lives in Madrid. S/He is {student_detail[1]} years old and has {student_detail[3]} as debt")

答案 2 :(得分:0)

欢迎!

您将需要以某种方式标准化所有这些名称。给定您的示例数据,类似capitalize()方法的方法应该可以使用。

[...]
for student in students:
    normalized_city = item[2]
    print(f'The user {student[0]} lives in {student[2].capitalize()}, has an age of '
          f'{student[1]} and its debt is: {student[3]} EUR.')

我鼓励您在存储数据之前进行规范化,因此,无论用户如何写城市,都应将规范化保存起来。

答案 3 :(得分:0)

代码:

students = [
    ('Marcos', 23, 'Madrid', 850, '2388711341'),
    ('Elena', 35, 'MaDrid', 360, '0387700342'),
    ('Carmen', 21, 'Getafe', 50, '0014871388'),
    ('Carlos', 41, 'MAdrid', 580, '00887118456'),
    ('Maria', 28, 'Madrixx', 150, '587')
]
for student in [s for s in students if s[2].lower() == 'madrid']:
    print('The user {} lives in {}, has an age of {} years old and its debt is: {} EUR.'.format(student[0],
                                                                                            student[2].title(),
                                                                                            student[1], student[3]))

#
# A cleaner solution (using named tuple)
#

import collections

Student = collections.namedtuple('Student', 'name age city debt id')

students = [
    Student('Marcos', 23, 'Madrid', 850, '2388711341'),
    Student('Elena', 35, 'MaDrid', 360, '0387700342'),
    Student('Carmen', 21, 'Getafe', 50, '0014871388'),
    Student('Carlos', 41, 'MAdrid', 580, '00887118456'),
    Student('Maria', 28, 'Madrixx', 150, '587')
]
for student in [s for s in students if s.city.lower() == 'madrid']:
    print('The user {} lives in {}, has an age of {} years old and its debt is: {} EUR.'.format(student.name,
                                                                                            student.city.title(),
                                                                                            student.age,
                                                                                            student.debt))

输出:

The user Marcos lives in Madrid, has an age of 23 years old and its debt is: 850 EUR.
The user Elena lives in Madrid, has an age of 35 years old and its debt is: 360 EUR.
The user Carlos lives in Madrid, has an age of 41 years old and its debt is: 580 EUR.

答案 4 :(得分:-1)

欢迎堆栈溢出。

要转换“马德里”和“赫塔费”(其中必须满足和城市)的所有不同大小写,请使用string.lower(),然后将其与马德里的小写字母匹配。

  

str.lower()

     

返回字符串的副本,并将所有大小写的字符[4]转换为小写。

我看到没有答案涉及“ Madrixx”的例外,因此这个答案包含了这一点。如果您认为不必要,请随意选择其他选项。

所以我的代码是:

students = [
('Marcos', 23, 'Madrid', 850, '2388711341'),
('Elena', 35, 'MaDrid', 360, '0387700342'),
('Carmen', 21, 'Getafe', 50, '0014871388'),
('Carlos', 41, 'MAdrid', 580, '00887118456'),
('Maria', 28, 'Madrixx', 150, '587'),
]

for student in students:
city = student[2].lower()
if city[:5] == "madri" or city == "getafe":
    print("The user %s lives in Madrid,  has an age of %d years old and its debt is: %d EUR." % (student[0], student[1], student[3]))

以下是输出:

The user Marcos lives in Madrid,  has an age of 23 years old and its debt is: 850 EUR.
The user Elena lives in Madrid,  has an age of 35 years old and its debt is: 360 EUR.
The user Carmen lives in Madrid,  has an age of 21 years old and its debt is: 50 EUR.
The user Carlos lives in Madrid,  has an age of 41 years old and its debt is: 580 EUR.
The user Maria lives in Madrid,  has an age of 28 years old and its debt is: 150 EUR.

说明:

我们首先定义您提到的数据集。我复制粘贴了您的代码。

for student in students:这会循环搜索列表中的所有项目。首先,变量student Marcos ,然后是 Elena ,依此类推。

city = student[2].lower():这将获取列表中的 third 项,位置,并将其转换为小写。

if city[:5] == "madri" or city == "getafe"::这是为了处理数据集。我检查前五个字符是否为“ madri”(包括生活在“ Madrixx”中的Maria) OR 城市是否为“ getafe”,在马德里也是如此。

print("The user %s lives in Madrid, has an age of %d years old and its debt is: %d EUR." % (student[0], student[1], student[3])):在这里,我们做了一些python字符串格式化的魔术。 %s插入一个字符串,%d插入一个整数。我们将%s,%d和%d用于面积,年龄和债务(按该顺序)。然后,在关闭的字符串的末尾,放置% (student[0], student[1], student[3]),这是要插入字符串的值。

希望这很有用,并且在翻译方面做得很好!

答案 5 :(得分:-1)

对于学生中的项目:     学生,年龄,城市,债务,ID =项目     如果city.startswith('M'):         print(f'用户{student}居住在{city},年龄为{age},其债务为:{debt} EUR。')

相关问题