How to concatenate or join list of strings and integer lists

This is the place for queries that don't fit in any of the other categories.

How to concatenate or join list of strings and integer lists

Postby birda » Fri Mar 15, 2013 5:41 pm

I have the following lists:
peace: [ [0, 0, 0, 0, 1, 1, 1]
love: [1, 2, 3, 4, 5, 6, 7]
BA1: ['water', 'water', 'water', 'water','water', 'water' ,'water']
BA2: ['water', 'fire', ''fire', ''fire', ''fire', ''fire', ''fire']

The out put I want is(can be in form of dictionary or list or tuple ....):
BA= [ 'water'[0] : 'water'[1]
'water'[0] : 'fire'[2]
'water'[0] : 'fire'[3]
'water'[0] : 'fire'[4]
'water'[1] : 'fire'[5]
'water'[1] : 'fire'[6]
'water'[1] : 'fire'[7]}
finally renaming, 'water' by W and 'fire' by F:
BA= [ W[0] : W[1]
W[0] : F[2]
W[0] : F[3]
W[0] : F[4]
W[1] : F[5]
W[1] : F[6]
W[1] : F[7] ]

I tried as
BA=zip(zip(BA1, peace) ,zip(BA2,fire))
BA=[(('water',0),('water',1)), (('water',0),('fire',2)),(('water',0),('fire',3)),(('water',0),('fire',4)),(('water',1),('fire',5)),(('water',1),('fire',6)),(('water',1),('fire',7))]
':'.join("water'[x] for x in peace) to get 'water'[0], etc but ;(

any help is appreciated !
Last edited by birda on Sat Mar 16, 2013 10:22 am, edited 1 time in total.
birda
 
Posts: 9
Joined: Thu Mar 14, 2013 4:08 pm

Re: How to concatenate or join list of strings and integer l

Postby Mekire » Sat Mar 16, 2013 4:37 am

Ok. So you kinda got a freebee answer last question but this time you really need to fix up your post with code tags.

Also I'm not quite clear on what you are trying to accomplish. Are you trying to create a dictionary?

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: How to concatenate or join list of strings and integer l

Postby birda » Sat Mar 16, 2013 10:26 am

Hi mek ;), thanks. it can be in any form (dictionary, or list, tuple,....). The thing I am looking is to produce the pairs =>ffor example: W[0] corresponds to F[2].
birda
 
Posts: 9
Joined: Thu Mar 14, 2013 4:08 pm

Re: How to concatenate or join list of strings and integer l

Postby metulburr » Sun Mar 17, 2013 1:36 pm

Please do not post a new thread with essentially the same question. I have deleted it for that reason.
birda wrote:
Any help from python community?

Postby birda » Sun Mar 17, 2013 8:20 am

Dear members please help me to the following concept, since i'm newbie to python.
I have the following lists:
peace: [ [0, 0, 0, 0, 1, 1, 1]
love: [1, 2, 3, 4, 5, 6, 7]
BA1: ['water', 'water', 'water', 'water','water', 'water' ,'water']
BA2: ['water', 'fire', ''fire', ''fire', ''fire', ''fire', ''fire']

The out put I want is(can be in form of dictionary or list or tuple ....):
BA= [ 'water'[0] : 'water'[1]
'water'[0] : 'fire'[2]
'water'[0] : 'fire'[3]
'water'[0] : 'fire'[4]
'water'[1] : 'fire'[5]
'water'[1] : 'fire'[6]
'water'[1] : 'fire'[7]}
finally renaming, 'water' by W and 'fire' by F:
BA= [ W[0] : W[1]
W[0] : F[2]
W[0] : F[3]
W[0] : F[4]
W[1] : F[5]
W[1] : F[6]
W[1] : F[7] ]


any help is appreciated !


Mekire wrote:Also I'm not quite clear on what you are trying to accomplish.

+1
I am confused on what you are trying to do.
There are a couple reasons why:
1) You have no code tags to differentiate code from text, making it harder to see what is what
2) you have either not posted all the code or have errors in your code
Code: Select all
peace: [ [0, 0, 0, 0, 1, 1, 1]

I dont see the closing bracket

or

Code: Select all
BA= [ 'water'[0] : 'water'[1]
'water'[0] : 'fire'[2]
'water'[0] : 'fire'[3]
'water'[0] : 'fire'[4]
'water'[1] : 'fire'[5]
'water'[1] : 'fire'[6]
'water'[1] : 'fire'[7]}

I see the closing } at the end but not the opening one?

3) What is this suppose to mean?
Code: Select all
'water'[0]

because to me i looks like a convoluted way to write a string 'w'

4) this also doesnt make sense?
finally renaming, 'water' by W and 'fire' by F:

rename the string 'water' with a variable W?

5) What variable fire?
Code: Select all
BA=zip(zip(BA1, peace) ,zip(BA2,fire))


So that is a gist of it. The first time you posted i passed by your question because it just confused me. I am sure others have lurked this thread too because of the same reason.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to concatenate or join list of strings and integer l

Postby birda » Mon Mar 18, 2013 11:18 pm

Hi, very sorry for the confusion and syntax errors. thank you for your patience ;)
I guess the following question can solve my problem.

kk=[((0, 1), ('peace', 'fire')), ((5, 9), ('fire', 'world')), ((1, 2), ('fire', 'peace')), ((1, 20), ('fire', 'world')),((7,9),('peace','fire')),((3,6),('world','fire'))]

The output I am looking for is:
[((5, 9), ('fire', 'world')), ((1, 20), ('fire', 'world')),((6,3),('fire','world'))]

NB:Print those which contain ('fire','world'), if ((3,6),('world','fire')) should be changed to ((6,3),('fire','world'))

Many thanks
Birda
birda
 
Posts: 9
Joined: Thu Mar 14, 2013 4:08 pm

Re: How to concatenate or join list of strings and integer l

Postby Yoriz » Tue Mar 19, 2013 12:34 am

Code: Select all
kk = [((0, 1), ('peace', 'fire')),
      ((5, 9), ('fire', 'world')),
      ((1, 2), ('fire', 'peace')),
      ((1, 20), ('fire', 'world')),
      ((7, 9), ('peace', 'fire')),
      ((3, 6), ('world', 'fire'))]

result = []
for one, two in kk:
    if two == ('fire', 'world'):
        result.append((one, two))
    elif two == ('world', 'fire'):
        result.append(((one[1], one[0]), (two[1], two[0])))

print result

[((5, 9), ('fire', 'world')), ((1, 20), ('fire', 'world')), ((6, 3), ('fire', 'world'))]
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 831
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], W3C [Linkcheck] and 4 guests