Pandas Dataframe replace string based on length - python
I have a pandas dataframe (df2) with about 160,000 rows. I'm trying to change some of the values in a column (url).
The strings in this column have lengths between 108 and 150 characters. If the string is not 108 characters, I want to replace it with the same string, cutting off the last 10 characters. IF the string is 108 characters. I want to leave it alone. Please note that i'm not trying to make every string 108 characters, I'm just trying to cut off the last 10 characters of any string that isn't 108 characters.
example: len(s) = 114, replace with s[:-10]
I built a function that will do this, but it's insanely slow, probably because it rebuilds the dataframe in each loop.
for i in df2.url:
if len(i) != 108:
new_i = i[:-10]
df2 = df2.replace(i,new_i)
There has to be a faster way to do this, but I haven't been able to figure out how. I would love the expertise of someone more versed in pandas.
Below is an example of 200 rows of the column I'm trying to change:
['https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1301108?gameHash=bde58669fc59c853&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291187?gameHash=f7fcd2d6ca775fb5&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291192?gameHash=005335984c8f8a3a&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1301128?gameHash=fcbd2630c0faec49&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1301159?gameHash=9a7726176fdabfde&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1301169?gameHash=5d816e6d30d2b659&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1301183?gameHash=396641afdcdd99d9&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT02/1271494?gameHash=bd51798e1358c47f',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130153?gameHash=00a7861ac0a23aef',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT02/1271495?gameHash=0d828bbc9aa9996c',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT02/1271497?gameHash=bd4810bb801abf24',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130166?gameHash=1cff679b64acb047',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130177?gameHash=1f92cbefd9a965e0',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT02/1271500?gameHash=abbdae6c3e7b4006',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT02/1271505?gameHash=7c970a84e132a578',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130182?gameHash=ccb50f6e86e4c3df',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130193?gameHash=0995997660a65721',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1301262?gameHash=c594a9a52f46cc50',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130196?gameHash=31553f5bb6ba4420',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1301270?gameHash=5b3babb5d392d78d',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130201?gameHash=3d2aa031c17d90ae',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1301290?gameHash=31ce80069fdbc873',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130210?gameHash=91c7b22cded939ff',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1301305?gameHash=3f8d664b3b988446',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130221?gameHash=a8580ee66ffbb525',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291406?gameHash=5220923eb35c42c6',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291426?gameHash=83c7c51530ea074e',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291442?gameHash=28f7b485f710168f',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291458?gameHash=49cc14d02ccd0674',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291470?gameHash=f087c853097c2dd9',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT02/1261474?gameHash=e6c01a288de5dc41',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130229?gameHash=1489421028163983',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT02/1261475?gameHash=c984e795d6406cd5',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130243?gameHash=5491d110de253089',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT02/1261482?gameHash=f2283324f82caa66&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130253?gameHash=f8e39ae785d11c0c',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130264?gameHash=a98718c088ce663c',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT02/1261488?gameHash=6517011920487fbf&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291651?gameHash=5ec1b3473060dfd2',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291682?gameHash=a8f2c06d04117279',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291703?gameHash=cfb2d078f289825c',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291737?gameHash=cf67a15df43c2bb2',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291748?gameHash=7a3c085cf703d7bd',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291789?gameHash=51e5ed28085fd299',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291812?gameHash=e540d208bbc69bb3',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291835?gameHash=a75ab48a22470022',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1291845?gameHash=2eab12f8ffd0dfd0',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130294?gameHash=ecf040ad60fa9726&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130299?gameHash=499a21480080a722&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130306?gameHash=d0e60bf49b6bf008&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292296?gameHash=3db885bd11a047bc',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292315?gameHash=2ecf71aaea031312',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292329?gameHash=5ed85b948b32b8e8',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292341?gameHash=7335d6ca06763dc0&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292345?gameHash=6f86444cce429244',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292348?gameHash=c6a4eec48810e8d5&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292353?gameHash=6db57c090ed235bd&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292354?gameHash=79845cdf9a6e88db',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120429?gameHash=436739b9e99a246e&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120436?gameHash=58bc4281a76534f3&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120440?gameHash=4b74592ff226c39f&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120447?gameHash=9358d210749ab778&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292579?gameHash=14865e88bd1e30a7',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292607?gameHash=0ae34d7f67620dc4',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292635?gameHash=f94944bb4f061f0d',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292648?gameHash=1338dde99c71877f&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1302501?gameHash=f71748ae9cad5866&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1302519?gameHash=672c1377c3d37ed0&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1302531?gameHash=49cf9a8f3942b9c8&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1302595?gameHash=314d39ea940b354f',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1302628?gameHash=0ab39ec364a3ff5b',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1302635?gameHash=5625553825f5994e',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1302651?gameHash=555c7cd73dff952d',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292960?gameHash=e3ce73c142354517',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1292974?gameHash=ab79b8f6f354bc0b',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1302827?gameHash=6a1a5de57a7ce6b9&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1302855?gameHash=f9144d0822d68632&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1302881?gameHash=369cd071defeadd9&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1302906?gameHash=c65d2e76e9aa721e&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130488?gameHash=411522a3de69bb79',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130489?gameHash=51c4c81c13a484c7',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130496?gameHash=9575986535e4f4c2',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293312?gameHash=8e2209227e28843b',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120557?gameHash=f5bec07774ed5a5e',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293319?gameHash=762cb3a92744846f',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130515?gameHash=548d7e528ef1f81e',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293370?gameHash=8a70038d2eba61de',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293393?gameHash=841d85edbfa78057',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130518?gameHash=6764d64a5ef8377e&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120578?gameHash=838a1db0f44411c8&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120583?gameHash=c542c3368048efd6&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120585?gameHash=925d9c523a0b0bdb&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293765?gameHash=53412e36eb2eab86',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303478?gameHash=8df5ef3d826ad211&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303509?gameHash=d0849b1ba82d4826&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293812?gameHash=48d825f1bb110b55',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303533?gameHash=3a712b015a672d8d&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293850?gameHash=0a29fdee10ed35d0',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293885?gameHash=1ffaffd98da7e806',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303581?gameHash=2bf61273d44c302f&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293897?gameHash=77ccf507e1eaa05c',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293899?gameHash=aa93723cded96f3b',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293901?gameHash=f5fb660360f96ad6',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293909?gameHash=245dbdf428788434',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303619?gameHash=2e2f2ff9c6a32595&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303626?gameHash=3bba86d0f9ff1d11&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293929?gameHash=f4b6f53e68bbbc86',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303641?gameHash=25ffa91aeb9ed707',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293950?gameHash=e2f3a99412844d36',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303655?gameHash=4ff2ebbe72e635bb',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1293964?gameHash=10bd6ec239231196',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130548?gameHash=afd267703d3cbbb1&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303666?gameHash=a30e98d241d22eef',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130553?gameHash=f4360fb632593491&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130560?gameHash=e1e5bae936585a24&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120607?gameHash=f4b702f689f87c90',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130563?gameHash=43a7c73ecd281a63&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120622?gameHash=c87f08d06f392f3f',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120629?gameHash=6b39ee929c2ebc47',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120638?gameHash=eb17c2013b9ee77d',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120649?gameHash=aab6f321110ef3ed',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1294174?gameHash=8f5cb3f02bf790d7&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303861?gameHash=02847551947ca67d&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1294191?gameHash=e574ac58bbe81abb&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303876?gameHash=e733bc45e47f4856&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303904?gameHash=d8aac7332b9edfe8',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1294233?gameHash=6762c2c72bc47359&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303974?gameHash=28f566b2fa35a32a&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1294260?gameHash=246841d34c9660aa&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1303999?gameHash=619d5a2d571a1b01&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304020?gameHash=99508a2da285eb4c&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304032?gameHash=30e5b243a407326c&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304035?gameHash=f8e3702e77f87cc7',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304054?gameHash=db39c4bcb7c2320e&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304075?gameHash=a3d0d6acfb8b92f1&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304079?gameHash=8339c23d8d925f8b',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304081?gameHash=d3e5c8f0270ce96f&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304111?gameHash=64ded61e41c18ccd',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304122?gameHash=bf7e80351592ce98',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304132?gameHash=ff37582431bd7e7b&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130611?gameHash=a099e1df984018a1',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304158?gameHash=62b9c13c8cecf652',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1294417?gameHash=746905a629b8f374',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130621?gameHash=9d171c9622870a7b',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304165?gameHash=c34ae80c4ee8c7bd',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304169?gameHash=ee6bc6a087a6bc36&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304193?gameHash=fe234e8ca7d2343f',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130630?gameHash=b1b183ad3374db06',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304217?gameHash=b29c2b7461c7700f',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304223?gameHash=7c70a52e69b01c56',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130643?gameHash=15bb88ac79a622a1&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120705?gameHash=a6532b3af6accaf2',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304264?gameHash=f5e69d8e2f6bae5e',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1120711?gameHash=79659ad2d107f0d9',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304282?gameHash=f9ea42cec97e930f',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130676?gameHash=f3e34e47140460ff',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304302?gameHash=617f3af3e7d2ab4d',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130696?gameHash=532d412c3f38c0c5',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304324?gameHash=01bf1a7465a412ba',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130709?gameHash=1491ee8228ad66ec',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304356?gameHash=fc584c5143087c0b',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304374?gameHash=175112ba57cbce5e',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304391?gameHash=72ad86120a14eb54',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304399?gameHash=2536b98ac19e617d',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304412?gameHash=a30a480459e9151c',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1294710?gameHash=0d01dd80aa803997',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1294725?gameHash=3ae63821918e2b43',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1294738?gameHash=5fd20a0eec2c86f4',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304487?gameHash=0d5d1e4e719e8c46&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130723?gameHash=c5113e7f25839c2e&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304504?gameHash=fda4895c0bea1e8a&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1294750?gameHash=fa9335e1a61165a5',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130734?gameHash=88de231d14ea4b07&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304521?gameHash=b70af7bde6c54520&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1294784?gameHash=7d1bf4754cda9b46',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130739?gameHash=4ddb470392dd9248&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304544?gameHash=b14635dac9add7b4&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1294813?gameHash=51f4579db6e7049f',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130748?gameHash=8f544ac73c53a606&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304574?gameHash=a5dcde67b90f29e3&tab=overview',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304609?gameHash=7a5a6778a7074f09',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304635?gameHash=4cda5972cf8dd6bd',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304671?gameHash=67e29eccbbc8f667',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304691?gameHash=1572b2bb76b73da1',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304700?gameHash=b50bc9265ac35f9f',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304739?gameHash=b80bb99cbce5bd71',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304768?gameHash=6ed6e9d7108f27e0',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304785?gameHash=13054e8f14fcae76',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304794?gameHash=b4c27881f0c4481c',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304884?gameHash=29e8f7f002108b46',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304889?gameHash=7774d22665d9526d',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304894?gameHash=bac5c580914f7aaa',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1304928?gameHash=b8b029b3d4002fbc',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT04/1130793?gameHash=a3f6e45612b56302',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1295356?gameHash=731afc76037bd245',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1295369?gameHash=e743122ca08b77d8',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1295383?gameHash=072ee1028f03f4c9',
'http://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1295402?gameHash=560c984fc1ba1168',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1295440?gameHash=32cdbf5ce1441159&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1295467?gameHash=bcc21c92fa78e889&tab=overview',
'https://matchhistory.na.leagueoflegends.com/en/#match-details/ESPORTSTMNT01/1295489?gameHash=95386dea5cf1ab09&tab=overview']
Basic Solution
The below solution makes use of a lambda function defined within a call to pandas.DataFrame.apply().
df['url'] = df['url'].apply(lambda x: x if len(x) == 108 else x[:-10])
Here, each value within df['url'] (x) remains the same if len(x) == 108, otherwise it is updated to be x[:-10].
Handling Exceptions
The below solution is similar to that provided above, however in this case some basic exception handling has been implemented within the url_trim() function called by pandas.DataFrame.apply().
This is more robust than the first solution and will not halt code execution when an exception is thrown within pandas.DataFrame.apply() due to unexpected values within df['url'] rows, in these cases the value is simply left unchanged - for example if numpy.nan is used for null values.
def url_trim(x):
try:
if len(x) != 108:
return x[:-10]
else:
return x
except:
return x
df['url'] = df['url'].apply(lambda x: url_trim(x))
The following code will check the length of the url columns and prune of last 10 characters of the string if the string is below 108.The modified url will be included in modified_url column.
# Get string length
df["string_length"] = df["url"].astype(str).str.len()
# Create a filter based on string length
filter_length = df["string_length"]<108
# Extract string for the filter
df["modified_url"]=df["url"]
df.loc[filter_length,"modified_url"]=df[filter_length]["url"].astype(str).str[:-10]
Related
Need to filter some strings elements but I get TypeError: unsupported operand type(s) for |: 'str' and 'str'
So I'm using pandas to filter a csv and I need to filter three different string elements of a column, but when I use the or (|) I get that mistake. Any other way I can filter many strings without having to name different variables to act like one filter each? This is the code: # What percentage of people with advanced education (`Bachelors`, `Masters`, or `Doctorate`) make more than 50K? bdegree = df[(df["education"] == "Bachelors") & (df["salary"] >= "50K")].count() mdegree = df[(df["education"] == "Masters") & (df["salary"] >= "50K")].count() phddegree = df[(df["education"] == "Doctorate") & (df["salary"] >= "50K")].count() all_degrees = bdegree + mdegree + phddegree print(all_degrees) percentaje_of_more50 = (all_degrees / df["education" == "Bachelors"|"Masters"|"Doctorate"].count())*100 print("The percentaje of people with bla bla bla is", percentaje_of_more50["education"].round(1)) By the way, I am working in an error in the logic on this code, so just ignore it :).
== looks for an exact match and since no one's "education" includes the string "Bachelors"|"Masters"|"Doctorate", it will return a Series of all Falses . You can use isin instead like: msk = df["education"].isin(["Bachelors","Masters","Doctorate"]) The above will return a boolean Series, so using the .count method on it will just show the length of it, which is probably not something you want. So you need to use it to filter the relevant rows: df[msk].count() Then you can write percentage_of_more50 as: percentage_of_more50 = (all_degrees / df[msk].count())*100 Note that you can also derive all_degrees using isin as well: all_degrees = df[df["education"].isin(["Bachelors","Masters","Doctorate"]) & (df['salary']>='50K')].count() Also df["salary"] >= "50K" works as you intend only if all salaries are below "99k" otherwise you'll end up with wrong output because if you check "100k" > "50k" it throws up False, even though it's True. One way to get rid of this problem is to fill the "salary" column data with "0"s until each entry is a certain number of characters long using str.zfill like: df['salary'] = df['salary'].str.zfill(5) Then each entry becomes 5 characters long. For example, s = pd.Series(['100k','50k']).str.zfill(5) becomes: 0 0100k 1 0050k dtype: object Then you can make the correct comparison.
Python ValueError: dictionary update sequence element #4 has length 3; 2 is required
I need to transform a tuple to dictionary with its respective key->value. The issue is that for a specific tuple I get the following error: ValueError: dictionary update sequence element #4 has length 3; 2 is required. But for other tuples with the same format it transforms it without problems. Could someone guide me to what is the reason of the error? In the attached code the tuple1 value works fine, but the tuple value gives the above error. tupla = ['.1.3.6.1.4.1.35873.5.1.2.1.1.1.1="314"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.2="10943"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.3="RTU : otu-8000e-Comtec (172.17.74.133)..Alarm type: OPTICAL..Timestamp: Jan 15 2022 - 08:31..Severity: CLEAR..Link name: PROV-21-82-83-84 (PRI) RUTA 7 (PROV) - Port 2..Probable cause:"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.5="1"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.4="port=2"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.6="1"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.7="1"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.8="0x07e6010f081f1400"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.9="otu-8000e-Comtec (172.17.74.133)"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.1="PROV-21-82-83-84 (PRI) RUTA 7 (PROV)"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.2="0"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.3="0.18"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.4=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.5=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.6=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.1=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.2=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.3=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.4=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.5=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.6=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.7=""'] tupla1 = ['.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.1.3701361="3701361"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.2.3701361="CRITICAL"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.3.3701361="CRITICAL"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.4.3701361="VALE-078-001"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.5.3701361="Microreflection Threshold 1 Violation"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.6.3701361="2021-09-02T19:14:04.834Z"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.7.3701361="0"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.8.3701361="1333972"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.9.3701361="http://SRVXPTPRODSTG01.vtr.cl/pathtrak/analysis/view.html#/node/1333972"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.10.3701361="7"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="28400000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="0"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="HOLA"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="30800000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="7"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="CRITICAL"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="40700000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="0"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="NONE"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="35600000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="2"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="CRITICAL"'] miDiccionarioTupla= dict([(tupla[x].split('"')[0]+tupla[x].split('"')[1]).split('=') for x in range(len(tupla))]) print(miDiccionarioTupla) #miDiccionarioTupla1= dict([(tupla1[x].split('"')[0]+tupla1[x].split('"')[1]).split('=') for x in range(len(tupla1))]) #print(miDiccionarioTupla1)
The problem is the fifth item in tupla: '.1.3.6.1.4.1.35873.5.1.2.1.1.1.4="port=2"' That line contains two equal signs, so the final .split('=') produces too many values.
As noted by John Gordon, the data has an extraneous "=" in one of the rows. I am not one hundred percent sure what you are hoping to achieve with your code, but I have a potential solution that might help to deal with the extraneous equals sign. The code may also be a bit easier to read: tupla = ['.1.3.6.1.4.1.35873.5.1.2.1.1.1.1="314"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.2="10943"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.3="RTU : otu-8000e-Comtec (172.17.74.133)..Alarm type: OPTICAL..Timestamp: Jan 15 2022 - 08:31..Severity: CLEAR..Link name: PROV-21-82-83-84 (PRI) RUTA 7 (PROV) - Port 2..Probable cause:"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.5="1"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.4="port=2"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.6="1"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.7="1"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.8="0x07e6010f081f1400"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.9="otu-8000e-Comtec (172.17.74.133)"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.1="PROV-21-82-83-84 (PRI) RUTA 7 (PROV)"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.2="0"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.3="0.18"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.4=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.5=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.6=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.1=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.2=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.3=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.4=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.5=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.6=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.7=""'] tupla1 = ['.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.1.3701361="3701361"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.2.3701361="CRITICAL"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.3.3701361="CRITICAL"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.4.3701361="VALE-078-001"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.5.3701361="Microreflection Threshold 1 Violation"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.6.3701361="2021-09-02T19:14:04.834Z"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.7.3701361="0"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.8.3701361="1333972"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.9.3701361="http://SRVXPTPRODSTG01.vtr.cl/pathtrak/analysis/view.html#/node/1333972"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.10.3701361="7"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="28400000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="0"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="HOLA"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="30800000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="7"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="CRITICAL"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="40700000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="0"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="NONE"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="35600000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="2"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="CRITICAL"'] With Python, it is not necessary to use the length of the object and reference the index of the object (using [x]). We can simply parse the object directly with a for loop (for item in tupla): miDiccionarioTupla = dict() for item in tupla: We can split strings on a character (such as =) AND with this data, we can choose how many times to check for that using the maxsplit parameter. key, value = item.split('=', maxsplit=1) I am presuming you want to eliminate any extra quotes in the values to the right side of your items, so I added a .replace() method call on the value: (i.e. "CRITICAL" becomes CRITICAL). This replaces any examples of " with an empty string, essentially removing all the double quotes. value = value.replace('"', '') miDiccionarioTupla.update({key: value}) print(miDiccionarioTupla) miDiccionarioTupla1 = dict() for item in tupla1: key, value = item.split('=', 1) value = value.replace('"', '') miDiccionarioTupla1.update({key: value}) print(miDiccionarioTupla1)
How do I store a large int in python without the newlines being counted by len()?
So I wanna store a long integer which is too big for one line in python. Do I just ignore PEP 8 and just make it longer than 120 characters? Cause if I do it like this: num="""7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843 8586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 6689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749 3035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776 6572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397 5369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474 8216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586 1786645835912456652947654568284891288314260769004224219022671055626321111109370544217506941658960408 0719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606 0588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450""" and try to access a specific index of that integer or use len() on it I get a length of 1009 instead of the 1000 digits the number actually has. And putting everything into one line would make that line 1004 characters long which doesn't seem that great either.
I would use the following literal over multiple lines in parentheses for cleanliness: num = ( '7316717653' '1330624919' '2251196744' ) so that len(num) from the above example returns: 30
Another option you have is to put the number into another file (say number.txt) and read it at runtime: number.txt 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450 main.py with open("number.txt", "r") as f: number = f.read()
I wouldn't use this personally, but one option is to remove the newlines: num = """ 123 456 """.replace('\n', '') print(repr(num)) # -> '123456'
There's lots of good answers already, but here's one that will give you a bit of extra convenience. You just have to put in a number and the size of the chunks per line, and you can reuse it for lots of long numbers, if needed: Format your number into multiple strings using a for loop and string concatenation: x = str(7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450) y = [] y.append("long_num = (") chunksize = 10 for i in range(0, len(x), chunksize ): y.append("\t"+"\""+x[i:i+chunksize ]+"\"") y.append(")") for part in y: print (part) Outputs the following string that you can use in your code, referencing #blhsing's answer: long_num = ( "7316717653" "1330624919" "2251196744" "2657474235" "5349194934" "9698352031" "2774506326" "2395783180" "1698480186" ... ) ```
You can take a look at this post Is there a way to implement methods like __len__ or __eq__ as classmethods? Simple make a class for your long integer, and replace the len(self) function to not count \n
XLRD: Successfully extracted 2 lists out of 2 sheets, but list comparison won't work
Ok so I have two xlsx sheets, both sheets have in their second column, at index 1, a list of sim card numbers. I have successfully printed the contents of both columns into my powershell terminal as 2 lists, and the quantity of elements in those lists, after extracting that data using xlrd. The first sheet (theirSheet) has 454 entries, the second (ourSheet) has 361. I need to find the 93 that don't exist in the second sheet and put them into (unpaidSims). I could do this manually of course, but I would like to automate this task for the future when I inevitably need to do it again so I am trying to write this python script. Considering python agrees that I have a list of 454 entries, and a list of 361 entries, I thought I just need to figure out a list comparison and I researched that on Stack Overflow, and tried 3 times with 3 different solutions, but each time, when I use that script to produce the third list (unpaidSims), it says 454...meaning it hasn't removed the entries that are duplicated in the smaller list. Please advise. from os.path import join, dirname, abspath import xlrd theirBookFileName = join(dirname(dirname(abspath(__file__))), 'pycel', 'theirBook.xlsx') ourBookFileName = join(dirname(dirname(abspath(__file__))), 'pycel', 'ourBook.xlsx') theirBook = xlrd.open_workbook(theirBookFileName) ourBook = xlrd.open_workbook(ourBookFileName) theirSheet = theirBook.sheet_by_index(0) ourSheet = ourBook.sheet_by_index(0) theirSimColumn = theirSheet.col(1) ourSimColumn = ourSheet.col(1) numColsTheirSheet = theirSheet.ncols numRowsTheirSheet = theirSheet.nrows numColsOurSheet = ourSheet.ncols numRowsOurSheet = ourSheet.nrows # First Attempt at the comparison, but fails and returns 454 entries from the bigger list unpaidSims = [d for d in theirSimColumn if d not in ourSimColumn] print unpaidSims lengthOfUnpaidSims = len(unpaidSims) print lengthOfUnpaidSims print "\nWe are expecting 93 entries in this new list" # Second Attempt at the comparison, but fails and returns 454 entries from the bigger list s = set(ourSimColumn) unpaidSims = [x for x in theirSimColumn if x not in s] print unpaidSims lengthOfUnpaidSims = len(unpaidSims) print lengthOfUnpaidSims # Third Attempt at the comparison, but fails and returns 454 entries from the bigger list unpaidSims = tuple(set(theirSimColumn) - set(ourSimColumn)) print unpaidSims lengthOfUnpaidSims = len(unpaidSims) print lengthOfUnpaidSims
According to the xlrd Documentation, the col method returns "a sequence of the Cell objects in the given column". It doesn't mention anything about comparison of Cell objects. Digging into the source, it appears that they didn't code any comparison methods into the class. As such, the Python documentation states that the objects will be compared by "object identity". In other words, the comparison will be False unless they are the exact same instance of the Cell class, even if the values they contain are identical. You need to compare the values of the Cells instead. For example: unpaidSims = set(sim.value for sim in theirSimColumn) - set(sim.value for sim in ourSimColumn)
Format a python list and search for patterns
I am getting rows from a spreadsheet with mixtures of numbers, text and dates I want to find elements within the list, some numbers and some text for example sg = [500782, u'BMOU9015488', u'SD4', u'CLOSED', -1, '', '', -1] sg = map(str, sg) #sg = map(unicode, sg) #option? if any("-1" in s for s in sg): #do something if matched I don't feel this is the correct way to do this, I am also trying to match stuff like -1.5 and -1.5C and other unexpected characters like OPEN15 compared to 15 I have also looked at sg.index("-1") If positive then its a match (Only good for direct matches) Some help would be appreciated
If you want to call a function for each case, I would do it this way: def stub1(elem): #do something for match of type '-1' return def stub2(elem): #do something for match of type 'SD4' return def stub3(elem): #do something for match of type 'OPEN15' return sg = [500782, u'BMOU9015488', u'SD4', u'CLOSED', -1, '', '', -1] sg = map(unicode, sg) patterns = {u"-1":stub1, u"SD4": stub2, u"OPEN15": stub3} # add more if you want for elem in sg: for k, stub in patterns.iteritems(): if k in elem: stub(elem) break Where stub1, stub2, ... are the fonctions that contains the code for each case. It will be called (max 1 time per strings) if the string contains a matching substring.
What do you mean by "I don't feel this is the correct way to do this" ? Are you not getting the result you expect ? Is it too slow ? Maybe, you can organize your data by columns instead of rows and have a more specific filters. If you are looking for speed, I'd suggest using the numpy module which has a very intersting function called select() Scipy select example By transforming all your rows in a numpy array, you can test several columns in one pass. This function is amazingly efficient and powerful ! Basically it's used like this: import numpy as np a = array(...) conds = [a < 10, a % 3 == 0, a > 25] actions = [a + 100, a / 3, a * 10] result = np.select(conds, actions, default = 0) All values in a will be transformed as follow: A value 100 will be added to any value of a which is smaller than 10 Any value in a which is a multiple of 3, will be divided by 3 Any value above 25 will be multiplied by 10 Any other value, not matching the previous conditions, will be set to 0 Bot conds and actions are lists, and must have the same number of arguments. The first element in conds has its action set as the first element of actions. It could be used to determine the index in a vector for a particular value (eventhough this should be done using the nonzero() numpy function). a = array(....) conds = [a <= target, a > target] actions = [1, 0] index = select(conds, actions).sum() This is probably a stupid way of getting an index, but it demonstrates how we can use select()... and it works :-)