Python : What is a string array? [closed]

Python : What is a string array? [closed]



So I'm doing this coding challenge in Python and the problem says that the input is a string array. I downloaded a sample input, and the .txt file is like this:


string array


.txt


13
Alice;START
Bob;START
Bob;1
Carson;START
Alice;15
Carson;6
David;START
David;24
Evil;START
Evil;24
Evil;START
Evil;18
Fiona;START



Honestly, I don't know how to process that input, so I failed the challenge badly. However, I'm still interested in learning.



I tried to process the input using split(";").


split(";")



Any ideas how to parse this input?



This question appears to be off-topic. The users who voted to close gave this specific reason:





"arrays" aren't really a thing in Python (I mean sure, they are, but you don't use them because lists are so much nicer). What was the output supposed to be?
– Adam Smith
Sep 3 at 19:59





The output should be 11;Evil;SHORTENED_JOB, because Evil started a job with ID less than the previous started job (IDs are incremental), where 11 is the line number
– Bruno Cortez
Sep 3 at 20:03



11;Evil;SHORTENED_JOB


Evil


11





The input is a file containing text; read the file contents into relevant internal data-structures. The use of "string array" in any instructions, while unfortunate, does not imply an implementation and would be better conveyed as "n lines, as specified by the first line, where each line is a semi-colon separated pairs of.. etc etc" - solutions will use appropriate data-structures per the specific language.
– user2864740
Sep 3 at 20:03





2 Answers
2


with open("your_file.txt") as f:
array = [line.rstrip().split(";") for line in f]



The explanation:



The first line is a safe way to open a text file. (The file will be automatically closed after the work).



f at the end of that line becomes the name of the iterable object - with every iteration we obtain the full next line from the opened text file.


f



Now we use that object for creating the list by a List Comprehension (the second line) - see the end of that line:


for line in f]



Because the line contains the full line, i. e. with trailing newline (n) symbol, we use the rstrip() method to trim it out:


line


n


rstrip()


[line.rstrip() for line in f]



And finally we split every such string into a list (using the delimiter symbol ;)


;


[line.rstrip().split(";") for line in f]



and assign the result to the variable array:


array


array = [line.rstrip().split(";") for line in f]



The test:


from pprint import pprint
pprint(array)



The output:


[['13'],
['Alice', 'START'],
['Bob', 'START'],
['Bob', '1'],
['Carson', 'START'],
['Alice', '15'],
['Carson', '6'],
['David', 'START'],
['David', '24'],
['Evil', 'START'],
['Evil', '24'],
['Evil', 'START'],
['Evil', '18'],
['Fiona', 'START']]



Note:



The first line of your text file contains the number 13 - it is probably the number of remaining lines. If you want ignore it, just append the [1:] to the second line of code:


[1:]


with open("your_file.txt") as f:
array = [line.rstrip().split(";") for line in f][1:]



There is no enough details but I would assume that the first line is the number of examples and the following lines contain the tuples which are, in fact, the string array you mentioned.



I would go the following way:


with open ("input.txt") as f:
no_examples = f.readline().strip()
array =
for i in range(no_examples):
example = f.readline().strip().split(';')
array.append(example)



The assignment to example variable strips the invisible line end characters and splits the result by the separator ;.


example


;

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?