Python: 可変個の引数を受け取る関数

Sponsored Link

このエントリーをはてなブックマークに追加
はてなブックマーク - Python: 可変個の引数を受け取る関数
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Livedoor Clip
Bookmark this on Yahoo Bookmark

ソースコード

#!/usr/bin/python
# coding: UTF-8

# 可変個の引数を受け取る関数

# 引数の前に*を付けると、関数内ではタプルとして受け取る
def sigma_sq(*nums): # Σf(k)^xのを計算、f(n)はnumsの要素
    print type(nums), nums
    x = 2 # 乗数
    n_elem = len(nums) # タプルの長さ
    sum = 0
    for elem in nums:
        sum += pow(elem, x) # x乗する
    return sum

print sigma_sq(1, 2, 3)

# 関数呼び出し側の引数: リスト[タプル]の前に*を付けると
# 関数側でリスト[タプル]を展開し、個々の要素を埋め込んでくれる
list = [1, 2, 3]
print sigma_sq(*list)
print sigma_sq(*[x for x in range(4)])

def plusx3(a, b, c):
    return a+b+c

print 'n', plusx3(*list)

# 補足: 上の処理を簡潔に書く
sq = 2
print 'n', sum([pow(x, sq) for x in list])

実行結果

<type 'tuple'> (1, 2, 3)
14
<type 'tuple'> (1, 2, 3)
14
<type 'tuple'> (0, 1, 2, 3)
14

6

14

チュートリアル

関連すると思われる記事:

Sponsored Link

This entry was posted in Python and tagged . Bookmark the permalink.

Facebook comments:

One Response to Python: 可変個の引数を受け取る関数

  1. Pingback: Prestissimo (Yuya T)

コメントを残す

メールアドレスが公開されることはありません。

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>