逐个迭代嵌套元组中的元素

时间:2018-02-22 09:02:18

标签: python tuples

我有一个任务要求我返回一个给定的元组,但是将它创建为一个新的元组。因此,我的代码如下。

def new_tup(tree):
    if tree == ():
        return tree
    else:
        return tree[0] + new_tup(tree[1:])

但是,如果我想将嵌套元组中的元素连接到这个新元组中,我应该如何修改此代码?目前,只有在元组中的元素只是元素时,代码才有效,但如果元组中嵌入了元组则不行。

1 个答案:

答案 0 :(得分:0)

要将数组(或元组元组)折叠到一个维度,您可以在numpy.ndarray.flatten之后使用converting to a ndarray之类的函数。

但是如果你正在寻找一个简短的功能来自己做,你可以尝试这样的事情:

#!/bin/bash
Source="/FILES/user/directory1"
file="customer*" #Not only single file all customer files need to search
string="yoursearchstring" 
mail="yourmail@maildomain.com"

Count=$(find $Source -type f -name "$file" |xargs grep [y]oursearchstring | wc -l)
#Above command find files with name "customer*" in current directory as well as in all 
#sub directories inside the parent directory and provide each file as parameter to 
#grep command. grep command will search for the string in each file and wc-l will finally 
#count the number of occurrence of the string in it
if [ "$Count" -gt 0 ]
        then
        echo "Sending mail to $mail, number of files matching $Count"
        echo "Number of files matching $string is $Count" | sendmail -t  $mail
else
     echo "Files doesn't match with string"
fi