0

here's my code:

d = ['ant', 'bird', 'gecko', 'snake', 'wasp']
a = ['rabbit panda bird rabbit', 'bird gecko ant panda', 'wasp snake gecko ant']
b = []
for i in range (0, len(a)):
    c = a[i].split()
    for i in d:
        b.append(c.count(i))
print(b)

here's the output:

[0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1]

but i want the desired output like this:

[[0, 1, 0, 0, 0], [1, 1, 1, 0, 0], [1, 0, 1, 1, 1]]
0

7 Answers 7

2

I think this achieves what you want:

d = ['ant', 'bird', 'gecko', 'snake', 'wasp']
a = ['rabbit panda bird rabbit', 'bird gecko ant panda', 'wasp snake gecko ant']
b = []
for i in range (0, len(a)):
    c = a[i].split()
    e = []
    for i in d:
        e.append(c.count(i))
    b.append(e)
print(b)

Note the addition of a local array 'e' which has the contents appended to, this is then appended to your array 'b'

Sign up to request clarification or add additional context in comments.

Comments

1

Use List Comprehension:

d = ['ant', 'bird', 'gecko', 'snake', 'wasp']
a = ['rabbit panda bird rabbit', 'bird gecko ant panda', 'wasp snake gecko ant']

list_of_lists = [[x.split().count(y) for y in d] for x in a]

print list_of_lists

Output:

[[0, 1, 0, 0, 0], [1, 1, 1, 0, 0], [1, 0, 1, 1, 1]]

Comments

1

Using python's list comprehension

d = ['ant', 'bird', 'gecko', 'snake', 'wasp']
a = ['rabbit panda bird rabbit', 'bird gecko ant panda', 'wasp snake gecko ant']
b = []
for i in range (0, len(a)):
    c = a[i].split()
    b.append([c.count(i) for i in d])
print(b) # [[0, 1, 0, 0, 0], [1, 1, 1, 0, 0], [1, 0, 1, 1, 1]]

Comments

1
spam = ['ant', 'bird', 'gecko', 'snake', 'wasp']
eggs = ['rabbit panda bird rabbit', 'bird gecko ant panda', 'wasp snake gecko ant']

print([[int(word in egg.split(' ')) for word in spam] for egg in eggs])

output

[[0, 1, 0, 0, 0], [1, 1, 1, 0, 0], [1, 0, 1, 1, 1]]

Comments

1

Here you go -

d = ['ant', 'bird', 'gecko', 'snake', 'wasp']
a = ['rabbit panda bird rabbit', 'bird gecko ant panda', 'wasp snake gecko ant']
b = []
for i in range (0, len(a)):
    c = a[i].split()
    count_list = [c.count(i) for i in d]
    b.append(count_list)
print(b)

Output - 
[[0, 1, 0, 0, 0], [1, 1, 1, 0, 0], [1, 0, 1, 1, 1]]

Comments

1

The problem with your code is that you are not creating nor adding elements to a nested list. Here's a way to do it adapting your code:

# Start by creating a nsted list the same length of a
b = [[] for _ in range(len(a))]
# [[], [], []]
# Do the same but instead appending the elements to the sublists
# in b using i as index
for i in range (0, len(a)):
    c = a[i].split()
    for j in d:
        b[i].append(c.count(j))
print(b)

# [[0, 1, 0, 0, 0], [1, 1, 1, 0, 0], [1, 0, 1, 1, 1]]

You could also use a nested list comprehension to achieve this:

[[j.split().count(i) for i in d] for j in a]

Output

[[0, 1, 0, 0, 0], [1, 1, 1, 0, 0], [1, 0, 1, 1, 1]]

Comments

1

Other one liner option:

[ [ sub.count(word) for word in d ] for sub in [ string.split() for string in a ] ]
#=> [[0, 1, 0, 0, 0], [1, 1, 1, 0, 0], [1, 0, 1, 1, 1]]

It states for each word in d count its occurrence in sub, where sub is a sub-list derived for each string in a.

[ string.split() for string in a ]
#=> [['rabbit', 'panda', 'bird', 'rabbit'], ['bird', 'gecko', 'ant', 'panda'], ['wasp', 'snake', 'gecko', 'ant']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.