[Tutor] Read from large text file, find string save 1st string of each line where it appeared.
Walter Prins
wprins at gmail.com
Mon Dec 28 16:33:30 EST 2015
Hi Sutanu,
On 28 December 2015 at 11:20, sutanu bhattacharya <
totaibhattacharya at gmail.com> wrote:
> {'115160371': [45349980, 22477811, 40566595, 26947037, 16178191, 12984002,
> 20087719, 19771564, 61746245, 17467721, 32233776, 31052980, 70768904,
> 16113331, 12414642]} <tutor at python.org>
>
> suppose 61746245 <tutor at python.org> is my searching string. so o/p will be
> 115160371 <tutor at python.org> (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
More information about the Tutor
mailing list