tags:

views:

42

answers:

2

I have two long lists. One is names....John, Paul, Gorge, and Ringo ect x 50,000 each guy has a number. The numbers are after all the names in order. SO its like 45, 32, 22, 65. 32 is Pual. 45 is John ect. How can i format this so that each name goes with the number.

Thanks

string

+1  A: 

Easiest way is to split each string into an array; most languages have a split() function that splits a string on a given delimiter.

String[] nameArray = nameList.split(",");
String[] numberArray = numberList.split(",");

Now you have two arrays, where numberArray[247] contains the number that goes with the name in nameArray[247] (for example).

So you could, for example, loop through them:

for (int i=0; i<nameArray.length; i++) {
   println(nameArray[i] + " : " + numberArray[i];
}

It may be different depending on what language you're using, of course. The above examples are java-ish; in ruby the equivalent could be something like

Hash[*nameArray.zip(numberArray).flatten].each {|key, value| puts "#{key} : #{value}" }

EDITED TO ADD: Here's an example using a space delimiter, and doing it in Python (disclaimer: I don't know Python particularly):

names = "John Paul George Ringo"
numbers = "11 12 13 14"
nameArray = names.split(" ")
numberArray = numbers.split(" ")
for i in range(len(nameArray)):
    print nameArray[i] + ": " + numberArray[i]
JacobM
I need to do it in python
novak
Also my list looks like this John Paul Goorge Ringo Names Names Names 43 54 65 23 12 numbers numbers numbers
novak
Can you show me how do to that in python?
novak
+1  A: 

Assuming you want to turn it into a dictionary and you're starting with a python list (not a string used as a list):

base_list = ["John", "Paul", "Joe", 4, 5, 6]
names = []
numbers = []

for item in base_list:
    if isinstance(item, int): 
        # Checks if the item is an instance of int.
        # If it's a string, you can use the string's 'isdigit' function instead
        numbers.append(item)
    else: 
        names.append(item)

nameNumber = {}

for i in range(len(names)):
    # Iterate through a range of numbers from 0 to the size of names
    nameNumber[names[i]] = numbers[i]

This outputs {'Paul': 5, 'John': 4, 'Joe': 6}

If you just want to reorder your list so it goes name then number you can change the last few lines like so:

nameNumber = []

for i in range(len(names)):
    nameNumber.append(names[i])
    nameNumber.append(numbers[i])
thegrinner