How to extract informations of a list of tuples and how to write more than one information per line?

python list of tuples
python create list of tuples
tuple immutable python example
python tuple vs list
tuple python 3
dictionary python
tuple inside list python
python tuple methods

I'm wondering how I can resolve this problem... My input is :[(18.41870765673129, 9.511001141278493), (17.013801776758395, 14.63476877634968)] and I would like write with informations of tuple, one file like that:

18.41870765673129, 9.511001141278493
17.013801776758395, 14.63476877634968

I would like have two informations per line. My script:

def write(filename, l):
    with open(filename,"w") as file:
         for i in range(len(l)):
             for j in l[i]:
                 s = str(j)
                 file.write(s)

But my script doesn't work because the ouptut is:

18.418707656731299.51100114127849317.01380177675839514.63476877634968

Thank you!!!

Try this:

def write(filename, l):
    with open(filename,"w") as file:
         for i,j in l:
             file.write('{}, {}\n'.format(i, j))

If you are using Python3.6 or higher, you can change write line to:

file.write(f'{i}, {j}\n')

Lists and Tuples in Python – Real Python, You'll cover the important characteristics of lists and tuples in Python 3. You'll Defining and Using Tuples; Tuple Assignment, Packing, and Unpacking A given object can appear in a list multiple times: For example, a negative list index counts from the end of the list: Information on these methods is detailed below. Depending on the application, the danger of concurrent operations may be more or less than the danger posed by other factors such as file permissions. The developer would have to know more about the particular application being developed and its expected environment before choosing an implementation.

Try

def write(filename, l):
    with open(filename, "w") as file:
         for t in l:
             file.write(', '.join([str(s) for s in t]))
             file.write('\n')

Note: it's not necessary to get the length of the list:

for i in range(len(l)):

just get all tuples inside of l

for t in l:

The command below takes all parts of the tuple and converts them to string. The strings are then joined.

', '.join([str(s) for s in t])

Sequence Containers: Lists and Tuples, write Lists and Tuples. access the individual elements in a sequence with indexing. line 1, in <module> TypeError: can only concatenate list (not "tuple") to list can still index and slice them to extract individual elements or sub-​sequences). If the same value occurs more than once, only the first occurrence is deleted. Lists and tuples are arguably Python’s most versatile, useful data types.You will find them in virtually every nontrivial Python program. Here’s what you’ll learn in this tutorial: You’ll cover the important characteristics of lists and tuples.

Try this...

for i in range(len(l)):
    for j in range(len(l[i])):
        if j < len(l[i]) - 1:
            s = s + str(l[i][j]) +", "
        else:
            s = s + str(l[i][j]) +"\n"
        file.write(s)

[PDF] Python course in Bioinformatics, Functional programming or more about lists . or to load a file from the command line before entering Python in interactive mode (-i):. 1 General informations about Python and BioPython can be found: An appropriate use of tuples in a biological example could be the Extracting information from alignments. Example  Lists are more common than tuples, mostly because they are mutable. But there are a few cases where you might prefer tuples: In some contexts, like a return statement, it is syntactically simpler to create a tuple than a list. In other contexts, you might prefer a list.

9. Tuples, This is an example of a data structure — a mechanism for grouping and variable to reference a new tuple that used some information from the old one: This does the equivalent of seven assignment statements, all on one easy line. the values in a tuple on the right are 'unpacked' into the variables/names on the right:. Lists are more common than tuples, mostly because they are mutable. But there are a few cases where you might prefer tuples: In some contexts, like a return statement, it is syntactically simpler to create a tuple than a list. In other contexts, you might prefer a list.

3. Strings, lists, and tuples, A list containing no elements is called an empty list, and a tuple with no elements The first example is a list of five integers, and the next is a list of three strings. The indexing operator ( [ ] ) selects a single element from a sequence. (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not​  The most common use of for loops is to extract information from a list. A list, as you might know, is basically a collection of items or data. A list is essentially just one of the six different types of sequences used in Python. For this tutorial, however, we’ll focus only on two types of sequences: lists and tuples.

Python: TypeError: unhashable type: 'list', The standard way to solve this issue is to cast a list to a tuple. This means that when you try to hash an unhashable object it will result an error. an heterogeneous sequence of elements that are accessed via unpacking or are hashable and they have a single unique value that will never change. More Source Code :. Line 5: Line 5 also has an error! Extract all lines containing substring, using regex. The program below is similar to the above program, but using the re regular expressions module. The errors and line numbers are stored as tuples, e.g., (linenum, line). The tuple is created by the additional enclosing parentheses in the errors.append

Comments
  • No problem, If you find my answer helpful, please check the gray button beside.
  • I just suggest to OP avoiding a 'write' name for the function, as file objects have themselves a 'write' method and similarities can get confusing. Try save or something else. Also wt and encoding='utf-8' in the open call help readability and prevent different file treatments on different platforms.
  • Don't you need a newline character anywhere?
  • Yes, needed to add that
  • Thank you but I would like a file like that:
  • map(str, t) instead of [str(s) for s in t] will be better