Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从元组的元组返回元组,该元组包含另一元组中元素中的给定文本_Python_Comparison_Tuples_Python 3.3 - Fatal编程技术网

Python 从元组的元组返回元组,该元组包含另一元组中元素中的给定文本

Python 从元组的元组返回元组,该元组包含另一元组中元素中的给定文本,python,comparison,tuples,python-3.3,Python,Comparison,Tuples,Python 3.3,我从.csv文件中读取了一组元组,每个元组如下所示: ("1", "08:15:25", "17161234567") ac = (212, 312, 716) def check(tuplename): results = [] for row in tuplename: if any(ac in x for x in tuplename): results.append(x) print(results) 元组中的第三个元

我从.csv文件中读取了一组元组,每个元组如下所示:

("1", "08:15:25", "17161234567")
ac = (212, 312, 716)
def check(tuplename):
    results = []
    for row in tuplename:
        if any(ac in x for x in tuplename):
            results.append(x)
    print(results)
元组中的第三个元素是电话号码,我一直在尝试打印与另一个区号元组匹配的元组,如下所示:

("1", "08:15:25", "17161234567")
ac = (212, 312, 716)
def check(tuplename):
    results = []
    for row in tuplename:
        if any(ac in x for x in tuplename):
            results.append(x)
    print(results)
我编写的代码试图检查这一点,如下所示:

("1", "08:15:25", "17161234567")
ac = (212, 312, 716)
def check(tuplename):
    results = []
    for row in tuplename:
        if any(ac in x for x in tuplename):
            results.append(x)
    print(results)
我读过的另一段代码似乎与列表的对称比较有关,这似乎不是我想要的。输出为空列表:

[]

首先,您需要实际提取区号,即x[1:4] 然后检查一下你的区号元组中是否有

假设:tuplename是这些元组的列表,如下所示:

tuplename = [(1, "08:15:25", "17161234567"), (2, "12:41:08", "12127654321")]
然后,您的函数将被重写为:

def check(tuplename):
    results = []
    for row in tuplename:
        tel = row[2] # extract phone number from tuple
        tuple_ac = tel[1:4] # extract area code from phone number
        if tuple_ac in ac:
            results.append(x)
    print(results)
假设tuplename是一个行序列,并且您希望返回匹配的行,则检查应为:

def check(tuplename):
    return [row for row in tuplename if any(code in row[2] for code in ac)]
这将根据第[2]行(电话号码列)测试ac中的每个项目,生成匹配行列表,并返回列表

如果您觉得列表理解不容易理解,则等效循环版本为:

def check(tuplename):
    results = []
    for row in tuplename:
        if any(code in row[2] for code in ac):
            results.append(row)
    return results

假设您的区号列表实际上是字符串:

rows = [('1', '08:15:25', '17161234567'),
        ('2', '08:15:26', '12121234567'),
        ('3', '08:15:27', '19991234567'),
        ('4', '08:15:28', '13121234567')]
codes = ('212', '312', '716')

for item in (row for row in rows if row[2][1:4] in codes):
    print(item)
输出:

('1', '08:15:25', '17161234567')
('2', '08:15:26', '12121234567')
('4', '08:15:28', '13121234567')

appendx似乎没有缩进对不起,它在我的代码中。我修好了。你的元组包含字符串吗?如果是这样,你需要在它们周围显示引号。08:15:25也不是有效的Python值,因此该项必须是字符串。这里的tuplename是什么,是CSV文件中的一行吗?还是全是排的?如果ac本身就是一个元组,那么x中的ac永远不会起作用。该死,我刚刚读了标题,我猜这是某种元组概念。。。