0

I have a list like this

[796, 829, 1159, 1162]

I also have a list like this:

['144 154', '145 151', '145 152', '145 153', '145 154', '146 152', '146 153', '146 154', '147 153', '147 154'] These are not to scale

What I want to do is use the first lists elements as index for the last array

I have tried this piece of code:

contacts = []
for i in findres:
 contacts += rulelines[i]
print contacts

where findres is the first list and rulelines is the last list However this prints the contacts list out weirdly:

['5', ' ', '7', '2', '5', ' ', '1', '0', '5', '7', ' ', '1', '5', '0', '7', ' ', '1', '5', '3']

I'm sure its easy but where am I going wrong??

The desirable output I believe is ['5 72','5 105', '7 150',7 153']

I have not put down all of the list elements as there are over 100 elements in each

asked Apr 24, 2013 at 18:02
4
  • 4
    Can you tell me what your desirable output is? Commented Apr 24, 2013 at 18:05
  • 1
    Well, if the first list is findres and the second list is rulelines, then that won't produce the output you've given. Commented Apr 24, 2013 at 18:07
  • Can you include rulelines and findres in your code as well ? Commented Apr 24, 2013 at 18:10
  • Are you sure findres indexes are inside rulelines boundaries? Commented Apr 24, 2013 at 18:14

3 Answers 3

2

Looks like when you assign contacts = rulelines[i] you're actually assigning the rulelines[i] string. You should do contacts.append(rulelines[i]) to add the the contact to the list, otherwise you're constantly overwriting over the last assignment.

answered Apr 24, 2013 at 18:06
Sign up to request clarification or add additional context in comments.

Comments

1

Use this as a template:

findres = [5, 7, 15, 22]
contacts = list('abcdefghijklmnopqrstuvwxyz') # dummy list
result = [ contacts[index] for index in findres ]
print result
# ['f', 'h', 'p', 'w']
answered Apr 24, 2013 at 18:07

Comments

0

If I understand correctly, you need to extract the first number in every elements of findres first. Then, use those extracted numbers as an index for another array

>>> findres = ['144 154', '145 151', '145 152', '145 153', '145 154', '146 152', '146 153', '146 154', '147 153', '147 154']
>>> first_elements = [c.split()[0] for c in findres]
>>> print first_elements 
['144', '145', '145', '145', '145', '146', '146', '146', '147', '147']
>>> contact = []
>>> for i in first_elements:
 contacts.append(rulelines[i])
answered Apr 24, 2013 at 18:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.