Python 如何对字符串的奇偶字应用不同的运算?

Python 如何对字符串的奇偶字应用不同的运算?,python,string,python-3.x,Python,String,Python 3.x,我正在尝试更新字符串,以便: 奇数位置上的每个单词都变成大写。 偶数位置上的每个字都会颠倒 例如,如果我的输入字符串是: n = 'what is the boarding time in bangalore station' 我希望我的输出字符串为: WHAT si THE gnidraob TIME ni BANGALORE noitats 以下是我尝试的代码: n = 'what is the boarding time in bangalore station' m = n.spli

我正在尝试更新字符串,以便:

奇数位置上的每个单词都变成大写。 偶数位置上的每个字都会颠倒 例如,如果我的输入字符串是:

n = 'what is the boarding time in bangalore station'
我希望我的输出字符串为:

WHAT si THE gnidraob TIME ni BANGALORE noitats
以下是我尝试的代码:

n = 'what is the boarding time in bangalore station'
m = n.split(" ")
k=m[0::2]
for i in k:
    print(i.upper())

您的解决方案不完整。与其将每个备用单词切分并一次执行一种操作,不如依次迭代每个单词,并根据需要进行处理

这里有一个简单的方法来解决你的问题。定义一个小函数来为您执行此操作

将字符串拆分为单词 检查每个单词是否为偶数。如果是,则大写。如果没有,请将其反转 将转换后的单词连接成单个字符串 f所做的是将字符串拆分为一个单词列表。将列表转换为索引、单词元组列表。使用索引确定单词是奇数还是偶数。yield将函数转换为生成器,每次从循环中生成一个字。最后,str.join将每个单词连接回一个字符串


请注意,还有其他方法可以实现这一点。我把它们作为练习留给你。

你的解决方案不完整。与其将每个备用单词切分并一次执行一种操作,不如依次迭代每个单词,并根据需要进行处理

这里有一个简单的方法来解决你的问题。定义一个小函数来为您执行此操作

将字符串拆分为单词 检查每个单词是否为偶数。如果是,则大写。如果没有,请将其反转 将转换后的单词连接成单个字符串 f所做的是将字符串拆分为一个单词列表。将列表转换为索引、单词元组列表。使用索引确定单词是奇数还是偶数。yield将函数转换为生成器,每次从循环中生成一个字。最后,str.join将每个单词连接回一个字符串


请注意,还有其他方法可以实现这一点。我把它们作为练习留给你。

用理解回答问题的另一种方法:

n = 'what is the boarding time in bangalore station'
b = n.split()
final = ' '.join(map(lambda x: ' '.join(x), ((k.upper(), v[::-1]) for k, v in zip(b[::2], b[1::2]))))

print(final)
输出:

'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'

使用理解回答问题的另一种方法:

n = 'what is the boarding time in bangalore station'
b = n.split()
final = ' '.join(map(lambda x: ' '.join(x), ((k.upper(), v[::-1]) for k, v in zip(b[::2], b[1::2]))))

print(final)
输出:

'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'
您可以与列表理解表达式一起使用,以获得所需的字符串。例如:

>>> my_str = "what is the boarding time in bangalore station"
>>> words = my_str.split()  # list of words

>>> ' '.join(['%s %s'%(x.upper(), y[::-1]) for x, y in zip(words[::2], words[1::2])])
'WHAT si THE gnidraob TIME ni BANGALORE noitats'
注意:这里我使用列表理解而不是生成器表达式,因为虽然生成器表达式更有效,但与str.join一起使用时并非如此。为了了解这种奇怪行为的原因,请查看:

这里大多数基于理解的答案都使用生成器,尽管它们将其称为列表理解:D:D

您可以与列表理解表达式一起使用,以获得所需的字符串。例如:

>>> my_str = "what is the boarding time in bangalore station"
>>> words = my_str.split()  # list of words

>>> ' '.join(['%s %s'%(x.upper(), y[::-1]) for x, y in zip(words[::2], words[1::2])])
'WHAT si THE gnidraob TIME ni BANGALORE noitats'
注意:这里我使用列表理解而不是生成器表达式,因为虽然生成器表达式更有效,但与str.join一起使用时并非如此。为了了解这种奇怪行为的原因,请查看:


此处大多数基于理解的答案都使用生成器,尽管他们将其称为列表理解:D:D

另一个列表理解解决方案:

