Python双循环中所有字符串的大写

发布于 2024-12-02 11:12:14 字数 602 浏览 3 评论 0原文

我有以下 Python 代码,它循环遍历字符串并将每个字符大写:

str = 'abcd'
l  = list(str)
for i in range(len(l)):
    rl = list(str)
    cap_char = l[i].capitalize()
    rl[i] = cap_char
    str1 = ''.join(rl)
    print str1

生成:

Abcd aBcd abCd abcD

我想增强此代码以增加受大写限制的连续字符的数量,直到该数量达到 len(l) - 1 以产生:

Abcd aBcd abCd abcD    >> - 1 char capitalized
ABcd aBCd abCD AbcD    >> - 2 chars capitalized
ABCd aBCD AbCD ABcD    >> - 3 chars capitalized

当我进行索引算术时,我遇到“索引超出范围”错误,理解 idices 应该换行,但似乎无法生成优雅的代码;(

I have the following Python code, that cycles thru the string and capitalizes each character:

str = 'abcd'
l  = list(str)
for i in range(len(l)):
    rl = list(str)
    cap_char = l[i].capitalize()
    rl[i] = cap_char
    str1 = ''.join(rl)
    print str1

Which produces:

Abcd aBcd abCd abcD

I would like to enhance this code to increment number of successive characters subject to capitalization until such number reaches len(l) - 1 to produce:

Abcd aBcd abCd abcD    >> - 1 char capitalized
ABcd aBCd abCD AbcD    >> - 2 chars capitalized
ABCd aBCD AbCD ABcD    >> - 3 chars capitalized

I am running into "index out of range" errors when I do index arithmetic, understand idices should wrap, but can't seem to produce an elegant code ;(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残龙傲雪 2024-12-09 11:12:14
import itertools
x = 'abcd'
n = len(x)
for i in xrange(1,n):
  combinations = itertools.combinations(range(n), i)
  for c in combinations:
    print ''.join([k if m not in c else k.upper() for m,k in enumerate(x)]),
  print '    >> - {0} char(s) capitalized'.format(i)

输出:

Abcd aBcd abCd abcD     >> - 1 char(s) capitalized
ABcd AbCd AbcD aBCd aBcD abCD     >> - 2 char(s) capitalized
ABCd ABcD AbCD aBCD     >> - 3 char(s) capitalized
import itertools
x = 'abcd'
n = len(x)
for i in xrange(1,n):
  combinations = itertools.combinations(range(n), i)
  for c in combinations:
    print ''.join([k if m not in c else k.upper() for m,k in enumerate(x)]),
  print '    >> - {0} char(s) capitalized'.format(i)

Output:

Abcd aBcd abCd abcD     >> - 1 char(s) capitalized
ABcd AbCd AbcD aBCd aBcD abCD     >> - 2 char(s) capitalized
ABCd ABcD AbCD aBCD     >> - 3 char(s) capitalized
我的鱼塘能养鲲 2024-12-09 11:12:14

在计算索引号时使用模运算符:

idx = idx % len(str)

顺便说一句,不要使用 str 作为 python 中的变量名称。要了解原因,请尝试以下操作:

print str(4)
str = 'foo'
print str(4)

use modulo operator while computing index number:

idx = idx % len(str)

BTW, do not use str as a varaible name in python. To understand why, try this:

print str(4)
str = 'foo'
print str(4)
垂暮老矣 2024-12-09 11:12:14

根据您对 wim 问题的回答,您要么想要 wim 的答案 或这个:

>>> def upper_case(str_, start, end):
...  substr = str_[start:end].upper()
...  return str_[:start] + substr + str_[end:]
... 
>>> def raise_combinations(str_, length):
...  for x in xrange(len(str_) - length + 1):
...   print(upper_case(str_, x, x + length))
... 
>>> raise_combinations('abcdefghi', 1)
Abcdefghi
aBcdefghi
abCdefghi
abcDefghi
abcdEfghi
abcdeFghi
abcdefGhi
abcdefgHi
abcdefghI
>>> raise_combinations('abcdefghi', 4)
ABCDefghi
aBCDEfghi
abCDEFghi
abcDEFGhi
abcdEFGHi
abcdeFGHI

编辑:当然,如果你想循环这个:

>>> str_ = "abcd"
>>> for x in xrange(1, len(str_) + 1):
...  raise_combinations(str_, x)
... 
Abcd
aBcd
abCd
abcD
ABcd
aBCd
abCD
ABCd
aBCD
ABCD
>>> 

Depending on your answer to wim's question, you either want wim's answer or this:

>>> def upper_case(str_, start, end):
...  substr = str_[start:end].upper()
...  return str_[:start] + substr + str_[end:]
... 
>>> def raise_combinations(str_, length):
...  for x in xrange(len(str_) - length + 1):
...   print(upper_case(str_, x, x + length))
... 
>>> raise_combinations('abcdefghi', 1)
Abcdefghi
aBcdefghi
abCdefghi
abcDefghi
abcdEfghi
abcdeFghi
abcdefGhi
abcdefgHi
abcdefghI
>>> raise_combinations('abcdefghi', 4)
ABCDefghi
aBCDEfghi
abCDEFghi
abcDEFGhi
abcdEFGHi
abcdeFGHI

EDIT: And, of course, if you want to loop over this:

>>> str_ = "abcd"
>>> for x in xrange(1, len(str_) + 1):
...  raise_combinations(str_, x)
... 
Abcd
aBcd
abCd
abcD
ABcd
aBCd
abCD
ABCd
aBCD
ABCD
>>> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文