有没有更好的方法来实现我在这里尝试做的事情?

时间:2015-08-14 16:12:57

标签: python python-2.7

我正在创建一个单词生成工具,它可以获取提供的单词并为其添加各种重复数据。

现在我正在实现我想要做的事情,但我的方法看起来很长。有更好的方法吗?

#!/usr/local/bin/python

words = raw_input("Enter Word!: ")


def add_numbers(wordlist):
    parts = wordlist.split()
    numbers_list = ["1","2","3","4","5","6","7","8","9","10"]

    try:
        value1 = parts[0]
        value2 = parts[1]
        value3 = parts[2]
        value4 = parts[3]
        value5 = parts[4]
    except IndexError:
        bads = 'null'

    try: value1
    except NameError: value1 = None
    try: value2
    except NameError: value2 = None
    try: value3
    except NameError: value3 = None
    try: value4
    except NameError: value4 = None
    try: value5
    except NameError: value5 = None

    if value1 is None:
        pass
    else:
        first_list = []
        first_list.append(value1+str(numbers_list[0]))
        first_list.append(value1+str(numbers_list[0])+str(numbers_list[1]))
        first_list.append(value1+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2]))
        first_list.append(value1+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3]))
        first_list.append(value1+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4]))
        first_list.append(value1+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4])+str(numbers_list[5]))
        first_list.append(value1+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4])+str(numbers_list[5])+str(numbers_list[6]))
        first_list.append(value1+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4])+str(numbers_list[5])+str(numbers_list[6])+str(numbers_list[7]))
        first_list.append(value1+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4])+str(numbers_list[5])+str(numbers_list[6])+str(numbers_list[7])+str(numbers_list[8]))
        first_list.append(value1+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4])+str(numbers_list[5])+str(numbers_list[6])+str(numbers_list[7])+str(numbers_list[8])+str(numbers_list[9]))
        print first_list

    if value2 is None:
        pass
    else:
        second_list = []
        second_list.append(value2+str(numbers_list[0]))
        second_list.append(value2+str(numbers_list[0])+str(numbers_list[1]))
        second_list.append(value2+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2]))
        second_list.append(value2+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3]))
        second_list.append(value2+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4]))
        second_list.append(value2+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4])+str(numbers_list[5]))
        second_list.append(value2+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4])+str(numbers_list[5])+str(numbers_list[6]))
        second_list.append(value2+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4])+str(numbers_list[5])+str(numbers_list[6])+str(numbers_list[7]))
        second_list.append(value2+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4])+str(numbers_list[5])+str(numbers_list[6])+str(numbers_list[7])+str(numbers_list[8]))
        second_list.append(value2+str(numbers_list[0])+str(numbers_list[1])+str(numbers_list[2])+str(numbers_list[3])+str(numbers_list[4])+str(numbers_list[5])+str(numbers_list[6])+str(numbers_list[7])+str(numbers_list[8])+str(numbers_list[9]))
        print second_list

3 个答案:

答案 0 :(得分:2)

这样的事情怎么样?

worldlist = "test hello world"
parts = worldlist.split()
values = list(range(1,11))
mangled = []
for part in parts:
    l = []
    val = ''
    for x in values:
        val += str(x)
        l.append(part + val)
    mangled.append(l)

for l in mangled:
    print(l)

结果:

['test0', 'test01', 'test012', 'test0123', 'test01234', 'test012345', 'test0123456', 'test01234567', 'test012345678', 'test0123456789', 'test012345678910']
['hello0', 'hello01', 'hello012', 'hello0123', 'hello01234', 'hello012345', 'hello0123456', 'hello01234567', 'hello012345678', 'hello0123456789', 'hello012345678910']
['world0', 'world01', 'world012', 'world0123', 'world01234', 'world012345', 'world0123456', 'world01234567', 'world012345678', 'world0123456789', 'world012345678910']

答案 1 :(得分:1)

从顶部开始:

#!/usr/local/bin/python

words = raw_input("Enter Word!: ")

def add_numbers(wordlist):
    parts = wordlist.split()
    numbers_list = ["1","2","3","4","5","6","7","8","9","10"]

在这里,您可以将各种默认值添加到parts的末尾,以便在您过早地“耗尽”parts时使用它们。我忽略bads,因为你似乎永远不会使用它。

    value1, value2, value3, value4, value5 = (parts + [None]*5)[:5]

几乎没有任何理由将pass与条件语句一起使用。在这里,您可以简单地反转测试并忽略else

    if value1 is not None:

您重复使用相同的子表达式,因此保存结果,这样您就不需要反复做同样的事情。基本上,您只想在迭代numbers_list

时累积值
        first_list = []
        str_value = str(value1)
        for num_value in numbers_list:
            str_value += num_value
            first_list.append(str_value)

        print first_list

value2等人也这样做。

    if value2 is not None:

        second_list = []
        str_value = str(value1)
        for num_value in numbers_list:
            str_value += num_value
            second_list.append(str_value)

        print second_list

您可能希望使用值列表,而不是5个单独的value1value2等变量,但我将其作为练习来进行替换。

答案 2 :(得分:0)

老实说,我很难理解你 想要实现的目标,但这里有更紧凑的代码,可以产生相同的结果:

def add_numbers(wordlist):
    parts = wordlist.split()

    for x in xrange(0, 2):
        if len(parts) < x+1:
            pass
        else:
            s = parts[x]
            some_list = [s]
            for i in xrange(1, 11):
                s += str(i)
                some_list.append(s)
            print some_list


add_numbers('Carl stole corals from Clara')