【问题标题】:Python: Unpack individual strings from tuple using Unpacking asterisk argumentPython:使用解包星号参数从元组中解包单个字符串
【发布时间】:2013-05-01 12:22:08
【问题描述】:

在下面的代码中,我试图通过迭代作为不同长度、字符串类型的参数提供的所有单词来创建一个新单词。我在这里读到 * 运算符使其成为非关键字可选参数。

def gen_new_word(word1,*nwords):
new_word=''
t0=[i for i in nwords]
t=max(word1,max(t0))
print (t),str(len(t)) #check largest string
for i in xrange(len(t)):
    try:
        print 'doing iter i# %s and try' %(i)
        new_word=new_word+(word1[i]+nwords[i])
        print new_word
    except IndexError,e:
        print 'entered except'
        c=i
        for x in xrange(c,len(t)):
            print 'doing iter x# %s in except' %(x)
            new_word=new_word+t[x]
        break
return new_word

输出:

gen_new_word('janice','tanice','practice')
tanice 6
doing iter i# 0 and try
jtanice
doing iter i# 1 and try
jtaniceapractice
doing iter i# 2 and try
entered except
doing iter x# 2 in except
doing iter x# 3 in except
doing iter x# 4 in except
doing iter x# 5 in except
Out[84]: 'jtaniceapracticenice'

Q1:为什么max(word1,max(t0)) 不提供 'practice' 作为最大字符串,而是提供 tanice?

Q2:t=max(word1,max(t0)) 有效,但 max(word1,nwords) 无效。为什么以及是否有解决方法?

Q3:在new_word=new_word+(word1[i]+nwords[i]) 中,我希望显示字符串中的单个字母。期望的结果应该是“jtpaarnnaiicccteeice”,但应该是“jtaniceapracticenice”。 由于 * nwords 给出了存储在元组中的第一个元素。我希望 *nwords 将其扩展为单个字符串。我怎样才能做到这一点?我的意思是,我不知道一般意义上它可能包含多少参数。

【问题讨论】:

  • 您是否试图找到 A) 可以使用传递给函数的单词中的部分/所有字母来构建的单词,即传入 heroot,返回的一个值将是 @987654327 @。 AND/OR B) 从字面上尝试将单词组合成一个更大的单词:传入sleddog 将得到sleddog
  • 使用max,您不是在比较长度,而是比较词汇位置(t 在字母表中p 之后)。因此tanice > practice。示例:print max('hello', 'goodbye') 给出hello
  • 我很难看到你真正想要回到这里的是什么。你能详细说明一下吗?也许我们可以想出一个更优雅的方式来做到这一点。

标签: python argument-unpacking


【解决方案1】:

Q1:为什么 max(word1,max(t0)) 不是给出 'practice' 作为最大字符串,而是给出 tanice

字符串按字典顺序排列。 t > p 所以tanice 大于practice

Q2:t=max(word1,max(t0)) 有效,但 max(word1,nwords) 无效。为什么以及是否有解决方法?

这是因为max 需要一个可迭代的或可变数量的参数。在后一种情况下,max 试图将字符串与列表进行比较,而不是将字符串与列表中的每个元素进行比较。你可以使用itertools.chain:

max(chain([word1],nwords),key=len)  #assuming you're comparing based on length.

第三季度:...

我不太确定你在这里做什么。根据描述,您似乎想在将字符串压缩在一起后将它们链接起来:

from itertools import chain,izip_longest
''.join(chain.from_iterable(izip_longest(word1,*nwords,fillvalue='')))

这是一个没有函数的例子:

>>> from itertools import chain,izip_longest
>>> words = ('janice','tanice','practice')
>>> ''.join(chain.from_iterable(izip_longest(*words,fillvalue='')))
'jtpaarnnaiicccteeice'

解包操作符是双向的。你可以用它来表达“我希望这个函数有可变数量的位置参数”:

def foo(*args): ...

或者,“我想向这个函数传递可变数量的位置参数”:

foo(*iterable)

这里我使用第二种形式将可变数量的字符串传递给izip_longest1,它接受任意数量的位置参数。

以上代码等价于:

''.join(chain.from_iterable(izip_longest('janice','tanice','practice',fillvalue='')))

1zip_longest on python3.x

【讨论】:

  • 在第三季度,我正试图以更好的方式实现这一目标!是的,这回答了我所有的问题,并为我提供了更多可以在文档中查看的功能。谢谢。
【解决方案2】:
>>> max("tanice", "practice")
'tanice'
>>>

您应该比较 len(word) 而不是单词本身!

【讨论】:

  • max("tanice", "practice", key=len)
猜你喜欢
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多