Re: [Tutor] Read from large text file, find string save 1st string of each line where it appeared.
On 29Dec2015 03:12, Steven D'Aprano wrote: On Mon, Dec 28, 2015 at 04:50:05PM +0530, sutanu bhattacharya wrote: suppose 61746245 is my searching string. so o/p will be [...] I don't understand the question. What is "o/p"? "output" Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read from large text file, find string save 1st string of each line where it appeared.
Hi Sutanu, On 28 December 2015 at 11:20, sutanu bhattacharya < totaibhattacha...@gmail.com> wrote: > {'115160371': [45349980, 22477811, 40566595, 26947037, 16178191, 12984002, > 20087719, 19771564, 61746245, 17467721, 32233776, 31052980, 70768904, > 16113331, 12414642]} > > suppose 61746245 is my searching string. so o/p will be > 115160371 (1st string). Area in between third bracket > ([ > ]) is the searching area... > We are not mind readers, and as others have said, you need to provide more of a description of what you're trying to accomplish and what version of Python, OS etc you are using. But, assuming Windows, Python 2.x, and assuming what described as "searching a string" is in fact more of a looking up id's in lists of id's held as the values in a Python dict, then simplistically/directly you could do something as follows: example.py--- friendsmap1 = { 115160371: [45349980, 22477811, 40566595, 26947037, 16178191, 12984002, 20087719, 19771564, 61746245, 17467721, 32233776, 31052980, 70768904, 16113331, 12414642], 45349980: [22477811, 40566595, 26947037, 16178191], 16178191: [61746245, 17467721, 32233776, 31052980], 31052980: [22477811, 40566595, 32233776, 31052980] } friendsmap2 = { 16178191: [61746245, 17467721, 32233776, 31052980], 31052980: [22477811, 40566595, 32233776, 31052980] } def friendswith(friendsmap, friendid): res = [key for key, value in friendsmap.items() if friendid in value] return res # Examples: print friendswith(friendsmap1, 61746245) print friendswith(friendsmap1, 26947037) print friendswith(friendsmap2, 61746245) example.py--- output--- [115160371, 16178191] [115160371, 45349980] [16178191] Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Read from large text file, find string save 1st string of each line where it appeared.
I think what he's looking for is something similar to grep 6174625 | awk -F ":" {print $1} I don't know if there is a more efficient Python built-in used to search for the line containing 6174625 (grep in python) other than simply iterating though the entire file, with a for loop, line-by-line. You'd also need to decide whether you want to print all lines containing the searched-for string or just the first occurence. You can then use the split method to return the first string on the line, using the semi-colon as the delimiter. for line in open("file.txt"): if "6174625" in line: return line.split(":")[0] On Mon, Dec 28, 2015 at 6:20 AM, sutanu bhattacharya < totaibhattacha...@gmail.com> wrote: > {'115160371': [45349980, 22477811, 40566595, 26947037, 16178191, 12984002, > 20087719, 19771564, 61746245, 17467721, 32233776, 31052980, 70768904, > 16113331, 12414642]} > > suppose 61746245 is my searching string. so o/p will be > 115160371 (1st string). Area in between third bracket > ([ > ]) is the searching area... > > > kindly help me to solve this problem.. > > -- > Sutanu Bhattacharya > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > def problem(6174625): return 115160371 You haven't really described your problem, so above is a solution for what you asked. What have you tried so far, and what was your result? -- Joel Goldstick http://joelgoldstick.com/stats/birthdays ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read from large text file, find string save 1st string of each line where it appeared.
On Mon, Dec 28, 2015 at 04:50:05PM +0530, sutanu bhattacharya wrote: > {'115160371': [45349980, 22477811, 40566595, 26947037, 16178191, 12984002, > 20087719, 19771564, 61746245, 17467721, 32233776, 31052980, 70768904, > 16113331, 12414642]} > > suppose 61746245 is my searching string. so o/p will be > 115160371 (1st string). Area in between third bracket ([ > ]) is the searching area... I don't understand the question. What is "o/p"? What do you mean, "searching string"? The string you are searching *for*, or the string you are searching *in*? You have something that looks like a dictionary { } followed by an email address. What does that mean? If you expect any useful answers, you will have to give a more careful question. Please show some sample data, and expected result, using valid Python syntax. For example: text = """This is a large string. It contains many lines of text. And some numbers: 22477811, 40566595, 26947037 And more numbers: 32233776, 31052980, 70768904 And lots more text. """ target = "233" expected result: "32233776" -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read from large text file, find string save 1st string of each line where it appeared.
On 28/12/2015 11:20, sutanu bhattacharya wrote: {'115160371': [45349980, 22477811, 40566595, 26947037, 16178191, 12984002, 20087719, 19771564, 61746245, 17467721, 32233776, 31052980, 70768904, 16113331, 12414642]} suppose 61746245 is my searching string. so o/p will be 115160371 (1st string). Area in between third bracket ([ ]) is the searching area... kindly help me to solve this problem.. We will help when you show us the code that you've written. What OS and Python version are you using? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read from large text file, find string save 1st string of each line where it appeared.
On Mon, Dec 28, 2015 at 6:20 AM, sutanu bhattacharya < totaibhattacha...@gmail.com> wrote: > {'115160371': [45349980, 22477811, 40566595, 26947037, 16178191, 12984002, > 20087719, 19771564, 61746245, 17467721, 32233776, 31052980, 70768904, > 16113331, 12414642]} > > suppose 61746245 is my searching string. so o/p will be > 115160371 (1st string). Area in between third bracket > ([ > ]) is the searching area... > > > kindly help me to solve this problem.. > > -- > Sutanu Bhattacharya > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > def problem(6174625): return 115160371 You haven't really described your problem, so above is a solution for what you asked. What have you tried so far, and what was your result? -- Joel Goldstick http://joelgoldstick.com/stats/birthdays ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Read from large text file, find string save 1st string of each line where it appeared.
{'115160371': [45349980, 22477811, 40566595, 26947037, 16178191, 12984002, 20087719, 19771564, 61746245, 17467721, 32233776, 31052980, 70768904, 16113331, 12414642]} suppose 61746245 is my searching string. so o/p will be 115160371 (1st string). Area in between third bracket ([ ]) is the searching area... kindly help me to solve this problem.. -- Sutanu Bhattacharya ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor