# tuples

## Generate all combinations of positive and negative

Generate all combinations of positive and negative Question: I have this list of tuples a = [(1, 2), (2, 1)] And need all of the possible combinations of negative and positive: b = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)] I’ve tried using itertools.product(): xs = …

## Make set keep order of a tuple

Make set keep order of a tuple Question: In the code below, how can I use keep using set to have unique values, but also make set keep the order of how the filters are typed in? >>> filters = (‘f1’, ‘f2’) >>> set((*filters, ‘f3’)) {‘f1’, ‘f3’, ‘f2’} # expected: {‘f1’, ‘f2’, ‘f3’} Asked By: …

## How to store a tuple of floats into a file, open and read it to extract the mean per column?

How to store a tuple of floats into a file, open and read it to extract the mean per column? Question: I am computing the following scores iteratively which generates a new set of the following scores in each iteration: add_score, keep_score, del_score = get_corpus_sari_operation_scores(sources, prediction, references) I first want to store them into a …

## Extract tuple elements in list

Extract tuple elements in list Question: my_tuples = [(1, 2, 3), (‘a’, ‘b’, ‘c’, ‘d’, ‘e’), (True, False), ‘qwerty’] For this tuple, how can I append all values in list and dictionary. For example, I want output like list1 = [1,2,3,a,b,c] so on and for dictionary I want output like{1:1,2:2,3:3,4:a}. Thanks in advance. For list, …

## Why am I getting an error when I am trying to change a tuple

Why am I getting an error when I am trying to change a tuple Question: I am trying to change a tuple but, it keeps giving me this error: Traceback (most recent call last): File "main.py", line 2, in <module> my_tuple = ‘y’ TypeError: ‘tuple’ object does not support item assignment My code is: my_tuple …

## create tuple from ranges in python

create tuple from ranges in python Question: Is there a way in python to create a tuple of months and years (as: [(2020, 1),(2020,2)…] ) by using the tuple() function? My code: monthyears = [] for y in range(2020,2017,-1): if y == 2018: m_end = 6 else: m_end = 0 for m in range(12,m_end,-1): monthyears.append((y,m)) …

## How to substitute a list of words based on a list of tuples by preserving the same original order?

How to substitute a list of words based on a list of tuples by preserving the same original order? Question: Given the following list of tokens: a = [‘heyyo’, ‘how’, ‘ale’, ‘yiou’] And a list of tuples: b = [(‘yiou’, ‘you’), (‘heyyo’, ‘hello’)] How can I replace the elements of the list a considering the …

## substracting list of tuples from another list of tuples in python

substracting list of tuples from another list of tuples in python Question: I have two lists of tuples but imagine long lists of tuples and the idea is to get these two conditions tuples that are only in list2 get rid off of individul tuples where the first element of tuples in list1 match the …