n = 'what is the boarding time in bangalore station'
print(' '.join(word[::-1] if ind % 2 == 1 else word.upper() for ind, word in enumerate(n.split())))
upper_words = [item.upper() for item in n.split()[0::2]]
reverse_words = [item[::-1] for item in n.split()[1::2]]
print(' '.join(word[0] + ' ' + word[1] for word in zip(upper_words, reverse_words)))
输出:

'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'
另一个解决方案:

n = 'what is the boarding time in bangalore station'
print(' '.join(word[::-1] if ind % 2 == 1 else word.upper() for ind, word in enumerate(n.split())))
upper_words = [item.upper() for item in n.split()[0::2]]
reverse_words = [item[::-1] for item in n.split()[1::2]]
print(' '.join(word[0] + ' ' + word[1] for word in zip(upper_words, reverse_words)))
输出:

'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'

另一个列表理解解决方案:

n = 'what is the boarding time in bangalore station'
print(' '.join(word[::-1] if ind % 2 == 1 else word.upper() for ind, word in enumerate(n.split())))
upper_words = [item.upper() for item in n.split()[0::2]]
reverse_words = [item[::-1] for item in n.split()[1::2]]
print(' '.join(word[0] + ' ' + word[1] for word in zip(upper_words, reverse_words)))
输出:

'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'
另一个解决方案:

n = 'what is the boarding time in bangalore station'
print(' '.join(word[::-1] if ind % 2 == 1 else word.upper() for ind, word in enumerate(n.split())))
upper_words = [item.upper() for item in n.split()[0::2]]
reverse_words = [item[::-1] for item in n.split()[1::2]]
print(' '.join(word[0] + ' ' + word[1] for word in zip(upper_words, reverse_words)))
输出:

'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'
'WHAT si THE gnidraob TIME ni BANGALORE noitats'

在一行中有一点更具描述性:

print(' '.join([w[::-1] if i % 2 else w.upper() for i, w in enumerate(n.split())]))
输出:

'WHAT si THE gnidraob TIME ni BANGALORE noitats'

在一行中有一点更具描述性:

print(' '.join([w[::-1] if i % 2 else w.upper() for i, w in enumerate(n.split())]))
输出:

'WHAT si THE gnidraob TIME ni BANGALORE noitats'

不要将其提升到高水平,仅通过以下方式:

n = 'what is the boarding time in bangalore station'
m = list(n.split(' '))
for i in range(len(m)):
    if i%2==0:
      print(m[i].upper(),end=' ')
    else:
        print(m[i][::-1].lower(),end=' ')

不要将其提升到高水平,仅通过以下方式:

n = 'what is the boarding time in bangalore station'
m = list(n.split(' '))
for i in range(len(m)):
    if i%2==0:
      print(m[i].upper(),end=' ')
    else:
        print(m[i][::-1].lower(),end=' ')
那么:

n = 'what is the boarding time in bangalore station'

' '.join([token.upper() if i%2==0 else token[::-1] 
          for i, token in enumerate(n.split())
         ]
        )
使用枚举进行迭代以保持计数。 用i%2检查奇数对偶数 大写(如果为偶数),反向字符串,即str[:-1]如果为奇数 使用“”联接子字符串列表。联接[…] 那么:

n = 'what is the boarding time in bangalore station'

' '.join([token.upper() if i%2==0 else token[::-1] 
          for i, token in enumerate(n.split())
         ]
        )
使用枚举进行迭代以保持计数。 用i%2检查奇数对偶数 大写(如果为偶数),反向字符串,即str[:-1]如果为奇数 使用“”联接子字符串列表。联接[…]
您的解决方案到底有什么问题?@goodvibration我不知道如何反转奇数字符串并将其连接起来。“.joinx[:-1]如果我在enumeraten.split中%2 else x.upper代表I,x,那么您的解决方案到底有什么问题?@goodvibration我不知道如何反转奇数字符串并将其连接起来。“.joinx[:-1]如果我%2 else x.upper为i,x在enumeraten.split中。谢谢。我是Python新手。您的方法是最简单的。谢谢。我是Python新手。你的方法最简单。哇。这真的很容易理解。谢谢。这真的很容易理解。感谢你列表理解vs.生成器表达式与str.join的事情很有趣。不容易发现。非常感谢。关于这一点,我刚刚读了雷蒙德·海廷格的回答。我想知道它是否与Python3.x相同。@srig是的,您也会在Python3中看到相同的行为
与str.join的生成器表达式比较有趣。不容易发现。非常感谢。关于这一点,我刚刚读了雷蒙德·海廷格的回答。我想知道Python 3.x是否也一样。@srig是的,您在Python 3中也会看到同样的行为。与往常一样,您的Python非常棒:与往常一样,您的Python非常棒: