Adding words between lines to an array

Related searches

This is the content of my file:

david    C001 C002 C004 C005 C006 C007

*    C008 C009 C010 C011 C016 C017 C018

*    C019 C020 C021 C022 C023 C024 C025

anna C500 C521 C523 C547 C555 C556

*    C557 C559 C562 C563 C566 C567 C568

*    C569 C571 C572 C573 C574 C575 C576

*    C578

charlie    C701 C702 C704 C706 C707 C708

*    C709 C712 C715 C716 C717 C718

I want my output to be:

david=[C001,C002,C004,C005,C006,C007,C008,C009,C010,C011,C016,C017,C018,C019,C020,C021,C022,C023,C024,C025]

anna=[C500,C521,C523,C547,C555,C556,C557,C559,C562,C563,C566,C567,C568,C569,C571,C572,C573,C574,C575,C576,C578]

charlie=[C701,C702,C704,C706,C707,C708,C709,C712,C715,C716,C717,C718]

I am able to create:

david=[C001,C002,C004,C005,C006,C007]
anna=[C500,C521,C523,C547,C555,C556]
charlie=[C701,C702,C704,C706,C707,C708]

counting the number of words in a line and using line[0] as the array name and adding the remaining words to the array. However, I don't know how to take the continuation of words in the next lines starting with "*" to the array.

Can anyone help?

NOTE: This solution relies on defaultdict being ordered, which is something that was introduced on Python 3.6

Somewhat naive approach:

from collections import defaultdict

# Create a dictionary of people
people = defaultdict(list)

# Open up your file in read-only mode
with open('your_file.txt', 'r') as f:
    # Iterate over all lines, stripping them and splitting them into words
    for line in filter(bool, map(str.split, map(str.strip, f))):
        # Retrieve the name of the person
        # either from the current line or use the name of the last person processed
        name, words = list(people)[-1] if line[0] == '*' else line[0], line[1:]
        # Add all remaining words to that person's record
        people[name].extend(words)


print(people['anna'])
# ['C500', 'C521', 'C523', 'C547', 'C555', 'C556', 'C557', 'C559', 'C562', 'C563', 'C566', 'C567', 'C568', 'C569', 'C571', 'C572', 'C573', 'C574', 'C575', 'C576', 'C578']

It also has the additional benefit of returning an empty list for unknown names:

print(people['matt'])
# []

Setting Up an Array with Fields (Microsoft Word), This tip demonstrates how to insert these arrays in your document using the particularly if you literally want the array in-line within your sentence. With the insertion point still within the field (between the braces), press F9. An empty string to represent the line we will add to our lines array; The difference or the number of words between the first and last word in the line; let line = ""; let difference = last - index - 1; Like the question said, if we're on the last line or if there is only one word in the line we will left justify.

You could read the lists into a dictionary using regular expressions:

import re
with open('file_name') as file:
    contents = file.read()
res_list = re.findall(r"[a-z]+\s+[^a-z]+",contents)
res_dict = {}
for p in res_list:
    elt = p.split()
    res_dict[elt[0]] = [e for e in elt[1:] if e != '*']

print(res_dict)

Reading lines from a text file and storing them into an array , After putting lines into a cell array, I use a regular expression to pick out which lines were equations and which were not. fid=fopen('LUT.txt');. There are several methods for adding new elements to a JavaScript array. If you instead want to remove elements from an array in JavaScript, we’ve got an article for that too. Feel free to click the links below to jump ahead to the explanation of each method: Using push() to add to the end of […]

I figured out a way myself. Thanks to the ones who gave their own solution. It gave me new perspective.

Below is my code:

persons_library={}
persons=['david','anna','charlie']
for i,person in enumerate(persons,start=0):
    persons_library[person]=[]

with open('data.txt','r') as f:
    for line in f:
        line=line.replace('*',"")
        line=line.split()
        for i,val in enumerate(line,start=0):
            if val in persons_library:
                key=val
            else:
                persons_library[key].append(val)
    print(persons_library)

Split strings at newline characters - MATLAB splitlines, Split str at the newline character. newStr is a 1-by-2 string array. Each element contains one line of the text. newStr = splitlines(� Add words from different cells together with formulas. You can easily add words from different cells together with formulas. Please do as follows. 1. Please select a blank cell for locating the added words, enter formula =A2&B2&C2 into the Formula Bar, and then press the Enter key.

Solving The Problem: Text Justification, An empty string to represent the line we will add to our lines array; The difference or the number of words between the first and last word in the line. let line = ""� Use the Shapes Menu to Insert a Line in Word . A third way to add a line to a Word document is to draw it on the page. The Shapes menu contains several line options, including lines with arrow points on one or both ends. After you draw the line, customize the color and appearance.

Reading a file line by line, Getting input data into a program from the command line, or from questions and and even Word processors such as LibreOffice or Microsoft Word can also be used each line ( number ) as a string, not a float or int that we can add to other numbers. Note the difference between just writing filestr and writing print filestr . Hence in order to add an element in the array, one of the following methods can be done: By creating a new array: Create a new array of size n+1, where n is the size of the original array. Add the n elements of the original array in this array. Add the new element in the n+1 th position. Print the new array.

What you need is a 2 dimensional array of chars. Char array_name[4][length of words]. Because "first" is already array of chars. – Aleksandar Makragić Jan 21 '16 at 13:59 @Arc676 his question is so basic, you're answer will only confuse him more :) – Aleksandar Makragić Jan 21 '16 at 14:01

Comments
  • Will it always be these three names? Please provide the code you came up with so far so answers can use it as a base.
  • How would you go about deciding in your head which person the line of text belongs to? The python solution is the exact same
  • you want your output in a text file, right?
  • you can use david.extend('* C008 C009 C010 C011 C016 C017 C018'.strip('*').split() if line starts with *