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 = …

Total answers: 5

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: …

Total answers: 2

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 …

Total answers: 2

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, …

Total answers: 1

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[1] = ‘y’ TypeError: ‘tuple’ object does not support item assignment My code is: my_tuple …

Total answers: 6

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)) …

Total answers: 3

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 …

Total answers: 2

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 …

Total answers: 2

List of tuples to DataFrame w. column for elements, column for tuple length

List of tuples to DataFrame w. column for elements, column for tuple length Question: I have a list of tuples of different lenghts, where the tuples can be thought to encode teams of people, such as: data = [(‘Alice’,), (‘Bob’, ‘Betty’), (‘Charlie’, ‘Cindy’, ‘Cramer’)] From this, I would like to create a DataFrame with a …

Total answers: 3

How to fetch data in Python from SQL as a list of strings instead of tuples?

How to fetch data in Python from SQL as a list of strings instead of tuples? Question: I’m trying to get the columns names of an SQLite table, but I’m getting back a list of tuples with the names. cur.execute(”’SELECT name FROM PRAGMA_TABLE_INFO(‘Emails’)”’) .fetchall()) If I print this the output is: [(’email’,), (‘count’,)] How can …

Total answers: 1