python -regular expression - list element
Hello, I am a beginner in Python and am not able to use a list element for regular expression, substitutions. list1 = [ 'a', 'o' ] list2 = ['star', 'day', 'work', 'hello'] Suppose that I want to substitute the vowels from list2 that are in list1, into for example 'u'. In my substitution, I should use the elements in list1 as a variable. I thought about: for x in list1: re.compile(x) for y in list2: re.compile(y) if x in y: z = re.sub(x, 'u', y) but this does not work -- http://mail.python.org/mailman/listinfo/python-list
reading from list with paths
Hello, Suppose this is a stupid question, but as a python beginner I encounter a lot of obstacles... so I would be very grateful with some help for following question: I would like to read files, of which the complete filepaths are mentioned in another textfile. In this textfile (list.txt) are for example the following paths: /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_1LG_f01.TextGrid /data/chorec/chorec-nieuw/s01/S01C001M1/ S01C001M1_1LGPseudo_f01.TextGrid /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_AVI1_f01.TextGrid I know how to open and read one file in my current directory, but after trying to find this out my self, I give up... So could someone help me and write some code so that I know how to open and read the content of the mentioned files. Thanks a lot Antar2 -- http://mail.python.org/mailman/listinfo/python-list
reading from list with paths
Hello, I would like to read and print files, of which the complete filepaths are mentioned in another textfile. In this textfile (list.txt) are for example the following paths: /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_1LG_f01.TextGrid /data/chorec/chorec-nieuw/s01/S01C001M1/ S01C001M1_1LGPseudo_f01.TextGrid /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_AVI1_f01.TextGrid I know how to open and read one file in my current directory, but after trying to find this out my self, I give up... I already got one answer for this question, but it did not work Thanks a lot Antar2 -- http://mail.python.org/mailman/listinfo/python-list
list previous or following list elements
Hello Suppose I have a textfile (text1.txt) with following four words: Apple balcony cartridge damned paper bold typewriter and I want to have a python script that prints the words following the word starting with the letter b (which would be cartridge) or differently put, a script that prints the element following a specified element: I am more experienced in Perl, and a beginner in python I wrote a script that - of course - does not work, that should print the element in a list following the element that starts with a b import re f = open('text1.txt', 'r') list1 = [] list2 = [] for line in f: list1.append(line) a = re.compile("^b") int = 0 while(int <= list1[-1]): int = int + 1 a_match = a.search(list1[int]) if(a_match): list2.append(list1[int + 1]) print list2 I did not find information about addressing previous or following list elements. So some help would be magnificent Thanks a lot -- http://mail.python.org/mailman/listinfo/python-list
delete lines
I am new in python and I have the following problem: Suppose I have a list with words of which I want to remove each time the words in the lines below item1 and above item2: item1 a b item2 c d item3 e f item4 g h item1 i j item2 k l item3 m n item4 o p I did not find out how to do this: Part of my script was f = re.compile("item\[1\]\:") g = re.compile("item\[2\]\:") for i, line in enumerate(list1): f_match = f.search(line) g_match = g.search(line) if f_match: if g_match: if list1[i] > f_match: if list1[i] < g_match: del list1[i] But this does not work If someone can help me, thanks! -- http://mail.python.org/mailman/listinfo/python-list
start reading from certain line
I am a starter in python and would like to write a program that reads lines starting with a line that contains a certain word. For example the program starts reading the program when a line is encountered that contains 'item 1' The weather is nice Item 1 We will go to the seaside ... Only the lines coming after Item 1 should be read Thanks! -- http://mail.python.org/mailman/listinfo/python-list
read file into list of lists
Hello, I can not find out how to read a file into a list of lists. I know how to split a text into a list sentences = line.split(\n) following text for example should be considered as a list of lists (3 columns and 3 rows), so that when I make the print statement list[0] [0], that the word pear appears pear noun singular books nouns plural table noun singular Can someone help me? Thanks -- http://mail.python.org/mailman/listinfo/python-list
common elements between list of lists and lists
Hello, I am a beginner in python. following program prints the second element in list of lists 4 for the first elements in list 4 that are common with the elements in list 5 list4 = [['1', 'a'],['4', 'd'],['8', 'g']] list5 = ['1', '2', '3'] for j in list4: for k in list5: if j[0] == k: print j[1] Result: a I would like to do the same thing starting with following lists, where the numbers in list 5 are without ''. Is there a way to convert integers in a list to integers in '' ? This is based on a situation where I want to find common numbers between a list and a list of lists where the numbers in the list are without '' and the numbers in the list of lists are with '' list4 = [['1', 'a'],['4', 'd'],['8', 'g']] list5 = [1, 2, 3] This might be a stupid question, but anyway, thanks for your answer It is not my first post on this site. In some way it is not possible to react on the messages that I receive to thank the persons that react. Anyway, thanks a lot -- http://mail.python.org/mailman/listinfo/python-list
substitution of list elements
I want to replace each first element in list 5 that is equal to the first element of the list of lists4 by the fourth element. I wrote following code that does not work: list4 = [['1', 'a', 'b', 'c'], ['2', 'd', 't', 'e'], ['8', 'g', 'q', 'f']] list5 = ['1', '2', '3'] for j in list4: for k in list5: if j[0] == k: k = j[3] print list5 Wanted result: ['c', 'e', '3'] thanks! -- http://mail.python.org/mailman/listinfo/python-list
convert list of lists to list
Is there a way to convert list_of_listsA to list_of_listsB, where one list in listof lists A is one element of listB? list_of_listsA: [['klas*', '*', '*'], ['mooi*', '*', '*', '*'], ['koe'], ['arm*', '*', '*(haar)'], ['groei*', '*', '*', '*', '*']] listB: ['klas* * *', 'mooi* * * *, 'koe', 'arm* * * (haar)', 'groei* * * * *'] Thankx! -- http://mail.python.org/mailman/listinfo/python-list
concatenate the elements in each list of a list of lists
I already asked a similar question, but encounter problems with python... How can I concatenate the elements in each list of a list of lists list_of_listsA = [['klas*', '*', '*'], ['mooi*', '*', '*', '*'], ['arm*', '*', '*(haar)']] wanted result: list_of_listsA = [['klas* * *'] ['mooi* * * *'] ['arm* * *(haar)']] Thanks a lot ! -- http://mail.python.org/mailman/listinfo/python-list