1) Is there any way to split into ?


2)
db = [["a", "b", "c"], ["apple", "bean", "cat"]]

lines = sys.stdin.readlines()

for i in range(len(lines)):
lines = lines[:-1]

for i in range(len(lines)):
for j in range(len(db[0])):
if lines == db[0][j]:
print db[1][j]
else:
print lines
If I input:
a
b
c
abc

the output of the program will be:
apple
a
a
a
b
bean
b
b
c
c
cat
c
abc
abc
abc
abc

What I want is:
apple
bean
cat
abc

I think this problem is caused by the nested-loops. However, I do not know how to fix it.
Please help!

Recommended Answers

All 4 Replies

You should put code-tags to keep the white space in your program. Even with reply the white space is not there. After fixing indention and trying to run the code including import sys I got error

File "J:/python27/loop_problem.py", line 4, in <module>
    lines = sys.stdin.readlines()
AttributeError: readlines

1) Is there any way to split into ?

>>> l[0].split() + l[1].split()
['a', 'b', 'c']

As postet over,repost your code with code tag.
And include the error message you get called(Traceback)

I got it running by directly running it without IDLE which redirects the stdin.

But maybe you are trying to do something like this?

import sys
items = ["apple", "bean", "cat"]
db = dict((item[0],item) for item in items)
print db
request = 'n/a'
while request:
    request = raw_input()
    print db.get(request, request)

You would also have to test for the length of each item:

db = [["a", "b", "c"], ["apple", "bean", "cat"], ['d', 'e', 'f']]
for each_list in db:
    output_list = []
    for item in each_list:
        if len(item) > 1:
            print item
        else:
            output_list.append(item)
    if len(output_list):
        print "".join(output_list)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.