Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Boolean - Fatal编程技术网

Python 在列表上应用布尔值字符串

Python 在列表上应用布尔值字符串,python,list,boolean,Python,List,Boolean,我有两张单子 第一: ["a<4", "b<3", "c<6"] 我想应用布尔列表将第一个列表变成: ["a<4", "b>3", "c>6"] [“a3”,“c>6”] 根据布尔值列表将较小值更改为较大值 我如何才能做到这一点?您可以尝试: >>> list1 = ["a<4", "b<3", "c<6"] >>> list2 = ["T", "F", "F"] >>> for i

我有两张单子

第一:

["a<4", "b<3", "c<6"]
我想应用布尔列表将第一个列表变成:

["a<4", "b>3", "c>6"]
[“a3”,“c>6”]
根据布尔值列表将较小值更改为较大值

我如何才能做到这一点?

您可以尝试:

>>> list1 = ["a<4", "b<3", "c<6"]
>>> list2 = ["T", "F", "F"]
>>> for index in range(len(list1)):
    if list2[index] == "F":
        temp_data = (list1[index]).replace("<",">")
        list1[index] = temp_data

>>> print list1
['a<4', 'b>3', 'c>6']
>>列表1=[“a您可能需要这个

def transform(statement, corretness):
    if corretness == 'F':
        if '<' in statement:
            return statement.replace('<', '>')
        else:
            return statement.replace('>', '<')
    return statement

statements = ["a<4", "b<3", "c<6"]
correctness = ["T", "F", "F"]

statements = [transform(s, c) for (s, c) in zip(statements, correctness)]
// ['a<4', 'b>3', 'c>6']
def转换(语句,正确性):
如果正确性='F':
如果为“”,则为“6”]

我还没有测试代码。我正在直接向这个编辑器写这篇文章。 我希望这对你有帮助

a = ["a<4", "b<3", "c<6"]
b = ["T", "F", "F"]
newa = list()
for i in range(len(a)):
    if b[i] == 'F':
        if '<' in a[i]:
            newa.append(a[i].replace("<",">"))
        elif '>' in a[i]:
            newa.append(a[i].replace(">","<"))
    else:
        newa.append(a[i])

print newa
只是为了好玩:

[''.join([a, '<>'[(o == "<") ^ (c == "T")], n]) for (a, o, n), c in zip(x, y)]

[''.join([a,”[(o==”我来自R,所以我不喜欢循环和if语句。如果您需要做的只是根据条件从小变大,您可以使用
numpy
轻松地将其矢量化

l1 = ["a<4", "b<3", "c<6"]
l2 = ["T", "F", "F"]

import numpy as np
n1 = np.array(l1)
n2 = np.array(l2) == "F"
n1[n2] = np.char.replace(n1[n2], "<", ">") 
print n1
## ['a<4' 'b>3' 'c>6']

l1=[“比使用字典字典更好吗?如果两个列表的大小相同,那么只需对其进行迭代。(伪代码)
对于列表1中的i:If not boolList[i]:change_element_list1
如果我的评论对你有帮助,那么我会把它们作为答案。让我知道从大到小的变化是什么,或者这不是一个要求?啊哈,这是overkilling@aahung为什么是过度杀戮?一种简单的矢量化,而不是编写循环和if语句?当我使用Python时,我只使用numpy数组因为这使我的代码缩短了10倍。无意冒犯,这确实是一个很好的(优雅的)解决方案。我的意思是,NumPy不是一个预安装的软件包,安装第三个软件包来解决它是一种过度杀伤力。
[''.join([a, '<>'[(o == "<") ^ (c == "T")], n]) for (a, o, n), c in zip(x, y)]
>>> x = ["a<1", "b<2", "c>3", "d>4"]
>>> y = ["T", "F", "F", "T"]
>>> [''.join([a, '<>'[(o == "<") ^ (c == "T")], n]) for (a, o, n), c in zip(x, y)]
['a<1', 'b>2', 'c<3', 'd>4']
l1 = ["a<4", "b<3", "c<6"]
l2 = ["T", "F", "F"]

import numpy as np
n1 = np.array(l1)
n2 = np.array(l2) == "F"
n1[n2] = np.char.replace(n1[n2], "<", ">") 
print n1
## ['a<4' 'b>3' 'c>6']