Python3 preventing nested list and show position/index of values in list
Python3 preventing nested list and show position/index of values in list
As the title suggest, i'm getting a nested list from splitting twice.
" ".join didn't help with this problem.
I also can't figure out how to print position of a word in the list.
The goal is to split id user text words on tab(which works) and print the words with their position in a list.
import sys
def main():
olist =
for line in sys.stdin:
i,u,t,w = line.split('t')
olist.append(w.split())
print(olist)
for num,name in enumerate(olist):
print(num, name)
output print(olist):
[['RT', '@AfshinEllian1', ':', 'Kennelijk', 'vinden', 'ze', 'daar', 'aan', 'die', 'gezellige', 'tafel', 'normaal', 'dat', 'steltje', 'barbaren', 'onze', 'grondwettelijke', 'rechten', 'bedreigen', '.', 'Zouden', 'we', 'ook', 'voor', 'andere', 'buitenwettelijke', 'dreigingen', 'moeten', 'capituleren', '?', 'Wat', 'een', 'door', 'ons', 'gesubsidieerde', 'domheid', '!', '#laatop1', '#cartoonwedstrijd']]
output print(num, name):
0 ['RT', '@AfshinEllian1', ':', 'Kennelijk', 'vinden', 'ze', 'daar', 'aan', 'die', 'gezellige', 'tafel', 'normaal', 'dat', 'steltje', 'barbaren', 'onze', 'grondwettelijke', 'rechten', 'bedreigen', '.', 'Zouden', 'we', 'ook', 'voor', 'andere', 'buitenwettelijke', 'dreigingen', 'moeten', 'capituleren', '?', 'Wat', 'een', 'door', 'ons', 'gesubsidieerde', 'domheid', '!', '#laatop1', '#cartoonwedstrijd']
Desired output =
RT [0]
@AfshinEllian1 [1]
: [2]
Kennelijk [3]
etc
Since there's only one item in your nesting, why not simply switch to
enumerate(olist[0])
? We're not sure what output you expect from this, and you neglected to supply proper input.– Prune
Sep 13 '18 at 18:30
enumerate(olist[0])
Added desired output
– stefan lakin
Sep 13 '18 at 18:32
2 Answers
2
If I understand your problem correctly, this line:
olist.append(w.split())
is not doing what you want. You need to extend the list instead:
olist.extend(w.split())
which joins two lists.
Thanks, this is exactly what i was looking for
– stefan lakin
Sep 13 '18 at 18:43
There is no need to split it twice, simply print it directly via:
import sys
def main():
for line in sys.stdin:
for num, name in enumerate(line.split("t")):
print(" []".format(name.strip(), num))
main()
# next line is input line
This is a test
# and the output for it ...
This [0]
is [1]
a [2]
test [3]
If you want to have ongoing numbering, you can also use this code. Here you can simply use the +
-operator which will concat your lists which is probably what you want.
+
def main():
olist =
for line in sys.stdin:
olist += [name for name in line.split("t")]
for num, name in enumerate(olist):
print(" []".format(name.strip(), num))
The used .strip()
-method is simply cosmetic, because sys.stdin will also catch line separator.
.strip()
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you please show some example of your desired output?
– Teoretic
Sep 13 '18 at 18:29